gpt4 book ai didi

r - 如何使conditionalPanel根据shinydashboard中的事件选项卡显示菜单项

转载 作者:行者123 更新时间:2023-12-04 09:25:41 26 4
gpt4 key购买 nike

我正在尝试制作一个带有一堆不同选项卡的 Shiny 仪表板,这些选项卡显示不同类型的数据。我想要的是选择某个TabiTem时,在侧栏中选择选择inptinput项目。 (最终我希望在多个选项卡上发生这种情况,但我现在只在一个选项卡上工作。)
这是我想要的可执行示例:


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

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

tabset1 = tabsetPanel(id = "mtcars",
tabPanel(id = "mtplots","mtcars plots",
fluidRow(box(title = "Plot1", plotOutput("mtcarsplot1"))
)),


tabPanel(id = "mttable","MTcars tables",
fluidRow(box(title = "Table 1", tableOutput("mtcarstable1")))
))



tabset2 = tabsetPanel(id = "iris",
tabPanel(id = "iris","iris plots",
fluidRow(box(title = "Plot1", plotOutput("irisplot1"))
)),


tabPanel(id = "mttable","iris tables",
fluidRow(box(title = "Table 1", tableOutput("iristable1")))
))




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


dashboardHeader(),


dashboardSidebar(
sidebarMenu(
menuItem("MTCARS", tabName = "mt", icon = icon("user-tie")),
selectInput("mtvar", "Choose a variable", choices = colnames(mtcars)),
sliderInput("mtlines", "Number of lines", 1,50,10),


# **I would like a conditionalPanel here such that if the tab mtplots is selected, a selectInput as below shows up - but only is visible for that tab **

#selectInput("colorvar", "choose a color", choices = c("red", "yellow", "green", "blue"))


menuItem("IRIS", icon = icon("envelope-open-text"), tabName = "ir"),
selectInput("irvar", "Choose a variable", choices = colnames(iris)),
sliderInput("irislines", "Number of lines", 1,50,10)
)
),

dashboardBody(
tabItems(
tabItem("ir", tabset2),
tabItem("mt", tabset1)
)

)
)






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

server <- function(input, output, session) {

output$mtcarsplot1=renderPlot({


ggplot(mtcars, aes_string(x = input$mtvar)) + geom_histogram()


})

output$irisplot1=renderPlot({
ggplot(iris, aes_string(x = input$irvar)) + geom_histogram()


})


output$mtcarstable1=renderTable({
head(mtcars, input$mtlines)

})


output$iristable1=renderTable({
head(iris, input$irislines)

})




}

shinyApp(ui, server)

最佳答案

您可以使用 input$mtcars确定 tabsetPanel 中的哪个选项卡处于事件状态。要呈现动态/条件 UI 元素,您可以使用 uiOutput/renderUI .在 renderUI , 我用 req仅在选择正确的 tabPanel 时才呈现它:

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

data(iris)
data(mtcars)

tabset1 = tabsetPanel(id = "mtcars",
tabPanel(id = "mtplots","mtcars plots",
fluidRow(box(title = "Plot1", plotOutput("mtcarsplot1"))
)),


tabPanel(id = "mttable","MTcars tables",
fluidRow(box(title = "Table 1", tableOutput("mtcarstable1")))
))



tabset2 = tabsetPanel(id = "iris",
tabPanel(id = "iris","iris plots",
fluidRow(box(title = "Plot1", plotOutput("irisplot1"))
)),


tabPanel(id = "mttable","iris tables",
fluidRow(box(title = "Table 1", tableOutput("iristable1")))
))




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


dashboardHeader(),


dashboardSidebar(
sidebarMenu(
menuItem("MTCARS", tabName = "mt", icon = icon("user-tie")),
selectInput("mtvar", "Choose a variable", choices = colnames(mtcars)),
sliderInput("mtlines", "Number of lines", 1,50,10),


# **I would like a conditionalPanel here such that if the tab mtplots is selected, a selectInput as below shows up - but only is visible for that tab **


uiOutput("UI_conditional_input"),

menuItem("IRIS", icon = icon("envelope-open-text"), tabName = "ir"),
selectInput("irvar", "Choose a variable", choices = colnames(iris)),
sliderInput("irislines", "Number of lines", 1,50,10)
)
),

dashboardBody(
tabItems(
tabItem("ir", tabset2),
tabItem("mt", tabset1)
)

)
)






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

server <- function(input, output, session) {

output$mtcarsplot1=renderPlot({


ggplot(mtcars, aes_string(x = input$mtvar)) + geom_histogram()


})

output$irisplot1=renderPlot({
ggplot(iris, aes_string(x = input$irvar)) + geom_histogram()


})


output$mtcarstable1=renderTable({
head(mtcars, input$mtlines)

})


output$iristable1=renderTable({
head(iris, input$irislines)

})

output$UI_conditional_input <- renderUI({
req(input$mtcars == "mtcars plots")
selectInput("colorvar", "choose a color", choices = c("red", "yellow", "green", "blue"))

})




}

shinyApp(ui, server)

关于r - 如何使conditionalPanel根据shinydashboard中的事件选项卡显示菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63021852/

26 4 0