2

I started to play a little with shiny...

Is there a possibility to execute commands after the shiny app closes?

So here is a minimal example:

library(dplyr)
library(ggvis)
library(shiny)
library(ggplot2)
df <- data.frame(x=rnorm(10), y=rnorm(10), id=letters[1:10])
server <- function(input, output) {
  movie_tooltip <- function(x) {
    x$id
  }
  vis <- reactive({
    df %>%
      ggvis(~x, ~y) %>% 
      layer_points(key := ~id)  %>%
      add_tooltip(movie_tooltip, "hover")
  })
  vis %>% bind_shiny("plot1")  
}
ui <- fluidPage(
  ggvisOutput("plot1")
)
shinyApp(ui = ui, server = server) 

ggplot(df, aes(x, y)) + geom_point()

I would love to execute that ggplot command after closing the shiny app.

drmariod
  • 11,106
  • 16
  • 64
  • 110
  • 2
    Does http://stackoverflow.com/questions/27365575/how-to-exit-a-shiny-app-and-return-a-value help? (i.e. just add a button that exits vs use the "stop" button or window close icon) – hrbrmstr Mar 18 '15 at 07:32
  • Thanks, this already helps, but give me a `Graphics error: Plot rendering error` but additionally **sometimes** shows me the graph I am looking for... Don't know why it is not always working and why there is an error. – drmariod Mar 18 '15 at 07:46
  • 2
    I can reproduce said issue, but only in RStudio (the ggplot renders consistently in R.app or R command line run). This is probably an RStudio bug and is prbly worth reporting to https://support.rstudio.com – hrbrmstr Mar 18 '15 at 07:52
  • I wrote to the support... https://support.rstudio.com/hc/communities/public/questions/202974618-Executing-command-after-closing-shiny-app – drmariod Mar 18 '15 at 09:04

1 Answers1

2

As hrbrmstr suggested, I adapted his comment into my example.

library(dplyr)
library(ggvis)
library(shiny)
library(ggplot2)
df <- data.frame(x=rnorm(10), y=rnorm(10), id=letters[1:10])
server <- function(input, output) {
  movie_tooltip <- function(x) {
    x$id
  }
  vis <- reactive({
    df %>%
      ggvis(~x, ~y) %>% 
      layer_points(key := ~id)  %>%
      add_tooltip(movie_tooltip, "hover")
  })
  vis %>% bind_shiny("plot1") 
  observe({
    if(input$myBtn > 0){
      stopApp()
    }
  })
}
ui <- fluidPage(
  ggvisOutput("plot1"),
  actionButton("myBtn", "Press ME!")
)
shinyApp(ui = ui, server = server) 
ggplot(df, aes(x, y)) + geom_point()

This produces sometimes an error Graphics error: Plot rendering error but the graph get created... And sometimes the graph is not created at all.

I figured out, that it is also not possible to execute several things after the shinyApp command.

So for example, if I add several plots, only the first gets (sometimes) created

ggplot(df, aes(x, y)) + geom_point() + ggtitle("graph 1")
ggplot(df, aes(x, y)) + geom_point() + ggtitle("graph 2")
ggplot(df, aes(x, y)) + geom_point() + ggtitle("graph 3")
ggplot(df, aes(x, y)) + geom_point() + ggtitle("graph 4")
ggplot(df, aes(x, y)) + geom_point() + ggtitle("graph 5")
ggplot(df, aes(x, y)) + geom_point() + ggtitle("graph 6")
ggplot(df, aes(x, y)) + geom_point() + ggtitle("graph 7")
ggplot(df, aes(x, y)) + geom_point() + ggtitle("graph 8")
ggplot(df, aes(x, y)) + geom_point() + ggtitle("graph 9")
drmariod
  • 11,106
  • 16
  • 64
  • 110