Created
September 5, 2013 19:32
-
-
Save Ram-N/6454997 to your computer and use it in GitHub Desktop.
Response to StackOverflow R (Shiny) question around Automating interaction. (2 files)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) | |
) | |
} | |
}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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