Problem
I have a Shiny app in which I would like a selectInput
to only take the unique values that are in the data. In the below example, even though Man
is a possible value in the real world, it is not in the data, so I want the dropdown values to only include Woman
and Child
. When I run the below minimum working example, I recieve this error:
Warning: Error in : `x` must be 'data.frame', not 'environment'.
47: hypothesize_fn
46: predicate
44: check_type
43: calculate
41: observe
40: server [#2]
3: runApp
2: print.shiny.appobj
1: <Anonymous>
Error : `x` must be 'data.frame', not 'environment'.
This error persists on three different machines (one Windows, Mac, and Linux), as well as when I deploy to shinyapps.io
(it is present in the logs). I’ve also run the example code found here and here, and get errors, although different ones. It has worked before, but it started breaking a couple nights ago, and reverting back to a previously working version doesn’t fix it.
The reason I cant just pass sort(unique(data$cat1))
into the selectInput()
directly is because in my full working project, ui.R
and server.R
are two different files, and the data is only loaded into server.R
. Thanks, all.
Minimum working example
library(bslib)
library(shiny)
library(tidyverse)
data <- tibble("cat1" = c("Woman", "Woman", "Child"), "cat2" = c("b", "c", "c"))
ui <- page_navbar(
sidebar = sidebar(
selectInput(
"filter_category",
"Categories:",
choices = NULL,
selected = NULL,
multiple = TRUE
)
)
)
server <- function(input, output, session) {
observe({
updateSelectInput(
session,
"filter_letter",
choices = sort(unique(data$cat1)),
selected = sort(unique(data$cat1))
)
})
}
shinyApp(ui = ui, server = server)
Possibly related question here, but it hasn’t helped so far.
I have tried:
- Reverting to a previously-functional version. The error persists now.
- Removing the
observe()
function and returning to staticselectInput
choices (this works but itsn’t the functionality I want). - Switching between a Mac, Windows, and Linux machine.
- Deploying to
shinyapps.io
and using whatever environment they use there. - Manually setting
choices
andselected
with static vectors inside of theupdateSelectInput
function.
I was expecting:
A dropdown selectInput
in my UI and only contains the levels of the cat1
categorical variable that are present in the data.