我在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])
  }
)

产生:

相反,我希望文本输出读取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]))

09-25 18:07