检索选择名称而不是值

时间:2015-04-19 15:50:57

标签: r shiny

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

产生:

enter image description here

相反,我希望文本输出读取Choice 1。我怎么能这样做?

1 个答案:

答案 0 :(得分:7)

将您的选择放在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]))