Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

r - How to exit a Shiny app and return a value

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...