Shiny module ggplot reactive data error data can't be a function

31 Views Asked by At

I've reduced my code to a single module that works like this

library(shiny)
library(bslib)
library(bcmaps)
library(ggplot2)
library(dplyr)
mod_geography_ui <- function(id){
  sidebarLayout(
    sidebarPanel(
      selectInput(NS(id, "geography"),
                  "Select Geography Level",
                  choices = c("HA" = "hlth_authority_name",
                              "HSDA" = "hlth_service_dlvr_area_name",
                              "LHA" = "local_hlth_area_name",
                              "CHSA" = "cmnty_hlth_serv_area_name"),
                  selected = "HA",
                  width = '100%'),
      width = 3),
    mainPanel(
      tableOutput(NS(id, "bcmaps")),
      plotOutput(NS(id, "plot")),
      width = 9)
  )
}

mod_geography_server <- function(id){
  moduleServer(id, function(input, output, session){
    bcmaps_data <- reactive({
      if (input$geography == "hlth_authority_name"){
        bcmaps_data <- bcmaps::health_ha() %>%
          rename_with(tolower, everything()) %>%
          select(hlth_authority_name, geometry)
      }
      if (input$geography == "hlth_service_dlvr_area_name"){
        bcmaps_data <- bcmaps::health_hsda() %>%
          rename_with(tolower, everything()) %>%
          select(hlth_service_dlvr_area_name, geometry)
      }
      if (input$geography == "local_hlth_area_name"){
        bcmaps_data <- bcmaps::health_lha() %>%
          rename_with(tolower, everything()) %>%
          select(local_hlth_area_name, geometry)
      }
      if (input$geography == "cmnty_hlth_serv_area_name"){
        bcmaps_data <- bcmaps::health_chsa() %>%
          rename_with(tolower, everything()) %>%
          select(cmnty_hlth_serv_area_name, geometry)
      }
      return(bcmaps_data)
    })

    output$bcmaps <- renderTable({
      data <- bcmaps_data() %>% 
        dplyr::select(1)
    })
    
    output$plot <- renderPlot({
      ggplot(data = bcmaps_data()) +
        geom_sf(aes(fill = .data[[input$geography]], geometry = geometry))
        
    })
  })
}

app_ui <- function(request) {
  fluidPage(
    mod_geography_ui("geography_1")
  )
}

app_server <- function(input, output, session) {
  # Your application server logic
  mod_geography_server("geography_1")
}

shiny::shinyApp(ui = app_ui, server = app_server)

This runs generally fine and produces a map (the list issue is part of my troubleshooting which isn't working.

The problem I have is where I'm deploying I can't draw from the bc maps package, so I created the data locally by using

library(dplyr)

health_ha <- bcmaps::health_ha() %>%
  rename_with(tolower, everything()) %>%
  select(hlth_authority_name, geometry)

usethis::use_data(health_ha, overwrite = TRUE)

With each of the 4 datasets being made. I'm producing my shiny dashboard within a golem framework and the data is there.

However when I change my bcmaps reactive within my actual app to

bcmaps_data <- reactive({
      if (input$geography == "hlth_authority_name"){
        bcmaps_data <- health_ha
      } else if (input$geography == "hlth_service_dlvr_area_name"){
        bcmaps_data <- health_hsda
      } else if (input$geography == "local_hlth_area_name"){
        bcmaps_data <- health_lha
      } else if (input$geography == "cmnty_hlth_serv_area_name"){
        bcmaps_data <- health_chsa
      }
      return(bcmaps_data)
    })

My code breaks and I get this error:

Warning: Error in ggplot: `data` cannot be a function.
ℹ Have you misspelled the `data` argument in `ggplot()`
  177: <Anonymous>
  176: signalCondition
  175: signal_abort
  174: rlang::abort
  173: cli::cli_abort
  172: ggplot.function
  170: renderPlot [C:/Projects/testapp/R/mod_geography.R#67]
  168: func
  128: drawPlot
  114: <reactive:plotObj>
   98: drawReactive
   85: renderFunc
   84: output$geography_1-plot
    3: runApp
    2: print.shiny.appobj
    1: <Anonymous>
Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.

I'm really struggling how to troubleshoot and identify the issue. Originally I wanted to left_join the bcmaps data with another cohort dataset, and was having issues with that. Trying to just get the individual pieces working properly, but that is proving an issue.

Any help/advice would be greatly appreciated.

0

There are 0 best solutions below