1

Is it possible for the choices in selectizeInput to be rows from a data frame? If so, would the returned data be a list of items from the selected row? I have been unable to make this work. In the code below, cityInput works since the choices are a character vector; but locationInput does not work, the item list in the select box is empty.

This is a common occurrence where user input requires selection based on the values in multiple columns to determine a unique row. In the example below, different cities have the same name and state is used to uniquely determine a location. Pasting the two columns together is one solution, but in complex cases this approach gets messy.

library(shiny)

locations <- data.frame(City=c("Ames", "Beaumont", "Beaumont", "Portland", "Portland"),
                        State=c("IA", "CA", "TX", "ME", "OR"))

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectizeInput("cityInput", "City", choices=NULL, selected=NULL),
      selectizeInput("locationInput", "Location", choices=NULL, selected=NULL)
    ),
    mainPanel("Main Panel")
  )
)

server <- function(input, output, session) {
  updateSelectizeInput(session, 'cityInput',
              choices = locations$City,
              server = TRUE
  )
  updateSelectizeInput(session, 'locationInput',
              choices = locations,
              server = TRUE
  )
}

shinyApp(ui, server)
Lee
  • 405
  • 2
  • 10

1 Answers1

1

Apparently selectizeInput needs the columns of the data.frame to be called value and label.

Then it shows something for the locations:

library(shiny)

locations <- data.frame(value=c("Ames", "Beaumont", "Beaumont", "Portland", "Portland"),
                        label=c("IA", "CA", "TX", "ME", "OR"))


ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectizeInput("cityInput", "City", choices=NULL, selected=NULL),
      selectizeInput("locationInput", "Location", choices=NULL, selected=NULL)
    ),
    mainPanel("Main Panel")
  )
)

server <- function(input, output, session) {

  updateSelectizeInput(session, 'cityInput',
                       choices = locations$value,
                       server = TRUE
  )
  updateSelectizeInput(session, 'locationInput',
                       choices = locations,
                       server = TRUE
  )
}

shinyApp(ui, server)
SeGa
  • 9,454
  • 3
  • 31
  • 70
  • Thank you but your answer does not address my issue. Your answer can be replicated in the original code by setting choices to locations$City and location$State in the two calls to updateSelectizeInput. In fact, it can be done entirely on the client side by setting choices to locations$City and locations$State in the calls to selectizeInput and dispensing with the server side updates. What I am aiming for is a single select box with each row having two columns, City and State. – Lee Jun 25 '18 at 11:09
  • Ok, I misinterpreted your question. I just edited my answer with a different approach. As you see, when you just add the data.frame to choices, the choices will be empty, as it doesnt know what to access. – SeGa Jun 25 '18 at 11:35
  • 1
    That may be the case, but it's puzzling because this documentation suggests otherwise [https://shiny.rstudio.com/articles/selectize.html]. Specifically, the section on Server-side selectize says "Here data can be an arbitrary R data object, such as a (named) character vector, or a data frame." – Lee Jun 25 '18 at 13:39
  • Just edited my answer. I found something useful [here](https://groups.google.com/forum/#!topic/shiny-discuss/2LI2EOaM8MM). The colnames have to be **value / label** – SeGa Jun 25 '18 at 14:02