Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve choice name rather than value

Tags:

r

shiny

I have a named choices slot in selectInput, and would like to retrieve the name associated with the choice, rather than the value.

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

Which produces:

enter image description here

Instead, I would like the text output to read Choice 1. How can I do this?

like image 816
tchakravarty Avatar asked Apr 19 '15 15:04

tchakravarty


1 Answers

Put your choices in an object in global.R and then use it in both server.R and ui.R.

In global.R:

fooChoices<-c("Choice 1" = "Choice1",
                        "Choice 2" = "Choice2",
                        "Choice 3" = "Choice3")

In ui.R:

selectInput("foo",
            label = "Select choice here:",
            choices = fooChoices) 

In server.R:

output$nameOfChoice = renderText(names(fooChoices[fooChoices==input$foo]))
like image 117
nicola Avatar answered Oct 13 '22 03:10

nicola