trafficonese / leaflet.extras2

Extra functionality for leaflet R package.
https://trafficonese.github.io/leaflet.extras2/
GNU General Public License v3.0
85 stars 19 forks source link

Spinner does not appear in Shiny application #48

Open edwardlavender opened 1 year ago

edwardlavender commented 1 year ago

I have wrapped the example for ?addSpinner into an R Shiny application., but the spinner does not appear.

Here is a reproducible example based on the example given for ?addSpinner. I expected a spinner to appear during the 'sleep' phase, but no spinner appears.

library(shiny)
library(leaflet)
library(leaflet.extras2)
library(magrittr)

ui <- fluidPage(
  leafletOutput("leaf")
)

sleep <- function(map){
  Sys.sleep(5)
  map
}

server <- function(input, output){
  output$leaf <- 
    renderLeaflet({
      leaflet(data = quakes) %>%
        addTiles()  %>%
        addSpinner() %>%
        startSpinner(options = list("lines" = 7, "length" = 20)) %>%
        sleep() %>%
        addMarkers(~long, ~lat, popup = ~as.character(mag), label = ~as.character(mag)) %>%
        stopSpinner()
    })
}

shinyApp(ui, server)
trafficonese commented 1 year ago

I'm pretty sure this is a bug in the upstream repo. There is not much I can do with this plugin. Its either start or stop the spinner. I think it could be related to this issue https://github.com/makinacorpus/Leaflet.Spin/issues/12

trafficonese commented 1 year ago

You could maybe do something like that:

library(shiny)
library(leaflet)
library(leaflet.extras2)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  actionButton("go", "Go"),
  leafletOutput("leaf")
)

server <- function(input, output){
  mapready <- reactiveVal(NULL)
  output$leaf <- renderLeaflet({
    m <- leaflet() %>%
        addTiles() %>% 
        leaflet.extras2::addSpinner() 
    shinyjs::delay(500, mapready(TRUE))
    m
  })
  observe({
    req(mapready())
    leafletProxy("leaf") %>% 
      startSpinner(list("lines" = 7, "length" = 40,
                        "width" = 20, "radius" = 10)) %>%
      clearGroup("quk") %>% 
      addMarkers(data = quakes, ~long, ~lat, group="quk",
                 popup = ~as.character(mag), label = ~as.character(mag)) %>%
      leaflet.extras2::stopSpinner()
  })
}

shinyApp(ui, server)
edwardlavender commented 1 year ago

That's great - thanks a lot!

radbasa commented 4 months ago

@trafficonese I'll take a look at this and see if we can make it work like it did when this was merged.

trafficonese commented 4 months ago

@radbasa I'm not sure what you mean. :) Did you maybe write in a wrong issue?

radbasa commented 3 months ago

@radbasa I'm not sure what you mean. :) Did you maybe write in a wrong issue?

I'll take a look at the spinner again, and see if the "bug" mentioned above can be fixed.