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:
Instead, I would like the text output to read Choice 1
. How can I do this?
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]))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With