In a bigger app I am building I have a situation where I need to update multiple plots in different tabPanel using one action button.
Now, the updating of the plots after pressing the action button works. However, if I go back to a previous tabPanel, I would like to work with the cached value in dataNorm or dataUnif to still be able to update the title of the plot.
Situation:
- Click go to view Uniform hist
- Update Unif hist title
- go to plotNorm tabPanel
- Click go to view Normal hist
- Update Norm hist title
- go back to plotUnif tabPanel (dont click go!)
- Try to update title...
Below you see the example:
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
tabsetPanel(id = "tabset",
tabPanel("plotUnif",
numericInput("unifCount", "Count", 100),
sliderInput("unifRange", "Range", min = -100, max = 100, value = c(-10, 10)),
textInput(inputId = 'titleUnif', "Change Title"),
plotOutput("plotUnif")
),
tabPanel("plotNorm",
numericInput("normCount", "Count", 100),
numericInput("normMean", "Mean", 0),
numericInput("normSd", "Std Dev", 1),
textInput(inputId = 'titleNorm', "Change Title"),
plotOutput("plotNorm")
)
),
actionButton("go", "Plot")
),
mainPanel(
"Bla"
)
)
)
server <- function(input, output){
# Record how many times go has been pushed
v <- reactiveValues(go_rec = 0L)
# Compute new dataUnif only if input$go in new and on that tab (wanted effect: otherwise return cached value)
dataUnif <- eventReactive(input$go,{
shiny::req(input$go > v$go_rec, input$tabset == "plotUnif", cancelOutput = T)
v$go_rec <- input$go
return(runif(input$unifCount, input$unifRange[1], input$unifRange[2]))
})
# same as dataUnif
dataNorm <- eventReactive(input$go,{
shiny::req(input$go > v$go_rec, input$tabset == "plotNorm", cancelOutput = T)
v$go_rec <- input$go
return(rnorm(input$normCount, input$normMean, input$normSd))
})
# Disply hist (be able to change title)
output$plotUnif <- renderPlot({
shiny::req(dataUnif())
hist(dataUnif(), main = input$titleUnif)
})
output$plotNorm <- renderPlot({
shiny::req(dataNorm())
hist(dataNorm(), main = input$titleNorm)
})
}
shinyApp(ui, server)