闪亮模块中带有反应输入的启动警告
Startup warning with reactive input in shiny module
我目前正在按照 {golem}
框架在不同模块中模块化 Shiny 应用程序。为简单起见,假设我有 3 个主要的闪亮模块:
mod_faith_plot
:生成给定数据集的散点图(我将使用 faitfhul)。
mod_points_select
:将下拉菜单与 select 将绘制的点数分离。 UI 输入有这个专用模块,因为我想将 selector 放在 sidebarPanel
而不是 mainPanel
中(在图旁边)。
mod_data
:根据 n_points
参数提供响应式数据框。
这些模块在 server
函数中互相交谈。
现在,当我在 mod_data
中使用简单的 head(., n_points())
启动我的应用程序时,我收到以下警告:
Warning: Error in checkHT: invalid 'n' - must contain at least one non-missing element, got none.
mod_points_select
中的输入在 selected_points
参数被赋值之前显然是 NULL
,有没有比我的 if条件?
library(shiny)
library(dplyr)
library(ggplot2)
# [Module] Plot faithful data -------------------------------------------------------
mod_faith_plot_ui <- function(id){
ns <- NS(id)
tagList(
plotOutput(ns("faith_plot"))
)
}
mod_faith_plot_server <- function(input, output, session, data){
ns <- session$ns
output$faith_plot <- renderPlot({
data() %>%
ggplot(aes(eruptions, waiting)) +
geom_point()
})
}
# [Module] Module for n_points dropdown ---------------------------------------------
mod_points_select_ui <- function(id){
ns <- NS(id)
uiOutput(ns("select_points"))
}
mod_points_select_server <- function(input, output, session){
ns <- session$ns
output$select_points <- renderUI({
selectInput(
ns("n_points"),
label = "Select how many points",
choices = seq(0, 200, by = 10),
selected = 50
)
})
reactive({input$n_points})
}
# [Module] Get filtered data -----------------------------------------------------------------
mod_data_server <- function(input, output, session, n_points){
ns <- session$ns
data <- reactive({
faithful %>%
# If condition used to avoid warnings at startup - switch lines to get warning
# head(., n_points())
head(., if(is.null(n_points())) { TRUE } else {n_points()})
})
}
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
mod_points_select_ui(id = "selected_points")
),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("plot", mod_faith_plot_ui(id = "faith_plot"))
)
)
)
)
server <- function(input, output, session) {
data <- callModule(mod_data_server, id = "data", n_points = selected_points)
selected_points <- callModule(mod_points_select_server, id = "selected_points")
callModule(mod_faith_plot_server, id = "faith_plot", data = data)
}
shinyApp(ui, server)
您可以使用 req()
来确保值可用:
data <- reactive({
req(n_points())
faithful %>%
head(., n_points())
})
当值不可用时,调用会被静默取消
我目前正在按照 {golem}
框架在不同模块中模块化 Shiny 应用程序。为简单起见,假设我有 3 个主要的闪亮模块:
mod_faith_plot
:生成给定数据集的散点图(我将使用 faitfhul)。mod_points_select
:将下拉菜单与 select 将绘制的点数分离。 UI 输入有这个专用模块,因为我想将 selector 放在sidebarPanel
而不是mainPanel
中(在图旁边)。mod_data
:根据n_points
参数提供响应式数据框。
这些模块在 server
函数中互相交谈。
现在,当我在 mod_data
中使用简单的 head(., n_points())
启动我的应用程序时,我收到以下警告:
Warning: Error in checkHT: invalid 'n' - must contain at least one non-missing element, got none.
mod_points_select
中的输入在 selected_points
参数被赋值之前显然是 NULL
,有没有比我的 if条件?
library(shiny)
library(dplyr)
library(ggplot2)
# [Module] Plot faithful data -------------------------------------------------------
mod_faith_plot_ui <- function(id){
ns <- NS(id)
tagList(
plotOutput(ns("faith_plot"))
)
}
mod_faith_plot_server <- function(input, output, session, data){
ns <- session$ns
output$faith_plot <- renderPlot({
data() %>%
ggplot(aes(eruptions, waiting)) +
geom_point()
})
}
# [Module] Module for n_points dropdown ---------------------------------------------
mod_points_select_ui <- function(id){
ns <- NS(id)
uiOutput(ns("select_points"))
}
mod_points_select_server <- function(input, output, session){
ns <- session$ns
output$select_points <- renderUI({
selectInput(
ns("n_points"),
label = "Select how many points",
choices = seq(0, 200, by = 10),
selected = 50
)
})
reactive({input$n_points})
}
# [Module] Get filtered data -----------------------------------------------------------------
mod_data_server <- function(input, output, session, n_points){
ns <- session$ns
data <- reactive({
faithful %>%
# If condition used to avoid warnings at startup - switch lines to get warning
# head(., n_points())
head(., if(is.null(n_points())) { TRUE } else {n_points()})
})
}
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
mod_points_select_ui(id = "selected_points")
),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("plot", mod_faith_plot_ui(id = "faith_plot"))
)
)
)
)
server <- function(input, output, session) {
data <- callModule(mod_data_server, id = "data", n_points = selected_points)
selected_points <- callModule(mod_points_select_server, id = "selected_points")
callModule(mod_faith_plot_server, id = "faith_plot", data = data)
}
shinyApp(ui, server)
您可以使用 req()
来确保值可用:
data <- reactive({
req(n_points())
faithful %>%
head(., n_points())
})
当值不可用时,调用会被静默取消