Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exit a Shiny app and return a value

Tags:

r

shiny

The help page for runApp says:

Runs a Shiny application. This function normally does not return; interrupt R to stop the application (usually by pressing Ctrl+C or Esc).

Does "normally" mean "always"? Is there any way to add an "exit" button and return a value to the R session that called runApp? Even if it's a hack, this would be convenient functionality. I've tried some searching but haven't found anything. I've also looked at the source for runApp, the last line is return(.globals$retval), so it looks like there should be a way.

Can I set .globals$retval directly and then call some kind of interrupt? Is there a function in the shiny package that does this?

like image 895
mrip Avatar asked Dec 08 '14 19:12

mrip


People also ask

How do you exit a Shiny app?

The official way of ending Shiny (http://rstudio.github.io/shiny/tutorial/#run-and-debug) requires interrupting R by typing Esc or clicking Stop, which also terminates the execution of the calling script.

How do you save data on the Shiny app?

The most trivial way to save data from Shiny is to simply save each response as its own file on the current server. To load the data, we simply load all the files in the output directory. In our specific example, we also want to concatenate all of the data files together into one data. frame.

How do you enter Shiny data?

To add an input in a Shiny app, we need to place an input function *Input() in the ui object. Each input function requires several arguments. The first two are inputId , an id necessary to access the input value, and label which is the text that appears next to the input in the app.


1 Answers

There is a stopApp function that stops the running app and returns an optional value:

myValue <- runApp(list(
  ui = bootstrapPage(
    numericInput('n', 'Number of obs', 100),
    actionButton("myBtn", "Press ME!"),
    plotOutput('plot')
  ),
  server = function(input, output, session) {
    output$plot <- renderPlot({ hist(runif(input$n)) });
    observe({
      if(input$myBtn > 0){
        stopApp(7)
    }
    })
  }
))

On stopping:

> myValue
[1] 7
like image 81
jdharrison Avatar answered Sep 20 '22 15:09

jdharrison