我有一个命名choices插槽selectInput,并希望检索与选择关联的名称,而不是值.
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])
}
)
Run Code Online (Sandbox Code Playgroud)
哪个产生:

相反,我希望文本输出读取Choice 1.我怎样才能做到这一点?
把你的选择,在一个对象global.R,然后在都使用它server.R和ui.R.
在global.R:
fooChoices<-c("Choice 1" = "Choice1",
"Choice 2" = "Choice2",
"Choice 3" = "Choice3")
Run Code Online (Sandbox Code Playgroud)
在ui.R:
selectInput("foo",
label = "Select choice here:",
choices = fooChoices)
Run Code Online (Sandbox Code Playgroud)
在server.R:
output$nameOfChoice = renderText(names(fooChoices[fooChoices==input$foo]))
Run Code Online (Sandbox Code Playgroud)