Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing spinner when leafletOutput is updated through leafletProxy #17

Closed
aoles opened this issue Jun 8, 2018 · 12 comments
Closed

Missing spinner when leafletOutput is updated through leafletProxy #17

aoles opened this issue Jun 8, 2018 · 12 comments

Comments

@aoles
Copy link

aoles commented Jun 8, 2018

Hi,
the package works great with renderLeaflet, thanks a lot! However, the spinner doesn't show up when updating the map with leafletProxy, as illustrated by the following MWE. Do you have any advice on how to overlay a loader animation over the map while it is being updated by a call to leafletProxy?

Cheers,
Andrzej

library(shiny)
library(shinycssloaders)
library(leaflet)

ui <- fluidPage(
  withSpinner(leafletOutput("map")),
  p(),
  actionButton("recalc", "New points")
)

server <- function(input, output, session) {

  points <- eventReactive(input$recalc, {
    Sys.sleep(1)
    cbind(rnorm(40) * 2 + 13, rnorm(40) + 48)
  }, ignoreNULL = FALSE)

  output$map <- renderLeaflet({
    leaflet() %>%  setView(13, 48, 6) %>%
      addProviderTiles(providers$Stamen.TonerLite)
  })

  observe({
    leafletProxy("map") %>%
      clearMarkers() %>%
      addMarkers(data = points())
  })
}

shinyApp(ui, server)
@Mikea0228
Copy link

I also have this exact same issue.

@eroten
Copy link

eroten commented Jul 7, 2018

I am also having this issue.

@charlie-becker
Copy link

Also seeking a solution.

@elhohn
Copy link

elhohn commented Nov 29, 2018

Same.

@richardtc
Copy link

Also interested.

@J-O-H-N-P-A-U-L
Copy link

also interested

@ktsperry
Copy link

ktsperry commented Feb 5, 2019

I am also interested.

@eroten
Copy link

eroten commented Mar 19, 2019

Has anyone come up with a workaround?

@kimberlycl
Copy link

I used the shinyWidgets package in my shiny app. For this example I added the line shinyjs::useShinyjs() in the ui as well as a fluidRow to add a div that loads a spinner (you probably want to change the layout so it appears over the map but I'm not sure how to do that). Then I added the line shinyjs::showElement(id = 'loading') inside the observe function and before the leafletProxy call, and shinyjs::hideElement(id = 'loading') after the leafletProxy call at the end of the observe function. The spinner now shows up while the map is loading. Hope this helps!

`library(shiny)
library(shinycssloaders)
library(leaflet)
library(shinyWidgets)

ui <- fluidPage(
shinyjs::useShinyjs(),
leafletOutput("map"),
p(),
actionButton("recalc", "New points"),
p(),
fluidRow(column(3, shinyjs::hidden(div(id = 'loading', addSpinner(div(), spin = "circle", color = "blue")))
))
)

server <- function(input, output, session) {

points <- eventReactive(input$recalc, {
Sys.sleep(1)
cbind(rnorm(40) * 2 + 13, rnorm(40) + 48)
}, ignoreNULL = FALSE)

output$map <- renderLeaflet({
leaflet() %>% setView(13, 48, 6) %>%
addProviderTiles(providers$Stamen.TonerLite)
})

observe({
shinyjs::showElement(id = 'loading')

leafletProxy("map") %>%
  clearMarkers() %>%
  addMarkers(data = points())

shinyjs::hideElement(id = 'loading')

})
}

shinyApp(ui, server)`

@ngfrey
Copy link

ngfrey commented Oct 3, 2019

The solution above is great, except it doesn't work for larger shape renderings. For example, a map of US county polygons with some aesthetic mappings will not be rendered on the screen before the spinner is removed. If anyone has a workaround, it would be wonderful to see how to work around this problem.

@daattali
Copy link
Owner

daattali commented May 3, 2020

When I tried running the example given, I wasn't even getting a loader to show up initially on the map. It looks like any output with the name "map" was not working #49. This is now fixed

The other issue here is not seeing the loader when using the leaflet proxy. Without looking into this, I'm pretty sure it's happening because the actual plot object isn't getting recalculated. Any widget that uses a proxy to update it (leaflet, plotly, whatever else supports updating without a complete re-render) doesn't trigger a redraw from shiny, so shinycssloaders doesn't think it's recalculating.

I don't know enough and haven't looked into how the leafetlet proxy works, but unless there's a signal that shiny knows the specific map is being re-plotted, I don't think this will be solved with shinycssloaders.

@daattali
Copy link
Owner

Unfortunately I believe this is not an issue that can be solved with shinycssloaders. The problem is that shiny itself isn't sending a message saying that the plot is recalculating. There are specific javascript events that shiny usually fires when an output recalculates, namely outputinvalidated, but it's not getting fired when an output is modified via a proxy. If shiny is ever able to catach these proxy events and fire them, then shinycssloaders will automatically work. Until then, the work around is to manually show and hide a spinner, for example usiny shinyjs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests