【问题标题】:Save reactive value of selectInput when switching tabs切换选项卡时保存 selectInput 的反应值
【发布时间】:2021-07-08 13:54:22
【问题描述】:

当我在窗口中打开某个选项卡时,会出现一个 selectInput 菜单。我对多个选项卡使用相同的 selectInput(在 renderMenu 内)。我想弄清楚如何保存在一个选项卡上选择的值,以便在切换选项卡时将其作为选择的值。在这里,例如,如果我选择 mtcars plots 选项卡并选择“蓝色”,然后切换到 mtcars plots 2,我希望选择的颜色保持在'blue' 而不是切换回红色的第一个选项。

是的,我知道我目前没有对颜色做任何事情,我将在稍后添加该用法。


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)

【问题讨论】:

  • 不是答案,而是替代解决方案。我认为您最好使用 css 或 shinyjs 来简单地隐藏和显示输入。

标签: r shiny shinydashboard


【解决方案1】:

问题是每次切换选项卡时都会重新渲染颜色菜单,因此它会重置所选值。对于这样的事情,您想要做的只是显示/隐藏元素,而不是添加/删除它(这是您当前使用 req() 所做的)。

您可以在菜单中使用条件面板或使用shinyjs 包,如下所示(记得将shinyjs::useShinyjs() 添加到您的用户界面中,以显示/隐藏颜色菜单:

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)
    
})

【讨论】:

  • 所以我尝试了这个并且它有效,现在的问题是菜单在应用程序启动时出现。我希望它默认隐藏,因为打开的第一个选项卡不是我想要显示菜单的选项卡之一。
  • 您可以将菜单(在您的 UI 中)包裹在 shinyjs::hidden 中,它将开始隐藏。
猜你喜欢
  • 1970-01-01
  • 2018-11-20
  • 2020-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-08
  • 2020-04-19
  • 1970-01-01
相关资源
最近更新 更多