Skip to content

Instantly share code, notes, and snippets.

@tomjohn
Last active December 22, 2015 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomjohn/6480507 to your computer and use it in GitHub Desktop.
Save tomjohn/6480507 to your computer and use it in GitHub Desktop.
Automating Interaction with R Shiny App - Revised
library(shiny)
longPause <- 10
updates <- 0
updater <- function(){ updates + 1 }
runApp(list(
ui = pageWithSidebar(
headerPanel("Hello Shiny!"),
sidebarPanel(
sliderInput("obs",
"Number of observations:",
min = 1,
max = 1000,
value = 50)
,
selectInput(inputId = "secPause",
label = "Seconds between displays:",
choices = c(1, 2, 3),
selected = 2)
),
mainPanel(
plotOutput("distPlot")
)
),
server =function(input, output, session) {
updateTracker <- reactive( {
if( updates < 5 ) {
invalidateLater(as.numeric(input$secPause) * 1000, session)
updates <<- as.numeric(updater())
}
else {
invalidateLater(longPause * 1000, session)
updates <<- 0
}
return(updates)
})
output$distPlot <- renderPlot( {
# generate an rnorm distribution and plot it
dist <- rnorm(input$obs)
if(updateTracker() > 0) {
hist(dist, main = paste("Histogram count =" , updateTracker()))
}
else {
hist(dist, main = paste("Long Pause with: updates =", updateTracker()))
}
})
}
))
@tomjohn
Copy link
Author

tomjohn commented Sep 7, 2013

This is a revision to code presented in the following StackOverflow question:
http://stackoverflow.com/questions/18622100/automating-interaction-with-r-shiny-app

Ram’s suggestion helped me get past the error display and make some more, minor changes. The last part of the objective remains undone -- for “new user input to restart it.” The automated cycle of 5 displays should restart when either UI input (obs or secPause) is changed. However, only 1 histogram is shown each time obs is changed, and the short pause of the reactive source input$secPause stays inactive. Any suggestions please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment