r - 在 Shiny 中,数据框可以用作 selectizeInput 中的选择吗?
问题描述
selectizeInput 中的选择是否可以是数据框中的行?如果是这样,返回的数据是否会是所选行中的项目列表?我一直无法完成这项工作。在下面的代码中,cityInput 有效,因为选项是一个字符向量;但是locationInput不起作用,选择框中的项目列表为空。
这是一种常见的情况,用户输入需要根据多列中的值进行选择以确定唯一的行。在下面的示例中,不同的城市具有相同的名称,并且使用州来唯一地确定位置。将两列粘贴在一起是一种解决方案,但在复杂的情况下,这种方法会变得混乱。
library(shiny)
locations <- data.frame(City=c("Ames", "Beaumont", "Beaumont", "Portland", "Portland"),
State=c("IA", "CA", "TX", "ME", "OR"))
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectizeInput("cityInput", "City", choices=NULL, selected=NULL),
selectizeInput("locationInput", "Location", choices=NULL, selected=NULL)
),
mainPanel("Main Panel")
)
)
server <- function(input, output, session) {
updateSelectizeInput(session, 'cityInput',
choices = locations$City,
server = TRUE
)
updateSelectizeInput(session, 'locationInput',
choices = locations,
server = TRUE
)
}
shinyApp(ui, server)
解决方案
显然selectizeInput
需要调用 data.frame 的列value
和label
。
然后它显示了位置的一些东西:
library(shiny)
locations <- data.frame(value=c("Ames", "Beaumont", "Beaumont", "Portland", "Portland"),
label=c("IA", "CA", "TX", "ME", "OR"))
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectizeInput("cityInput", "City", choices=NULL, selected=NULL),
selectizeInput("locationInput", "Location", choices=NULL, selected=NULL)
),
mainPanel("Main Panel")
)
)
server <- function(input, output, session) {
updateSelectizeInput(session, 'cityInput',
choices = locations$value,
server = TRUE
)
updateSelectizeInput(session, 'locationInput',
choices = locations,
server = TRUE
)
}
shinyApp(ui, server)
推荐阅读
- splunk - 在 docker 容器中运行的 Splunk 索引器在 docker 重新启动时覆盖 inputs.conf
- android - 如果互联网不可用,如何从 webview 隐藏链接?Kotlin,Android 工作室
- vba - Excel VBA Web Scraping 未引用创建的 Internet Explorer 对象
- r - 随机森林中的变量选择和预测精度
- c# - 我可以在我的 Mac Catalina 10.15.4 上安全地安装 Mono 并在需要时配置 Mono 32 吗?
- matplotlib - 在 matplotlib 中使用粗体安装字体
- spring - 从 springfox 迁移到 springdoc 时为 Null ApiResponse
- python - Python:从文本文件中读取 URL 并保存结果错误
- arrays - 创建一个扩展数组的类
- swiftui-navigationlink - 放置在 NavigationLink 中时,颜色视图不可见