I am trying to update two pickerInput’s via an observeEvent, based on the dataframe it produces. However, it is throwing out an error message:
Warning: Error in $: $ operator is invalid for atomic vectors
3: runApp
2: print.shiny.appobj
1: <Anonymous>
I’ve produced an example below. I’ve checked to see if data() is an atomic vector or not, and it seems to be maintained as a dataframe.
l <- NULL
l$name <- c('b','e','d','b','b','d','e')
l$age <- c(20,20,21,21,20,22,22)
l <- as.data.frame(l)
l$name <- as.character(l$name)
l$age <- as.numeric(l$age)
library(shiny)
server <- shinyServer(function(input,output, session){
data <- reactive({
if(length(input$name) > 0 & length(input$age) > 0){
l %>% filter(age %in% input$age,
name %in% input$name)
}else if (length(input$name) > 0 & length(input$age) == 0){
l %>% filter(name %in% input$name)
}else if (length(input$name) == 0 & length(input$age) > 0){
l %>% filter(age %in% input$age)
}
else{
l
}
})
observeEvent(list(
input$name,
input$age
),
ignoreInit = TRUE,
{
updatePickerInput("age","Choose an age",
choices = c(unique(data()$age)))
updatePickerInput("name","Choose a name",
choices = c(unique(data()$name)))
print(str(data()))
print(is.data.frame(data()))
print(is.atomic(data()))
print(is.atomic(data()$name))
print("x")
})
output$table1 <- renderTable({
data()
})
}
)
ui <-shinyUI(fluidPage(
pickerInput("name","Choose a name", choices = c(unique(l$name)),
options = list(`actions-box` = TRUE),
multiple = T),
pickerInput("age","Choose an age", choices = c(unique(l$age)),
options = list(`actions-box` = TRUE),
multiple = T),
tableOutput("table1")
))
shinyApp(ui,server)