سؤال

I'm trying to use shiny-server as a process server: receive URL request, process R subroutines and output JSON as a result. but I have been unable print the output directly to the browser in JSON.

Is posible to use shiny-server in this way?

PD: I know that this is not a tipical use for shiny server

Thanks a lot!

هل كانت مفيدة؟

المحلول 2

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

Shiny server renders a text/html template (shinyUI) page and uses WebSocket callbacks to populate the content. The answer from @ScottChamberlain will render the JSON in the HTML body of a web browser. This won't work for a programmatic web request.

I found rApache, Rook and RJSONIO to be a robust and performant solution for JSON web services. You will need to be comfortable with configuring the Apache web server and, depending on your platform, building Apache modules.

rApache is a module that embeds R into the Apache web server allowing you to host Rook, brew and other R frameworks.

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

Other options include:

  • OpenCPU - A dedicated R HTTP server with explicit support for JSON RPC
  • node-rio - node.js server that interfaces RServe
  • FastRWeb - CGI or PHP interface to connect a web server to RServe
  • RServe - Binary R TCP/IP server
  • httpuv - HTTP and WebSocket server library for R
  • R's built in rhttpd - not recommended for production use

نصائح أخرى

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

https://github.com/trestletech/plumber

By commenting an R function like so:

#' @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 expose 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)

What 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 doesn't print pretty, but...

Also, have a look at this answer on the Shiny mailing list: https://groups.google.com/forum/#!searchin/shiny-discuss/json$20output/shiny-discuss/-JYOXAeLCtI/kslvMve_FmIJ - that Shiny isn't really designed to serve data as an API.

What about hacking shiny.

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 point you browser to http://127.0.0.1:[shinyport]/myEndPoint/

For me it worked by using verbatimTextOutput:

ui.R

verbatimTextOutput(outputId="jsonoutput")

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

output$jsonoutput <- renderText({

data <- getMainData()


result <- jsonlite::prettify(jsonlite::toJSON(data, auto_unbox = TRUE), 4)

return(result)
})
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top