2

I am currently modularizing a Shiny app in different modules following the {golem} framework. For simplicity, let's say I have 3 main shiny modules:

  • mod_faith_plot: generates a scatterplot of a given dataset (I'll use faitfhul).
  • mod_points_select: decouples a dropdown menu to select how many points will be plotted. UI inputs have this dedicated module as I wanted to place the selector in the sidebarPanel instead of mainPanel (alongside the plot).
  • mod_data: provides a reactive dataframe depending on the n_points argument.

This modules talk to each other in the server function. Now, when I start my app with a simple head(., n_points()) in mod_data I get the following warning:

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

The input in mod_points_select is clearly NULL before the selected_points argument gets assigned, is there a less hacky and more elegant way to avoid the warning at startup than my if condition?

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)

anddt
  • 1,134
  • 1
  • 5
  • 16

1 Answers1

2

You can use req() to ensure values are available:

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

When values are not available the call is silently canceled

HubertL
  • 17,371
  • 2
  • 20
  • 40