Brilliant server. Print JSON as a result of output

I am trying to use shiny-server as a process server: receive a URL request, process the R routines, and output JSON as a result. but I could not print the output directly in the browser in JSON.

Is it possible to use shiny-server in this way?

PD: I know this is not a typical use for a brilliant server

Thank you so much!

+7
rest r shiny json-rpc shiny-server
source share
5 answers

It looks like you are trying to create a REST or JSON-RPC web service using a brilliant server. This is currently not possible (using Shiny Server v1.2).

The brilliant server displays the text / html template (shinyUI) page and uses WebSocket callbacks to populate the content. The response from @ScottChamberlain will display the JSON in the body of the HTML web browser. This will not work for a web request program.

I found rApache , Rook, and RJSONIO to become a reliable and efficient solution for JSON web services. You will need to configure the Apache web server and, depending on your platform, create Apache modules.

rApache is a module that includes R in the Apache web server, allowing you to host Rook, brew, and other R-frames.

The Rook defines the interface between the R application and the web server. This makes it easy to deliver your JSON payload with the right content type.

Other options:

  • OpenCPU - a dedicated R HTTP server with explicit JSON RPC support
  • node-rio is a node.js server that interacts with RServe
  • FastRWeb - CGI or PHP interface for connecting a web server to RServe
  • RServe - TCP / IP binary R server
  • httpuv - HTTP and WebSocket server library for R
  • R built in rhttpd - not recommended for use in products
+7
source share

I found this other package today that wraps RPC / REST-ish R functions:

https://github.com/trestletech/plumber

Commenting on the function R, for example:

#' @get /mean normalMean <- function(samples=10){ data <- rnorm(samples) mean(data) } #' @post /sum addTwo <- function(a, b){ as.numeric(a) + as.numeric(b) } 

You can open it as a web api:

 > library(plumber) > r <- plumb("myfile.R") # Where 'myfile.R' is the location of the file shown above > r$run(port=8000) 
+8
source share

How about this simple solution?

https://gist.github.com/sckott/7478126

server.r

 require(shiny) require(RJSONIO) shinyServer(function(input, output) { output$jsonoutput <- renderText({ toJSON(list(a = 10, b = 12)) }) }) 

ui.r

 require(shiny) shinyUI(bootstrapPage( mainPanel( textOutput(outputId="jsonoutput") ) )) 

The text does not print quite, but ...

Also, look at this answer on the Shiny mailing list: https://groups.google.com/forum/#!searchin/shiny-discuss/json $ 20output / shiny-discuss / -JYOXAeLCtI / kslvMve_FmIJ - which is Shiny actually Not intended to serve data as an API.

0
source share

How about hacking brilliant.

 httpHandler = function(req){ message = list(value="hello") return(list(status = 200L, headers = list('Content-Type' = 'application/json'), body = toJSON(message))) } shiny:::handlerManager$addHandler(shiny:::routeHandler("/myEndPoint",httpHandler) , "a_unique_id") # then start your shiny app ... 

Then tell the browser http://127.0.0.1:[shinyport†/myEndPoint/

0
source share

For me, this worked with verbatimTextOutput:

ui.R

 verbatimTextOutput(outputId="jsonoutput") 

server.R - assume that the data to be converted to json is returned by getMainData ()

 output$jsonoutput <- renderText({ data <- getMainData() result <- jsonlite::prettify(jsonlite::toJSON(data, auto_unbox = TRUE), 4) return(result) }) 
0
source share

All Articles