r - Shiny 模块中带有反应输入的启动警告

标签 r shiny golem

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

  • mod_faith_plot:生成给定数据集的散点图(我将使用 faitfhul)。
  • mod_points_select:分离下拉菜单以选择要绘制的点数量。 UI 输入有这个专用模块,因为我想将选择器放置在 sidebarPanel 中,而不是 mainPanel (沿着绘图)。
  • mod_data:根据 n_points 参数提供响应式(Reactive)数据帧。

这些模块在服务器功能中相互通信。 现在,当我在 mod_data 中使用简单的 head(., n_points()) 启动应用程序时,我收到以下警告:

Warning: Error in checkHT: invalid 'n' -  must contain at least one non-missing element, got none.

selected_points参数被分配之前,mod_points_select中的输入显然是NULL,是否有一种不那么hacky和更优雅的方法来避免启动时发出警告,而不是我的 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())
})

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

关于r - Shiny 模块中带有反应输入的启动警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62475336/

相关文章:

r - 测试模块化 R Shiny (golem) 仪表板

r - 如何在 golem Shiny 应用程序中包含 R6 对象以跨模块共享数据

r - 计算多列中 "Y"和 "N"的百分比?

javascript - 将外部工具提示 JS 库与 networkD3 和 Shiny 结合使用

r - 我可以在 Shiny 的 onSessionEnded 事件中访问 session 值吗?

r - 如何使下载按钮在 R Shiny 的 golem 模块中工作

r - 数据框的列数表

r - ggplot2 绘制黑色轮廓

r - 如何在 R 中绘制具有 3 个因子的响应变量?

r - 如果检查输入,是否需要参数化 SQL 搜索?