mustafa00
mustafa00

Reputation: 881

How to read and show data on button click using eventReactive?

What I need to do is read and show geoJSON data only when button is clicked. My original data is quite heavy and there's no need to load it when app starts - only when particular button is clicked. Honestly, I did it with observeEvent and it works. I just wanted to know how to do this with eventReactive which I think would be better in this place.

My properly working code is below. Coommented code is my try to use eventReactive - and it also works (quite) fine but.. when I run code the basemap doesn't show up, it shows up along with choropleth only when I click the button.

library(shiny)
library(leaflet)
library(dplyr)
library(sf)


ui <- fluidPage(
  actionButton("btn", label = "Choropleths"),
  leaflet::leafletOutput("map") 
)

server <- function(input, output, session) {
  
  observeEvent(input$btn, {
    dataChoro <- sf::st_read(dsn = "https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json")

    leaflet::leafletProxy("map") %>%
      leaflet::addPolygons(
        data = dataChoro,
        group = "raw"
      )
   })
  
  # dataChoro <- eventReactive(input$btn, {
  #   sf::st_read(dsn = "https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json")
  # })  
        
  output$map <- leaflet::renderLeaflet({
    leaflet::leaflet() %>%
      leaflet::addTiles() %>%
      leaflet::setView(-99.659006, 40.011318, zoom = 4) #%>% 
      # leaflet::addPolygons(
      #   data = dataChoro(),
      #   group = "raw"
      # )
  })
}

shinyApp(ui, server)

Basically, in case I want to read & show given dataset 'on demand' - which approach is better: eventReactive or observeEvent?

Upvotes: 0

Views: 363

Answers (1)

Tomas Capretto
Tomas Capretto

Reputation: 741

Maybe something like the following? You basically store the data in a reactive object, and then listen on that reactive object.

  dataChoro <- eventReactive(input$btn, {
    sf::st_read(dsn = "https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json")
  })
  observeEvent(dataChoro(), {
    leaflet::leafletProxy("map") %>%
      leaflet::addPolygons(
        data = dataChoro(),
        group = "raw"
      )
  })
  output$map <- leaflet::renderLeaflet({
    leaflet::leaflet() %>%
      leaflet::addTiles() %>%
      leaflet::setView(-99.659006, 40.011318, zoom = 4)
  })

As a note, keep in mind that the data is going to be downloaded every time you click on the button. You may want to set a flag to prevent that within the eventReactive() flag.

Something like the following will work

  data_downloaded = FALSE
  dataChoro <- eventReactive(input$btn, {
    if (data_downloaded) {
      req(FALSE)
    }
    data = sf::st_read(dsn = "https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json")
    data_downloaded <<- TRUE
    data
  })

Upvotes: 1

Related Questions