Reputation: 375
I would like to add a checkbox (input$autorefresh
) in my shiny application to control where my data input is auto updated (=reactive()
) at every change, or whether it is only updated when a button (=input$refresh
) is pushed. The idea is described in the following code, which I however didn't expect to work. I could use reactive()
together with a conditional isolate()
, but since I have many inputs, that is not very elegant. Any ideas?
if (input$autorefresh==TRUE){
dataInput <- reactive({
dosomething
})
} else {
dataInput <- eventReactive(input$refresh,{
dosomething
})
}
Upvotes: 1
Views: 1186
Reputation: 29407
Are you looking for something like this?
library(shiny)
ui <- fluidPage(
checkboxInput("autorefresh","autorefresh", F),
actionButton("refresh","refresh"),
mainPanel(plotOutput("plot"))
)
autoInvalidate <- reactiveTimer(1000)
server <- function(input, output, session) {
data <- reactive({
input$refresh
data <- plot(rnorm(100),type="l",col="red")
if(input$autorefresh){
autoInvalidate()
return(data)
}
return(data)
})
output$plot <- renderPlot({
data()
})
}
runApp(shinyApp(ui = ui, server = server))
Upvotes: 3