Skip to content

Instantly share code, notes, and snippets.

@Ram-N
Created September 5, 2013 19:32
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 Ram-N/6454997 to your computer and use it in GitHub Desktop.
Save Ram-N/6454997 to your computer and use it in GitHub Desktop.
Response to StackOverflow R (Shiny) question around Automating interaction. (2 files)
library(shiny)
updates <- 0
updater <- function(updates){ updates + 1 }
shinyServer(function(input,output, session){
updateTracker <- reactive( {
invalidateLater(as.numeric(input$secPause) * 1000, session)
updates <<- as.numeric(updater(updates))
})
autoControl <- reactive( {
if(updateTracker() <= 5)
secPause <<- input$secPause
else
secPause <<- 60*60
return(secPause)
})
output$distPlot <- renderPlot( {
dist <- rnorm(input$obs)
if(updateTracker() <= 5) {
# generate an rnorm distribution and plot it
hist(dist, main = paste("Histogram count =" , updateTracker() )
)
}
else {
updates <<- 0
hist(dist, main = paste("Last Histogram count =",
as.numeric(updater(updates)), "with secPause =",
as.numeric(autoControl()))
)
}
})
})
library(shiny)
# Define UI for application that plots random distributions
shinyUI(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, 60*60),
selected = 1)
),
mainPanel(
plotOutput("distPlot")
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment