1

I couldn't find a solution anywhere,

I have this selector with named variables:

enter image description here

library(shiny)
library(shinythemes)

#ui.r
ui <- fluidPage(
uiOutput('input')
)
#server.r

server = function(input, output) {

    output$input <- renderUI({

        variable1 <- 'dog'
        variable2 <- 'cat'
        variable3 <- 'mouse'


        selectInput(inputId = 'something',
                    label = 'select animal',
                    choices = c(variable1 = 'animal1',
                                variable2= 'animal2',
                                variable3 = 'animal3'))

    })
}

shinyApp(ui, server)

I want to see the result of each variable rather than the name of the variable. I mean I want to be able to select between dog, cat, or mouse. But it has to remain a named choice since in later I will have to use those values as animal1, animal2 or animal3.

That's why this is NOT a solution for me, since it's not named. Even though it produces the selector that I want.


        selectInput(inputId = 'something',
                    label = 'select animal',
                    choices = c(variable1,
                                variable2,
                                variable3 ))

enter image description here

I have tried several attempts like as.character() or making it inside a vector/list but couldn't make this work.

CodingBiology
  • 262
  • 4
  • 13
  • From the heklp for `selectInput`: "choices List of values to select from. If elements of the list are named, then that name — rather than the value — is displayed to the user." So wouldn't `choices=c("dog"="animal1", "cat"="animal2", "mouse"="animal3")` give you what you want? Also, if using the tidyverse, be aware of the concepts of both [tidy](https://cran.r-project.org/web/packages/tidyr/vignettes/tidy-data.html)ness and [NSE](https://dplyr.tidyverse.org/articles/programming.html). – Limey Nov 01 '21 at 13:26
  • Thanks, @Limey , yes of course. That is correct. However, this is just a reproducible example, in my real application, the words: `dog`, `cat` and `mouse` will change and depend on the user. That's why I have to use `variable1`, `variable2` and `variable3`. – CodingBiology Nov 01 '21 at 13:55
  • Btw, @Limey why did you say about being aware of being tidyness? Is there something wrong with the code? – CodingBiology Nov 01 '21 at 13:58
  • If the selections change according to the user, then you're probably going to need `updateSelectInput` in your server function rather than defining the options with variables in the UI function. My reference to tidyness was because the names of your variables (`variableX`) made me think that you might be `select`ing variables from a wide dataset when `filter`ing observations from a long dataset *might* be easier. But you've not given us a use case with test data, so I can't be sure. FWIW, I would always go for the long option if both were feasible: it's more robust. – Limey Nov 01 '21 at 15:32
  • Thanks, @Limey, I will look into `updateSelecInput`. I already found a workaround, but it's not so clean. And thanks for the advice :) – CodingBiology Nov 01 '21 at 18:42

1 Answers1

0

I had the same problem and, thanks to your question and the advice of looking at updateSelectInput, I have solved it.

If you still need, here is the answer...

  1. In global.R, create a list with the names of your variables.
library(shiny)
library(shinythemes)


list_of_variables <- list('dog', 'cat', 'mouse')  

#ui.r
ui <- fluidPage(
  uiOutput('input')
)

  1. In selectInput, choices write "variable1", "variable2" and "variable3" as strings. Indeed, choices needs the names that will be shown in the selector ONLY in char/string format, i.e. choices = c("string" = variable/number/string/list-whatever) (see https://shiny.rstudio.com/reference/shiny/0.12.2/selectInput.html). This is the reason why you can't use a variable here.
#server.r

server = function(input, output, session) {  # Add "session" in function(input, output, session)
  
  output$input <- renderUI({
    
    selectInput(inputId = 'something',
                label = 'select animal',
                choices = list('variable1' = 'animal1',   
                               'variable2' = 'animal2',   
                               'variable3' = 'animal3'))
  })
  1. To have a variable in the selector list, you need to change the names entered in char/string format, "updating" them in a second step with updateSelectInput. In server.R you can add the code below, which recalls your inputId = 'something' and sobstitutes the old choices string names with your (list_of_variables)
  observe({           
    updateSelectInput(           
      session,
      'something',
      choices = unique(list_of_variables)
    )
  })
  
  
}

shinyApp(ui, server)

Here you can find more info about updateSelectInput: https://shiny.oxshef.io/tutorials_controls-dependent-on-data.html

EmaK
  • 154
  • 1
  • 9
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 30 '22 at 04:42