如何将 selecticizeGroupInput 与 future_promise 一起使用?
How can you use selecticizeGroupInput with a future_promise?
我正在使用未来的承诺来更有效地调用我的数据。但是,我发现我的过滤器(通过 selecticizeGroupServer 调用)不适用于未来的承诺。
请参阅附件中的最小可重现示例。当我从数据反应表达式中删除“future_promise”函数时,过滤器按预期工作。但是,当我使用“future_promise”时,过滤器模块会失败。
你能帮我找出我做错了什么吗?
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)
我们可以使用初始化的 reactiveVal
而不是 reactive
来避免将承诺传递给 selectizeGroupServer
。
请检查以下内容:
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)
我正在使用未来的承诺来更有效地调用我的数据。但是,我发现我的过滤器(通过 selecticizeGroupServer 调用)不适用于未来的承诺。
请参阅附件中的最小可重现示例。当我从数据反应表达式中删除“future_promise”函数时,过滤器按预期工作。但是,当我使用“future_promise”时,过滤器模块会失败。
你能帮我找出我做错了什么吗?
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)
我们可以使用初始化的 reactiveVal
而不是 reactive
来避免将承诺传递给 selectizeGroupServer
。
请检查以下内容:
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)