检索选择名称而不是值

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.Rui.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]))