检索选择名称而不是值
Retrieve choice name rather than value
我在 selectInput
中有一个名为 choices
的插槽,我想检索与该选项关联的名称,而不是值。
MWE:
shinyApp(
ui = fluidPage(
sidebarPanel(
selectInput("foo",
label = "Select choice here:",
choices = c("Choice 1" = "Choice1",
"Choice 2" = "Choice2",
"Choice 3" = "Choice3"),
selected = "Choice1",
multiple = TRUE),
textOutput("nameOfChoice")
),
mainPanel()),
server = function(input, output) {
output$nameOfChoice = renderText(input$foo[1])
}
)
产生:
相反,我希望文本输出显示为 Choice 1
。我该怎么做?
将您的选择放在 global.R
中的对象中,然后在 server.R
和 ui.R
中使用它。
在global.R
中:
fooChoices<-c("Choice 1" = "Choice1",
"Choice 2" = "Choice2",
"Choice 3" = "Choice3")
在ui.R
中:
selectInput("foo",
label = "Select choice here:",
choices = fooChoices)
在server.R
中:
output$nameOfChoice = renderText(names(fooChoices[fooChoices==input$foo]))
我在 selectInput
中有一个名为 choices
的插槽,我想检索与该选项关联的名称,而不是值。
MWE:
shinyApp(
ui = fluidPage(
sidebarPanel(
selectInput("foo",
label = "Select choice here:",
choices = c("Choice 1" = "Choice1",
"Choice 2" = "Choice2",
"Choice 3" = "Choice3"),
selected = "Choice1",
multiple = TRUE),
textOutput("nameOfChoice")
),
mainPanel()),
server = function(input, output) {
output$nameOfChoice = renderText(input$foo[1])
}
)
产生:
相反,我希望文本输出显示为 Choice 1
。我该怎么做?
将您的选择放在 global.R
中的对象中,然后在 server.R
和 ui.R
中使用它。
在global.R
中:
fooChoices<-c("Choice 1" = "Choice1",
"Choice 2" = "Choice2",
"Choice 3" = "Choice3")
在ui.R
中:
selectInput("foo",
label = "Select choice here:",
choices = fooChoices)
在server.R
中:
output$nameOfChoice = renderText(names(fooChoices[fooChoices==input$foo]))