r-切换选项卡时,保存selectInput的无功值



当我的窗口中打开某个选项卡时,会出现一个selectInput菜单。我对多个选项卡使用相同的selectInput(在renderMenu中(。我想知道如何保存在一个选项卡上选择的值,以便在切换选项卡时它将是所选的值。例如,在这里,如果我选择mtcars plots选项卡并选择"blue",然后切换到mt cars plot 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)

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

您可以在菜单中使用conditionalPanel,也可以使用shinyjs包和以下内容(记住将shinyjs::useShinyjs()添加到ui中,以显示/隐藏彩色菜单:

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)

})

最新更新