【问题标题】:Startup warning with reactive input in shiny module闪亮模块中带有反应输入的启动警告
【发布时间】:2020-10-10 00:10:21
【问题描述】:

我目前正在按照{golem} 框架在不同模块中模块化一个闪亮的应用程序。为简单起见,假设我有 3 个主要的闪亮模块:

  • mod_faith_plot:生成给定数据集的散点图(我将使用 faitfhul)。
  • mod_points_select:解耦下拉菜单以选择要绘制的点数。 UI 输入有这个专用模块,因为我想将选择器放在 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)

【问题讨论】:

    标签: r shiny golem


    【解决方案1】:

    您可以使用req() 来确保值可用:

    data <- reactive({
        req(n_points())
        faithful %>%
            head(., n_points())
    })
    

    当值不可用时,调用被静默取消

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-07
      • 2018-09-08
      • 2021-12-06
      • 2020-07-04
      相关资源
      最近更新 更多