闪亮的新手,似乎还找不到此信息:我正在制作一个带有两个selectInput下拉菜单的应用程序,以便第二个(以黄色突出显示)依赖于第一个。
这是用于数据探索,基本上会遍历不同的文件夹内容。这就是我想要的:
ui <- fluidPage(
selectInput(inputId = "site", label = "Select Site", choices = dir(basepath)),
selectInput(inputId = "duct", label = "Select Duct",
choices = dir(grep(input$site, dir(basepath, full.names = T), val = T)))
)
Run Code Online (Sandbox Code Playgroud)
显然,上面的代码不起作用。我不确定如何input$site在另一个输入功能中使用。
我找到了一些答案:在 R Shiny 中使用 selectinput 函数和在 Shiny ui.R 和 server.R 中使用条件面板显示多个输入框选择多个变量:基于条件的不同 selectInput但它们并不直接适用,因为我没有第一个输入 ( inputId = "site") 的可能选项的详尽列表。
我根据mtcars数据集创建了一个小示例。在这里,您会发现 中的齿轮selectInput site将对 的数据进行子集化,并将返回第二个selectInput调用的齿轮duct,该齿轮仅显示第一个齿轮中选定的值:
library(shiny)
ui <- basicPage(
headerPanel("Test Shiny App"),
sidebarPanel(
selectInput(inputId = "site", label = "Select Site", choices = unique(mtcars$gear)),
uiOutput("dynamicui")),
mainPanel()
)
server <- function(input, output, session){
output$dynamicui <- renderUI({
selectInput(inputId = "duct", label = "Select Duct", choices = rownames(mtcars)[mtcars$gear %in% input$site])
})
}
runApp(list(ui = ui, server = server))
Run Code Online (Sandbox Code Playgroud)