6

I've seen a number of people ask various versions of this question but I haven't found a clear answer.

I would like to send a POST request to an R Shiny application so that after the request is received the Shiny app can render the data from the body of the request. For instance, consider the use case where I wrote some sort of special analyzeData(data) R function that someone would run locally on their laptop. Within that function would be a httr::POST request that would upload the data to the Shiny server, and then the user would have the browser pop open and see the UI showing their data analyzed on the server. In a sense it's kind of like a fancy version of an "upload data" button, but instead of them uploading data from within the Shiny UI they instead would have the upload done before the UI opens by the function request.

So the flow would be:

  1. User runs analyzeData(data) function
  2. That function does a POST request to the Shiny server to send the data
  3. The browser pops open for that user so they can see the results from Shiny analyzing it.

So far as I can tell Shiny can't accept a POST request in this fashion. In fact I'm not sure there is any way for Shiny to accept data besides through a UI. It's almost like I want to have both Plumber and Shiny running on the same machine (Plumber to accept the data POST request, Shiny to then render the resulting UI), but that feels like overengineering.

Am I missing something? Is there a way to have this flow work with Shiny accepting the POST request directly?

Jacqueline Nolis
  • 1,457
  • 15
  • 22
  • 2
    Shiny isn't built around standard HTTP verbs, it uses websockets instead to provide a 'live' connection – anotherfred Feb 28 '21 at 00:16
  • I'm not sure whether you are interested in approaches aside from POST (using shiny and Plumber would be possible for sure). However, you might want to check shiny's `reactivePoll` function. You could use it along with e.g. a database which gets updated by your function or use `reactiveFileReader` to track file changes and trigger reactivity based on user input (not conducted in the UI). – ismirsehregal Mar 01 '21 at 08:18
  • 2
    For future readers: motivated by a talk by @JacquelineNolis, Joe Cheng posted a gist [here](https://gist.github.com/jcheng5/2aaff19e67079840350d08361fe7fb20) showing how to handle POST requests directly in `shiny`. Based on this I left an answer [here](https://stackoverflow.com/questions/25283736/r-shiny-rest-api-communication/71064046#71064046). – ismirsehregal Feb 10 '22 at 22:19
  • I currently do not have enough reputation to comment so that is why I am posting this as an answer. In the scenario you have presented, you want the shiny app to run after the call to analyzeData(), correct? If I understand this correctly, then you can create a function that utilizes analyzeData() and launches your shiny app. [Here](https://shiny.rstudio.com/articles/function.html) is an article of creating a Shiny App as a function that may be of use. – Owen Bezick Feb 28 '21 at 23:21

0 Answers0