[英]pickerInput names and values from dataframe R Shiny
I'm not sure if I'm just not searching the right thing, but I'm not able to find an answer.我不确定我是否只是没有寻找正确的东西,但我无法找到答案。
I have a Shiny dashboard which has a picker to subset dataframe columns.我有一个 Shiny 仪表板,它有一个选择器来子集 dataframe 列。
I have another dataframe which contains two columns:我还有另一个 dataframe 包含两列:
dat <- data.frame(
names = c("CODE", "STATE_OR_PROVINCE", "LOCATION_CODE"),
display = c("Code", "State or province", "Location code")
)
What I want is to simply have dat$display = dat$names in the choices = list()
argument inside of pickerInput
, but that throws an error.我想要的只是在
pickerInput
内部的choices = list()
参数中有 dat$display = dat$names ,但这会引发错误。
How can I achieve this?我怎样才能做到这一点? I know I can do it manually using:
我知道我可以手动使用:
pickerInput(
"example",
"Example: ",
choices = c(list("Code" = "CODE"),
list("State or province" = "STATE_OR_PROVINCE"),
list("Location code" = "LOCATION_CODE")
)
)
But this rapidly becomes cumbersome the more choices there are.但是,选择越多,这很快就会变得很麻烦。
Reproducible example:可重现的例子:
library(shiny)
library(tidyverse)
library(shinyWidgets)
dat <- data.frame(
names = c("CODE", "STATE_OR_PROVINCE", "LOCATION_CODE"),
display = c("Code", "State or province", "Location code")
)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
pickerInput(
"example",
"Example: ",
choices = c(list("Code" = "CODE"),
list("State or province" = "STATE_OR_PROVINCE"),
list("Location code" = "LOCATION_CODE")
)
)
),
mainPanel()
)
)
server <- function(input, output) {
observe({
input$example %>% glimpse()
})
}
# Run the application
shinyApp(ui = ui, server = server)
You can use a named vector for the choices
argument:您可以为
choices
参数使用命名向量:
choices_data <- dat$names
names(choices_data) <- dat$display
pickerInput(
"example",
"Example: ",
choices = choices_data
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.