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

Return a rendered leaflet map from leafletProxy() #59

Closed
StevenMMortimer opened this issue Jul 14, 2018 · 3 comments
Closed

Return a rendered leaflet map from leafletProxy() #59

StevenMMortimer opened this issue Jul 14, 2018 · 3 comments

Comments

@StevenMMortimer
Copy link

This is a random question but seems related to how the package is grabbing rendered widgets and updating them. Is it possible to retrieve a rendered leaflet map? For example, I want to use code like manipulateWidget(leafletProxy("objectId")) to return the same class of object as you get when a leaflet map is first created with leaflet(). It seems simple, but is this possible?

Here is a more complete example:

# map created and saved 
m1 <- leaflet() %>% addTiles()

# map rendered
output$mymap <- renderLeaflet({
  leaflet() %>% addTiles()
})
# and then retrieved
m2 <- manipulateWidget(leafletProxy("mymap"))

# these are identical objects
identical(m1, m2)
@FrancoisGuillem
Copy link
Contributor

Hello,

Can you explain why do you want to do this? Using a manipulateWidget inside a shiny app is not documented for now, but you can find some examples here: https://github.com/rte-antares-rpackage/manipulateWidget/tree/master/inst/examples .

With manipulateWidget, you can add modules to a shiny application. The modules can use reactive values from the application but they are not intended to modify other elements of the application.

@StevenMMortimer
Copy link
Author

Thanks for the quick reply @FrancoisGuillem! Maybe I don't understand the aim of manipulateWidget well enough, but I thought it returned already rendered objects and let you modify them. The reason why I asked the question was that in a Shiny app you can create a leaflet map using leaflet() and you can further add to that using leafletProxy() which pushes changes to the rendered map. Unfortunately, you cannot get the map object back in order to save it. I've posed this as a question on StackOverflow after finding that many other R users are trying to figure out a solution to this problem because the typical ways of saving a leaflet map don't work with a proxy object. It needs to be an object of class leaflet, htmlwidget. I thought this package might return the actual object in some way.

Right now there are two workaround solutions to this problem:

  1. Build and render the map object, then keep a second object in a parallel with the same updates to the rendered map, then download the second object when needed.
  2. On click, grab all the rendered HTML from the map element and throw into an HTML file.

Neither solution seems to be ideal, so thought it would be worth posing the question more broadly, but this probably isn't the right place. I'll close this issue now.

@FrancoisGuillem
Copy link
Contributor

I don't know if it can helps, but with manipulateWidget you can generate a leaflet map, update it and save it into an html file:

image

Here is the code to generate it:

myMapFun <- function(radius, color, initial, session, output) {
    if (initial) {
      # Widget has not been rendered
      map <- leaflet() %>% addTiles()
    } else {
      # widget has already been rendered
      map <- leafletProxy(output, session) %>% clearMarkers()
    }

    map %>% addCircleMarkers(lon, lat, radius = radius, color = color)
  }

  manipulateWidget(myMapFun(radius, color, .initial, .session, .output),
                   radius = mwSlider(5, 30, 10),
                   color = mwSelect(c("red", "blue", "green")))

This looks more or less like your first solution: when the user clicks the save button, a new htmlWidget object is created using the current values of the inputs.

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

2 participants