1

I'm building an R/Shiny app which is supposed to execute some code when called via HTTP GET, much similar to the first answer to this question. Now, the app works just fine when I view it through the web browser, but what I want is really to be able to call the app remotely outside of the web browser, e.g. via a HTTP GET call from cURL or server-side javascript, and make it execute code using URL parameters sent to it as input.

If I visit the following URL in my web browser (assuming that the Shiny app is running on localhost), everything works just fine:

http://127.0.0.1:8000/?param1=val1&param2=val2

But if I instead call the same URL using cURL, nothing happens (except the HTML for the Shiny App is returned):

curl -X GET http://127.0.0.1:8000/\?param1=val1\&param2=val2

My app was basically built as an expansion upon the example app in the first answer to the question linked above (by user @jdharrison), only it also executes some code that is exclusively server-side (i.e. not visible to the user and not returned on the front end), so that should hopefully suffice as a code example:

library(shiny)
runApp(list(
  ui = bootstrapPage(
    textOutput('text')
  ),
  server = function(input, output, session) {
    output$text <- renderText({
      query <- parseQueryString(session$clientData$url_search)
      paste(names(query), query, sep = "=", collapse=", ")
    })
  }
), port = 5678, launch.browser = FALSE)

Many thanks in advance!

Community
  • 1
  • 1
LCHansson
  • 129
  • 5

1 Answers1

3

If the resulting HTML contains Javascript, execution in a browser will be very different from execution in Curl because the latter won't execute the Javascript parts.

user4416936
  • 151
  • 1
  • 10
  • That's a good point - maybe the whole design idea was wrong from the start. The core question remains, though: is it possible to use Shiny to build a working web API in a similar vein to a generic RESTful API, accessible via generic GET? – LCHansson Jan 04 '15 at 15:46
  • 2
    @LCHansson Yes, I think the whole idea of using Shiny is just wrong. Shiny may not be useful at all without JavaScript and WebSockets. I recommend you to start from Shiny's dependency, the [httpuv](http://cran.rstudio.com/package=httpuv) package, instead. – Yihui Xie Jan 04 '15 at 19:43
  • @Yihui, sorry for the late answer. Your comment is exactly the kind of pointer I was looking for. If you would care to convert the comment into an answer I'd gladly mark it as Solution. Thanks! – LCHansson Jan 11 '15 at 17:51
  • 2
    @LCHansson What I said was not completely correct, but I just do not have time to give you the full picture. It really depends on what you really want. There might be some utilities in shiny that you can use. In particular, `session$registerDataObj()`, which is a potentially very powerful function but I have rarely mentioned it in public. It is a little bit involved to explain it. If you are curious enough, I'd recommend you to study how `renderDataTable()` was implemented in shiny, or Section 3.2 of http://rstudio.github.io/DT/ – Yihui Xie Jan 11 '15 at 20:57
  • @Yihui OK, I'll look into how DataTable is called from within Shiny then. Thank you for the pointers, and for your time! – LCHansson Jan 12 '15 at 18:37