I am using a future promise to call my data more efficiently. However, I'm finding that my filters (called through selecticizeGroupServer) do not work with the future promise.
See attached for a minimal reproducible example. The filters work as expected when I remove the "future_promise" function from the data reactive expression. However, when I use "future_promise," the filter module fails.
Could you please help me figure out what I'm doing wrong?
library(shiny)
library(future)
library(promises)
library(shinyWidgets)
plan(multisession)
ui <- fluidPage(
titlePanel("Example App"),
sidebarLayout(
sidebarPanel(),
mainPanel(
selectizeGroupUI(
id = "filters",
inline = FALSE,
params = list(
`mpg` = list(inputId = "mpg", title = "mpg", placeholder = "All"),
`cyl` = list(inputId = "cyl", title = "cyl", placeholder = "All"),
`disp` = list(inputId = "disp", title = "disp", placeholder = "All")
)
)
)
)
)
server <- function(input, output) {
data <- reactive({
future_promise({
mtcars
})
})
filter_mod <- reactive({})
observe({
filter_mod <<- callModule(
module = selectizeGroupServer,
id = "filters",
inline = FALSE,
data = data,
vars = c("mpg", "cyl", "disp")
)
})
}
# Run the application
shinyApp(ui = ui, server = server)
We can use a initialized reactiveVal
instead of reactive
to avoid passing a promise to selectizeGroupServer
.
Please check the following:
library(shiny)
library(future)
library(promises)
library(shinyWidgets)
plan(multisession)
ui <- fluidPage(
titlePanel("Example App"),
sidebarLayout(
sidebarPanel("The choices will appear after 5 seconds:"),
mainPanel(
selectizeGroupUI(
id = "filters",
inline = FALSE,
params = list(
`mpg` = list(inputId = "mpg", title = "mpg", placeholder = "All"),
`cyl` = list(inputId = "cyl", title = "cyl", placeholder = "All"),
`disp` = list(inputId = "disp", title = "disp", placeholder = "All")
)
)
)
)
)
server <- function(input, output, session) {
data <- reactiveVal(NULL)
future_promise({
Sys.sleep(5) # long running task
mtcars
}) %...>% data() # assign to reactiveVal "data" once the future_promise is resolved
filter_mod <- callModule(
module = selectizeGroupServer,
id = "filters",
inline = FALSE,
data = data,
vars = c("mpg", "cyl", "disp")
)
}
shinyApp(ui = ui, server = server)