Search code examples
rshinyshinydashboard

Save reactive value of selectInput when switching tabs


I have a selectInput menu that comes up when I have a certain tab open in my window. I use the same selectInput (inside renderMenu) for multiple tabs. I would like to figure out how to save the value chosen on one tab so it will be the chosen value when switching tabs. Here, for example, if I choose the mtcars plots tab and select 'blue', and then switch to mtcars plots 2, I would like the selected color to be kept at 'blue' rather than switching back to the first option of red.

Yes, I am aware that I am not currently doing anything with the colors, I will add that usage in later.


library(shiny)
library(shinythemes)
library(shinydashboard)
library(tidyverse)

options(warn=-1)
data(iris)
data(mtcars)






# Define UI for application that draws a histogram
ui <- dashboardPage(

  
  dashboardHeader(),
  
  
  dashboardSidebar(
    sidebarMenu(id = "menume",

sidebarMenuOutput("colormenu"),
    menuItem("MTCARS", tabName = "mt", icon = icon("user-tie")),
    selectInput("mtvar", "Choose a variable", choices = colnames(mtcars)),
    menuItem("IRIS", icon = icon("envelope-open-text"), tabName = "ir"),
    selectInput("irvar", "Choose a variable", choices = colnames(iris))
    )
  ),
  
  dashboardBody(
    tabItems(
      tabItem("mt", uiOutput("mttabs")),
      tabItem("ir", uiOutput("irtabs"))
      )
      
    )
  )




# ui <- secure_app(ui, enable_admin = TRUE)


# Begin Server ----------------------------------------------

server <- function(input, output, session) {
  
output$colormenu = renderMenu({
  req((input$menume=="mt"& input$mtcarstabsall%in%c(2,3))||
        (input$menume=="ir"& input$iristabsall%in%c(5,6)))
  
  selectInput("colorme", "Choose a color", c("red", "yellow", "green", "blue", "black"))
  
  
  
  
})
  
  
  
  
  
  
  output$mttabs = renderUI({
  output$mtcarsplot1=renderPlot({
    
    
    ggplot(mtcars, aes_string(x = input$mtvar)) + stat_bin(nbins = 10)
    
    
  })
  
  output$mtcarsplot2=renderPlot({
    
    
    ggplot(mtcars, aes_string(x = input$mtvar)) + geom_density()
    })
 
    
  output$mtcarstable1=renderTable({
    tabme= head(mtcars, 5)
    tabme

  
  })
  
  
  
  
  tabsetPanel(id = "mtcarstabsall",
              
              
              tabPanel(id = "mttable","MTcars tables",value=1,
                       fluidRow(box(title = "Table 1",  tableOutput("mtcarstable1")))
              ),
              tabPanel(id = "mtplots","mtcars plots",value=2,
                       fluidRow(box(title = "Plot1", plotOutput("mtcarsplot1"))
                       )),
              tabPanel(id = "mtplots2","mtcars plots 2",value=3,
                       fluidRow(box(title = "Plot1", plotOutput("mtcarsplot2")))))
  
  
  })
  
  
  output$irtabs = renderUI({
  
  output$irisplot1=renderPlot({
    ggplot(iris, aes_string(x = input$irvar)) + stat_bin(nbins = 10)
    
    
  })
  
  output$irisplot2=renderPlot({
    ggplot(iris, aes_string(x = input$irvar)) + geom_density()
    
    
  })
  
  

  output$iristable1=renderTable({
    tabme = head(iris, 5)
    tabme
  })
  
  
  tabsetPanel(id = "iristabsall",
              tabPanel(id = "mttable","iris tables",value=4,
                       fluidRow(box(title = "Table 1",  tableOutput("iristable1")))
              ),
              tabPanel(id = "irisplots","iris plots",value=5,
                       fluidRow(box(title = "Plot1", plotOutput("irisplot1"))
                       )),
              tabPanel(id = "irisplots2","iris plots 2",value=6,
                       fluidRow(box(title = "Plot2", plotOutput("irisplot2"))
                       )))
  })
  
  
}

shinyApp(ui, server)

Solution

  • The issue is that the color menu is re-rendering every time you switch tabs and so it resets the selected value. For something like this what you want to do instead is just show/hide the element rather than add/remove it (which is what you're currently doing with req()).

    You could use a conditionalPanel in your menu or use the shinyjs package with something like the below (remembering to add shinyjs::useShinyjs() to your ui, to show/hide the color menu:

    output$colormenu = renderMenu({
        # Remove the req
        selectInput("colorme", "Choose a color", c("red", "yellow", "green", "blue", "black"))
      })
      
    observe({
        # Show/hide menu based on condition using shinyjs::toggle
        show_menu_condition <- (input$menume=="mt"& input$mtcarstabsall%in%c(2,3)) || (input$menume=="ir"& input$iristabsall%in%c(5,6))
        
        shinyjs::toggle("colormenu",
                        condition = show_menu_condition)
        
    })