Update plots in different tabPanels with one action button Shiny

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:

  1. Click go to view Uniform hist
  2. Update Unif hist title
  3. go to plotNorm tabPanel
  4. Click go to view Normal hist
  5. Update Norm hist title
  6. go back to plotUnif tabPanel (dont click go!)
  7. 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)

comment out these reqs and the code gives the desired behaviour, perhaps rethink them.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.