I'm building an app with several tabs, some which involve excessive calculations and others that calculate quickly. A checkbox which would allow the user to choose between reactivity or manual updating, in combination with a "refresh"-button, would be ideal.
The simple example below illustrates what I'm aiming for. It nearly works, except for one final refresh when the "Automatically refresh"-checkbox is unchecked, which is a pain, should a computationally intensive tab be open. Is there any way around this?
ui.r
library(shiny)
shinyUI(fluidPage(
titlePanel("Test"),
sidebarLayout(
sidebarPanel(
checkboxInput("autoRefresh", "Automatically refresh", TRUE),
actionButton("refresh", "Refresh!"),
radioButtons("choice", "Choice of value:",
c("10" = 10,
"20" = 20))
),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Add random decimals to value", textOutput("value"))
)
)
)
))
server.r
library(shiny)
shinyServer(function(input, output) {
output$value <- renderText({
input$refresh
if(input$autoRefresh == 1) {
input$choice
}
isolate({
output <- runif(1,0,1) + as.numeric(input$choice)
})
})
})
Many thanks in advance!
In a Shiny application, the source typically is user input through a browser interface. For example, when the selects an item, types input, or clicks on a button, these actions will set values that are reactive sources. A reactive endpoint is usually something that appears in the user’s browser window, such as a plot or a table of values.
In fact, Shiny will prevent you from calling reactive expressions outside of these functions. Time to fix the broken check box for “Adjust prices for inflation.” Your user should be able to toggle between prices adjusted for inflation and prices that have not been adjusted.
Presently, Shiny has one class of objects that act as reactive sources, one class of objects that act as reactive conductors, and one class of objects that act as reactive endpoints, but in principle there could be other classes that implement these roles.
A reactive endpoint is usually something that appears in the user’s browser window, such as a plot or a table of values. In a simple Shiny application, reactive sources are accessible through the input object, and reactive endpoints are accessible through the output object.
In this solution, I made two observers: one for when the refresh
button is hit and a second for when choice
is changed. The first always updates the output.
The second checks the status of input$autoRefresh
and then either just exits or updates the renderText
.
Unfortunately you must have the runif
command written twice, which can be bad for updating your code (easier to introduce errors if you are doing something twice). In practice you might want to make a new function and then just call that function if this is a complex/multi-line process in your actual app.
shinyServer(function(input, output) {
observe({
input$refresh
output$value<-renderText({
isolate(runif(1,0,1) + as.numeric(input$choice))
})
})
observe({
input$choice
output$value<-if(input$autoRefresh==0) return() else {
renderText({isolate(runif(1,0,1) + as.numeric(input$choice))})
}
})
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With