[英]Shiny App checkboxInput and conditionalPanel
我是 ShinyApp 的新手。
我想将 checkboxInput() 与 conditionalPanel 一起使用,所以当它被选中时,类型的选项将显示出来(然后用户可以从“啤酒”、“提神”、“烈酒”、“酒”中选择一个类型)。 如果未选中,则不会显示类型选项。
下面是我的代码,但是无论我是否选中该框,当类型选项都没有显示时。 我想我应该在服务器功能中写点什么? 我真的不知道。 感谢您的帮助。
ui <- fluidPage(
titlePanel("BC Liquor Store prices"),
img(src = "BCLS.png",align = "right"),
sidebarLayout(
sidebarPanel(sliderInput("priceInput", "Price", 0, 100, c(25, 40), pre = "$"),
wellPanel(
checkboxInput("checkbox", "Filter by Type", FALSE),
conditionalPanel(
condition="checkbox==true",
selectInput("typeInput", "Product type",
choices = c("BEER", "REFRESHMENT", "SPIRITS", "WINE"),
selected = "WINE")
)
),
uiOutput("countryOutput")
),
mainPanel(
tabsetPanel(
tabPanel("Plot", plotOutput("coolplot")),
tabPanel("Summary", verbatimTextOutput("summary")),
tabPanel("Table", tableOutput("results"))
)
)
)
)
server <- function(input, output, session) {
output$countryOutput <- renderUI({
selectInput("countryInput", "Country",
sort(unique(bcl$Country)),
selected = "CANADA")
})
filtered <- reactive({
if (is.null(input$countryInput)) {
return(NULL)
}
bcl %>%
filter(Price >= input$priceInput[1],
Price <= input$priceInput[2],
Type == input$typeInput,
Country == input$countryInput
)
})
output$coolplot <- renderPlot({
if (is.null(filtered())) {
return()
}
filtered() %>% ggvis(~Alcohol_Content, fill := "#fff8dc") %>%
layer_histograms(width = 1, center = 0)
})
output$results <- renderTable({
filtered()
})
}
好的,您可以将条件输入分为两类。
1) 依赖于 ui.R 的输入(在你的情况下是 checkboxInput)
2) 依赖于 server.R 的输入(在您的示例中不是必需的)
解决方案:
1) 您可以使用 renderUI() 函数轻松解决,请参见下面的示例。
如果你真的想要 2),你需要一个 conditionalPanel,你会在 server.R 中使用一个反应函数,你保存在一个输出对象中,并在 ui.R 中使用小的 JS 代码段访问它。 对我来说,看起来 1) 对你来说已经足够了,如果我弄错了,让我知道然后我们调整答案来解决 2)。
一个提示:
默认情况下,您的“复选框”输入采用布尔值:false。 所以你不会渲染“typeInput”(直到你点击“checkbox”)。 所以到目前为止,“typeInput”是空的。 但是,如果您现在对“typeInput”产生依赖,那么闪亮会被混淆,因为“typeInput”没有被渲染,因此不存在。 所以在使用“typeInput”之前,你应该检查它是否可用: if(!is.null(input$typeInput))
否则闪亮会抱怨你的应用程序中实际上没有“typeinput”(再次:至少直到您单击“复选框”)。
ui <- fluidPage(
titlePanel("BC Liquor Store prices"),
img(src = "BCLS.png",align = "right"),
sidebarLayout(
sidebarPanel(sliderInput("priceInput", "Price", 0, 100, c(25, 40), pre = "$"),
wellPanel(
checkboxInput("checkbox", "Filter by Type", FALSE),
uiOutput("conditionalInput")
),
uiOutput("countryOutput")
),
mainPanel(
tabsetPanel(
tabPanel("Plot", plotOutput("coolplot")),
tabPanel("Summary", verbatimTextOutput("summary")),
tabPanel("Table", tableOutput("results"))
)
)
)
)
server <- function(input, output, session) {
output$countryOutput <- renderUI({
selectInput("countryInput", "Country",
sort(unique(bcl$Country)),
selected = "CANADA")
})
output$conditionalInput <- renderUI({
if(input$checkbox){
selectInput("typeInput", "Product type",
choices = c("BEER", "REFRESHMENT", "SPIRITS", "WINE"),
selected = "WINE")
}
})
filtered <- reactive({
if (is.null(input$countryInput)) {
return(NULL)
}
bcl %>%
filter(Price >= input$priceInput[1],
Price <= input$priceInput[2],
Type == input$typeInput,
Country == input$countryInput
)
})
output$coolplot <- renderPlot({
if (is.null(filtered())) {
return()
}
filtered() %>% ggvis(~Alcohol_Content, fill := "#fff8dc") %>%
layer_histograms(width = 1, center = 0)
})
output$results <- renderTable({
filtered()
})
}
# run the app
shinyApp(ui = ui, server = server)
如果有人像我一样在Shiny的模块中工作时遇到此问题,我将在此答案中添加内容。 在这种情况下,使用复选框作为conditionalPanel的条件可能会遇到此处描述的问题: 闪亮的应用程序checkboxInput和conditionalPanel
tl; dr:在Shiny中模块中的conditionalPanel中,checkboxInput的名称空间会影响面板的条件。 对我有用的唯一解决方案是:
checkboxInput(ns("smooth"), "Smooth"),
conditionalPanel(
condition = paste0("input['", ns("smooth"), "'] == true"),
...
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.