App crashing with leaflet map no reactive?

I have a working rshiny app but i think i need to add some functionality using the reactive functions to make it more efficient and stop it from crashing. I created a version of the app that uses census data instead of the data i am using which are traffic counts so i realize the example is somewhat trivial but i have found it breaks the same when selecting different states in the first filter. The things i am trying to maintain here is dynamic and cascading filters where one filter informs the latter which all seem to work fine but when i added the leaflet map Rstudio crashes at inconsistent times with no warnings. At times i can make multiple selections in the first filter with no propblem then out of nowhere it bombs. I suspect it has something to do with my lack of observe() or reactive() functions and that too much is going on. I have rewatched Joe's video on Ractive/observe and i can;t seem to figure a solution. Thanks for any help.

#Load Libraries

library(shiny)    
library(ggplot2)
library(plotly)
library(dplyr)
library(shinyWidgets)
library(tigris)
library(leaflet)





# Load data- USe census so others can help
  State_01_Tracts_Sp <- tracts("01")
  State_02_Tracts_Sp <- tracts("02")
  State_04_Tracts_Sp <- tracts("04")
  State_05_Tracts_Sp <- tracts("05")
  State_06_Tracts_Sp <- tracts("06")
  Tracts_Sp <- rbind(State_01_Tracts_Sp ,State_02_Tracts_Sp, State_04_Tracts_Sp, State_05_Tracts_Sp , State_06_Tracts_Sp  )
  

  #Decode fips into descriptive state and county names
  Tracts_Sp@data$State <- fips_codes$state_name[match(Tracts_Sp@data$STATEFP,fips_codes$state_code)]
  Tracts_Sp@data$County <- fips_codes$county[match(Tracts_Sp@data$COUNTYFP,fips_codes$county_code)]
  #Create a copy of the spatial data's data frame
  Data.. <- Tracts_Sp@data



#Set up User Interface
ui <- fluidPage(
  titlePanel("Census Viewer Test"),
  tabsetPanel(
    #Daily Counts Panel
    ##############
    #Hourly Counts Panel
    #######################
    tabPanel("Tab 1",
             #Call plot 
             fluidRow(column(width = 12,plotlyOutput("county_plot" ))),
             #Location Details 
             fluidRow( 
               column(3,
                      h4("Select Details"),
                      uiOutput("State_selector"),
                      uiOutput("County_selector"),
                      uiOutput("Tract_selector")),
               column(6,
                      #h4("Selected Location"),
                      leafletOutput("map_plot",height = 500))
               #Close row
             )
             #Close panel
    )
    #Close setPanel
  )
  #PAge end   
)

#Set up Server
#---------------------------
server <- shinyServer(function(session,input,output){
  #Temporal Details
  ##################
  #State
  output$State_selector <- renderUI({
    selectInput(inputId = "State",
                label = "State", multiple = FALSE,
                choices = c( unique(Data..$State)),
                selected =  unique(Data..$State)[1])
  })
  #County selection----
  output$County_selector <- renderUI({
    available0 <- as.character(unique(Data..$County[Data..$State %in% input$State ] ))
    pickerInput(inputId = "County", label = "Select County", choices = as.character(unique(available0)), 
                options = list(`actions-box` = TRUE, size = 10,`selected-text-format` = "count > 3"), multiple = TRUE,selected = as.character(unique(available0)))
  })
  #Tract selection----
  output$Tract_selector <- renderUI({
    available1 <- as.character(unique(Data..$GEOID[Data..$State %in% input$State & Data..$County %in% input$County] ))
    pickerInput(inputId = "Tract", label = "Select Tract", choices = as.character(unique(available1)), 
                options = list(`actions-box` = TRUE, size = 10,`selected-text-format` = "count > 3"), multiple = TRUE,selected = as.character(unique(available1)))
  })
 
  #Graphics
  ##########################
  #Select final data and chart-----
  output$county_plot <- renderPlotly({
    #Select data
    dat <- Data..[Data..$State%in%input$State & Data..$County%in%input$County & Data..$GEOID%in%input$Tract ,]
    #Set up axis parameters depending on amount of data 
    angle = 90
    
    #Initialze chart
    ggplotly(ggplot(data = dat, x=GEOID, y = ALAND, fill = State) +
               geom_bar(aes(x=GEOID, y = ALAND, fill = State),color = "black", position = "dodge", stat = "identity")+
                ggtitle(paste("Land Area of Select Counties ",unique(dat$State),sep="")) + 
               #Center plot
               theme(plot.title = element_text(hjust = 0.5)) +
               ylab("Land Area") +
               xlab("") +
               guides(color=guide_legend("State")) +
               theme(axis.text.x = element_text(angle = angle, hjust = 1),plot.background = element_rect(fill = "darkseagreen"))) %>% layout(dragmode = "select") 
    
  })
  #Select final data and map-----
  output$map_plot <- renderLeaflet({
    #Select data
    Map_Data_Sp <- Tracts_Sp[Tracts_Sp@data$State%in%input$State & Tracts_Sp@data$County%in%input$County,]
   
    #Create map 
    #Map <- mapview(Map_Data_Sp, map.types = "OpenStreetMap", legend = FALSE, col.regions = "red",color = "black",cex = 10)
    #Map@map
    Map_Data_Sp %>% 
        leaflet(width = "100%") %>%
      addProviderTiles(provider = "CartoDB.Positron") %>%
      addPolygons() 
    
    #Close map
  })
  
  
})
#Run App
shinyApp(ui,server)

I tend to render ui items only once, and then use update or proxy to update them, usually with observeEvent, which reacts only to specific things. Although that might be tricky with ggplotly.

Also, your data is enormous, I'd recommend downloading it offline and then tidying it and saving it in a lighter format for the app (e.g., rds or feather). Edit: Actually the data is not too big, but it is slow to read in.

Also suggest you start the app without everything selected, as this is a lot of data.

When I run this example, the leaflet doesn't update properly. Try leafletProxy.

1 Like

Hi woodward, thanks for the reply. do you know of a good example out there that does the dynamic (based data defined attributes) cascading filters that are properly using either observe/observeEvent or reactive()?

I am having trouble understanding where to place these commands and the examples i have found all seem to use some defined vector of values.

I can also convert to feather if there not any obvious downsides. The census data is used in the example to make it easy on others to help me but my actual data are traffic counts and one of the file is about 290 mb and feather appears to compress that to about 120 so that might offer some performance improvement and i will incorporate.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.