Calling a R shiny app from other applications via encoded url and get it to return a value to them

1,001 views
Skip to first unread message

RN

unread,
Jan 20, 2017, 1:11:00 PM1/20/17
to Shiny - Web Framework for R
I've a shiny app that's working fine & essentially calculates a single metric in response to multiple inputs. 

Now I've a request from someone who wants to use my logic to evaluate this calculated metric and use it in his (non shiny) application. I see that if I bookmark the app it anyways encodes the inputs into the url. 

Is it ok to ask the interfacer to just call shiny somehow with the encoded url? i.e. Is this a good way to convert my shiny app into something like a poor man's API. 

Any gotchas that I ought to be aware of? Normally I use an output function within shiny to render the metric on screen as html. Can shiny somehow return the value to the calling application too without having to parse out the  html page like a screen scraper?

Zhu Harry

unread,
Jan 22, 2017, 2:07:44 AM1/22/17
to Shiny - Web Framework for R
something like this:


citycode <- parseQueryString(session$clientData$url_search)[['cityCode']]

RN

unread,
Jan 22, 2017, 8:26:24 AM1/22/17
to Shiny - Web Framework for R
Thanks Zhu. 

But that takes care of getting the values *into* shiny. 

How do I return them to the calling app? i.e. Say I was displaying a mean and a confidence interval on the shiny ui via a textOutput. 

How do I return these two values back when there's a calling app & not an actual person looking at my shiny screen?

Joe Cheng

unread,
Jan 22, 2017, 11:17:32 AM1/22/17
to RN, Shiny - Web Framework for R
This is coming to Shiny very soon. What is your time frame for this project?
--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/fe8b9ea9-a1b8-48e0-aff7-9df773c6e1d8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

RN

unread,
Jan 22, 2017, 1:36:46 PM1/22/17
to Shiny - Web Framework for R
Thanks Joe. Timeline is as soon as I can get this done! :)

Well, ok so basically I was told I could use DeployR to do this. i.e. Converting my R code into something easily callable from other apps. i.e. An API. 

But I didn't like DeployR much when I tried it. I rather prefer shiny which I also think has a much more vibrant community and better support. And I actually have the UI already coded in Shiny and it works like a charm. I even deployed it within our organization by buying a shinyapps.io  account (we wanted authenticated access). 

So if I can, I want to avoid reinventing the wheel just for this and avoid bringing DeployR into the equation etc. That's why I was exploring if I could hack shiny itself to do this. 

RN

unread,
Jan 31, 2017, 1:08:32 AM1/31/17
to Shiny - Web Framework for R, rpn...@gmail.com
Hello Joe:

Any updates about when this feature will be released into shiny?


On Sunday, January 22, 2017 at 9:47:32 PM UTC+5:30, Joe Cheng [RStudio] wrote:

Bárbara Borges

unread,
Feb 1, 2017, 1:03:43 AM2/1/17
to Shiny - Web Framework for R, rpn...@gmail.com
Hi RN, I don't know what is the level of risk you're willing to have in this project. Joe has been working on a native Shiny API, but it is under development and it's likely it will still change a lot before we release into the master branch. So, if you need something stable, it may be a while (I can't say exactly how long, but at very least, a few weeks). However, if you just need something quick right now, you can use the implementation that we have today (this is kept in an undocumented branch on Github).

First, you need to install that branch of Shiny:
devtools::install_github("rstudio/shiny@feature/api")

Second, for ease of explanation here, let's also set a particular port to listen for your Shiny app (and make it not pop up every time):
options(shiny.launch.browser = FALSE, shiny.port = 7791)

Then. let's say your app looks like this:
library(shiny)

ui <- fluidPage(
  selectInput(inputId = "n_breaks",
    label = "Number of bins in histogram (approximate):",
    choices = c(10, 20, 35, 50),
    selected = 20),
  
  checkboxInput(inputId = "individual_obs",
    label = strong("Show individual observations"),
    value = FALSE),
  
  plotOutput(outputId = "main_plot", height = "300px")
)

server <- function(input, output, session) {
  output$main_plot <- renderPlot({
    hist(faithful$eruptions,
      probability = TRUE,
      breaks = as.numeric(input$n_breaks),
      xlab = "Duration (minutes)",
      main = "Geyser eruption duration")
    
    if (input$individual_obs) {
      rug(faithful$eruptions)
    }
  })
}

shinyApp(ui, server)

So, you have two inputs (`n_breaks` and `individual_obs`) and one plot output (`main_plot`). It's pretty basic, it looks like this:

































So, let's say you want to make this plot available though a URL, where you can also specify the two inputs that control it (`n_breaks` and `individual_obs`). Simply add this to your server function (notice that the content of the function is exactly the same, it's only the name of the object and the function itself that have changed...):

  session$api$main_plot <- servePlot({
    hist(faithful$eruptions,
      probability = TRUE,
      breaks = as.numeric(input$n_breaks),
      xlab = "Duration (minutes)",
      main = "Geyser eruption duration")
    
    if (input$individual_obs) {
      rug(faithful$eruptions)
    }
  })

Now, if you visit the same URL, you get the same app, no problem. But now, you can also visit another URL (this one, static) with the name of the object and the arguments passed in as parameters: http://127.0.0.1:7791/api/main_plot?n_breaks=25&individual_obs="T":



































These two URLs are not really related in any way. In fact, each time that you click "enter" on the API URL, Shiny will start up your app, run it once with the params you give it and immediately shut it down, saving only the artifact that you served with the serve function.

Try changing the values of the inputs on the URL and you'll get a different plot. E.g.:  http://127.0.0.1:7791/api/main_plot?n_breaks=50&individual_obs="F":



































Hope this helps you somewhat! Just keep in mind that this is still a very experimental feature that may change quite a bit in the immediate future... If this is enough for you, you can always just make sure you're on the same version of Shiny (same commit SHA), but that's obviously not a scalable answer if your app changes...

Pieter Lukasse

unread,
Jun 4, 2018, 7:56:51 AM6/4/18
to Shiny - Web Framework for R
Has this feature been released already?

Pieter Lukasse

unread,
Jun 4, 2018, 8:14:04 AM6/4/18
to Shiny - Web Framework for R
Has this feature been released already? And is there support for POST requests? 
Reply all
Reply to author
Forward
0 new messages