I am having trouble getting a table to pre-populate when it is encased within an eventReactive()
command using eventReactive(..., ignoreNULL = F)
. The complications are:
- my shiny app is using modules
- a filtering step is being used within
eventReactive()
using a variable from a staticselectInput()
and a variable from a dynamically-generatedselectInput()
The first variable will filter as expected, the second will not. The second variable is not detected as selected. My intent is when this app is initially launched, the table is generated using default values for the variables.
Here is a reprex:
### global conditions ####
library(dplyr); library(shiny); library(shinydashboard); library(DT)
dframe = data.frame(var1 = rep(c("AAA", "BBB", "CCC"), 10),
var2 = sample(c("orange", "pink"), 30, replace = T),
var3 = sample(1:100, 30))
### modular functions ####
extra_UI <- function(id) {
ns = NS(id)
tagList(selectInput(ns("VAR1"),
label = "select a variable",
choices = unique(dframe$var1),
selected = NULL),
uiOutput(ns("conditional_menu")),
uiOutput(ns("test_text")),
br(), br(),
actionButton(ns("go"), "Update Table"),
br(), br(),
DT::dataTableOutput(ns("table"))
)
}
extra <- function(input, output, session) {
ns <- session$ns
Var1 <- reactive(input$VAR1)
output$conditional_menu <- renderUI({
req(Var1())
colors <- dplyr::filter(dframe, var1 == Var1()) %>% distinct(var2) %>% pull(var2)
selectInput(ns("VAR2"), "Select a second variable", colors)
})
Var2 <- reactive(input$VAR2)
# output selections - both Var1 and Var2 have values assigned to them when app launches
output$test_text <- renderUI({
req(Var1(), Var2())
paste("selected variables: ", Var1(), Var2())
})
# output table = only Var1 registers as having a value when app launches
x <- eventReactive(input$go, {
dplyr::filter(dframe, var1 == Var1(), var2 == Var2())
}, ignoreNULL = FALSE) # table is produced after clicking the go button
output$table <- renderDataTable({ x() })
}
##### normal UI and server functions ####
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
fluidRow(
extra_UI(id = "new_menu")
)
)
)
server <- function(input, output, session) {
callModule(module = extra, id = "new_menu")
}
shinyApp(ui = ui, server = server)
When the app is launched, both Var1 and Var2 will be displayed in a renderText()
command, but the table is not produced. Instead there is this error for the filtering step:
Error: Problem with
filter()
input..2
.
e[31mxe[39m Input..2
must be of size 30 or 1, not size 0.
e[34mℹe[39m Input..2
isvar2 == Var2()
.
Everything displays as intended once the action button is clicked.
Any suggestions on how to make "Var2" visible for the filtering step upon app initialisation? Thanks in advance.
(of course, in this example, there really is not a need to dynamically generate the conditional menu for var2, but it's good proxy for what I am actually trying to do).