[英]Shinydashboard multiple conditions in conditionalPanel with same inputs
I'm building an app with shinyDashboard. 我正在使用ShinyDashboard构建应用程序。 I want to display several selectInput in sidebarMenu regarding the selected tabItem AND tabPanel. 我想在sidebarMenu中显示有关所选tabItem和tabPanel的几个selectInput。 The same selectInput are used in different tabItem. 在不同的tabItem中使用相同的selectInput。
It looks simple but I struggle with the conditional syntax in conditionalPanel using multiples arguments with both AND (&&), OR (||) and IN (%in%) operators. 看起来很简单,但是我在使用与AND(&&&),OR(||)和IN(%in%)运算符同时使用倍数参数的conditionalPanel中的条件语法时遇到了麻烦。 I tried to add bracket but it is not doing the job. 我试图添加支架,但没有完成任务。
I wrote this code, with is reproductible and working but not doing what I want as its always display the selectInputs. 我编写了这段代码,该代码具有可复制性并且可以正常工作,但是没有执行我想要的操作,因为它始终显示selectInputs。
library(shinydashboard)
library(dplyr)
mtcars$gear <- as.character(mtcars$gear)
all_gears <- sort(unique(mtcars$gear))
mtcars$cyl <- as.character(mtcars$cyl)
all_cyl <- sort(unique(mtcars$cyl))
ui <- dashboardPage(
dashboardHeader(title = "test"),
dashboardSidebar(
sidebarMenu(id="menu1",
menuItem(
"Dashboard",
tabName = "dashboard",
icon = icon("dashboard")
),
menuItem(
"Indicators",
tabName = "indicators",
icon = icon("info-circle")
)
),
conditionalPanel(
condition = "input.menu1 == 'dashboard' && input.tabselected %in% c('1','2')",
selectInput(
inputId = "cylinders",
label = "Select number of cylinders",
choices = all_cyl,
selected = '4',
multiple = TRUE,
selectize = FALSE
)
),
conditionalPanel(
condition = "(input.menu1 == 'dashboard' && input.tabselected == 2) || input.menu1 == 'indicators'",
selectInput(
inputId = "gearsnumber",
label = "Select number of gears",
choices = all_gears,
selected = '3',
multiple = TRUE,
selectize = FALSE
)
)
),
dashboardBody(
tabItems(
tabItem(tabName = "dashboard",
tabsetPanel(
tabPanel("Graph", value=1, plotOutput("plot")),
tabPanel("Table", value=2, dataTableOutput("table")),
tabPanel("Empty", value=3)
)
),
tabItem(tabName = "indicators",
infoBoxOutput("totalweight")
)
)
)
)
server <- function(input, output, session) {
selectedDatacyl <- reactive({
req(input$cylinders)
df <- as.data.frame(mtcars)
df$gear <- as.character(df$gear)
df$cyl <- as.character(df$cyl)
df <- mtcars
df %>% dplyr::filter(cyl %in% input$cylinders)
})
selectedDatagears <- reactive({
req(input$gearsnumber)
df <- selectedDatacyl()
df %>% dplyr::filter(gear %in% input$gearsnumber)
})
output$plot <- renderPlot({
ggplot( data = selectedDatacyl(), aes(x = rownames(selectedDatacyl()), y = mpg)) + geom_point()
})
output$table <- DT::renderDataTable({
DT::datatable( data = selectedDatagears(),
options = list(pageLength = 14),
rownames = FALSE)
})
output$totalweight <- renderInfoBox({
infoBox(
"Total weight",
paste0(sum(selectedDatagears()$wt), "lbs"),
icon = icon("chart-area"),
color = "green"
)
})
}
shinyApp(ui = ui, server = server)
What should I do to make thoses conditions operational? 我应该怎么做才能使这些条件可行? Thanks to all contribs. 感谢所有的贡献。
The condition in conditionalPanel
is a JavaScript expression, not a R expression. conditionalPanel中的conditionalPanel
是JavaScript表达式,而不是R表达式。
So you have to replace 所以你必须更换
input.menu1 == 'dashboard' && input.tabselected %in% c('1','2')
with 与
input.menu1 == 'dashboard' && (input.tabselected == '1' || input.tabselected == '2')
or 要么
input.menu1 == 'dashboard' && ['1','2'].indexOf(input.tabselected) > -1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.