-
Notifications
You must be signed in to change notification settings - Fork 505
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
How to Recolor An Already Existing Polygon #496
Comments
@jrisi256 are you open to incorporating the In In |
I am looking at your code now, and my only concern is the map I am creating is much larger than the one you are creating. For reference, I am using the shape files as detailed below, and I have about 13 outcome variables (and I am going to want to add more). And each time I add another set of Polygons based on the county shape files, I calculate it adds 3.65 Mb to the total size of the map. For my 13 outcomes, that comes out to over 50 Mb, and I anticipate that this would cause Leaflet/Shiny to slow down quite a bit. So I'm curious if there is a more 'elegant' solution versus having to copy/paste a bunch of code and wasting a lot of computational resources rendering basically the same polygons over and over again.
|
@jrisi256, this is possible but not through the currently provided R interface. I am waiting on response on #496 (reference) to make sure I am not missing another implementation. I will post an example if this will not be added to the API in the development version. |
@timelyportfolio Thank you so much! I eagerly await your example. |
@jrisi256: for speed, you might want to consider exporting your final spatial polygon data frame (or your entire leaflet map for that matter) as an .rds file in a separate R script, and then import that file into your Kyle Walker does this with his neighborhood diversity Shiny app. You can see his GitHub repo here. |
@cenuno Thank you! I'll have to check that out if this current line of inquiry doesn't work. |
@timelyportfolio Hello! Not to be a bother, but do you think my best bet is to build the map ahead of time as cenuno suggested? |
I don't want to rush anybody, but I'm just curious if #598 is scheduled to be merge soon? I have to work with massive shapefiles of thousands of complex polygons and I will have to consider another R solution than leaflet if this issue is not addressed soon. Thanks for your wonderful work! 😄 |
Yeah, this would be so very useful |
@PetraOleum @jplecavalier @jrisi256 I'm not sure why @edwindj #598 has not been accepted or discussed. In my mind his changes should work perfectly. I had considered this solved. Here is how to use it without the pull request. We'll need to add the JavaScript All at Once - Ugly and Intimidating
Cleaner and Maybe Less ScaryThe code looks like a lot since we have to manually add the JavaScript and R functions. I'll do a slightly different example below where we use Add the R + JS from pull 598
Shiny appThen I think the code becomes much less intimidating (run above first).
|
You're a lifesaver (or at least a data-and-timesaver). Thanks a bunch! Hopefully the changes will be merged in a future version |
This is awesome. I will try to use it to update labels too. This is how I did it: Javascript:
R function:
|
This is indeed wonderful. One quick question that I have regarding this solution, is it possible to also update the label of shapes? |
And also is it possible to the popup content also through leaflet proxy? |
Hi, I'm having an issue while adapting @edwindj setShapesStyle function to my own situation. This is the piece of code of the server function where I'm having some problems:
I came up with this based on the example given by @timelyportfolio. but in my situation I'm assigning to fillColor a variable (colorpal) which is calculated inside a reactive expression by the function ind_UF(). This function basically returns a vector of colors with the same size of my shapefile (uf_shp) and take as arguments the inputs given by my controls. When I run the app the polygons do not change their colors as I modify my inputs. I've already tried using addPolygons instead of setShapeStyle, whithin the observer, using the same reactive arrange shown above and had the expected results. I was wondering if someone had issues similar to this. Thanks! Edit 1: I've tested my shapefile (uf_shp) in the simple example provided by @timelyportfolio and had good results. |
Have you solved this? Thanks. |
Updating @martinzuba's post and addressing @DavZim's question: I have extended this to update labels in the same way. JS function to include in your app window.LeafletWidget.methods.setLabel = function(category, layerId, label){
var map = this;
if (!layerId){
return;
} else if (!(typeof(layerId) === "object" && layerId.length)){ // in case a single layerid is given
layerId = [layerId];
}
layerId.forEach(function(d,i){
var layer = map.layerManager.getLayer(category, d);
if (layer){ // or should this raise an error?
layer.unbindTooltip();
// the object subsetting to get the integer array and casting to string is what I added
layer.bindTooltip(label.label[i].toString());
}
});
}; The R function: setShapeLabel <- function( map, data = getMapData(map), layerId,
label = NULL,
options = NULL
){
cat("in setShapeLabel","\n")
options <- c(list(layerId = layerId),
options,
filterNULL(list(label = label
)))
# evaluate all options
options <- evalFormula(options, data = data)
# make them the same length (by building a data.frame)
options <- do.call(data.frame, c(options, list(stringsAsFactors=FALSE)))
layerId <- options[[1]]
label <- options[-1] # drop layer column
# typo fixed in this line
leaflet::invokeMethod(map, data, "setLabel", "shape", layerId, label);
} And I call this in Shiny via calls in leafletProxy: leafletProxy("mymap", data = my_data) %>%
# in my code label() is a reactiveVal() that computes the numerical value
setShapeLabel(layerId = ~ID, label = label()) %>%
setShapeStyle( ... use as indicated in earlier comments... ) %>% |
EDITED - found a bug in the original post, and working through it, realized that the question of interest is something else. I'm running into an issue where I have an animation time series (slider over dates), with multiple points being present per day. I need these points colour-coded (in a palette set by the overall data - some days will have 1 point, others - many, and the colours need to stay consistent), but can't seem to make it happen with @timelyportfolio's setCircleMarkerStyle approach. Any help would be amazing. The example below runs (but requires @timelyportfolio's set of functions from earlier in the thread). `
` |
Is it possible to use this solution by referencing group rather than layerId? I'm plotting ~50K rows and the recolouring of each individual layerId is really slowing down my app animation. I'm wondering if updating style by group would resolve this issue (I only have ~500 groups on the map)... Any thoughts? |
I think I second @JWilson2021's reply. My data is a spatialpolygonsdataframe, where the first column is the country iso and the second the country name. The following columns include the data that should be displayed. I designed my shiny app to make it possible to display the data of a certain column based on input selections. I would go and select the columns with the selection input, where choice1 could be col_1 and thus
Any help would be much appreciated! |
For anyone having issues with the polygons not changing color, despite everything looking right. The leaflet/javascript/src/layer-manager.js Line 39 in 32cb9c5
|
Hi! First of all, thanks for the great posts in this thread - I learned a lot about leaflet and shiny! Currently, I'm trying to combine leaflet, shiny and shiny.i18n to have the labels of my leaflet maps (legend, base groups, etc.) translated automatically when users select another language via picker input. See here for an example: https://stackoverflow.com/questions/74941485/leaflet-map-crashes-on-recoloring-after-translation-shiny-leaflet-shiny-i18n Unfortunately, the app crashes as soon as the language setting is changed, however, I have no idea if the problem is related to updating the polygon coloring or if the bug is somewhere else. Any kind of help is greatly appreciated! |
Has there been any progress on updating this feature? May I suggest an updatePolygons() function of some sort to match Shiny convention. |
Hi! Thanks for creating this wonderful library! I do have an issue though.
Currently I have shapefiles for all counties for all 50 states. And I am coloring them based on some outcome variable chosen by the user (e.g. unemployment, poverty rate, etc.)
Using leafletProxy() I can update the color however this necessitates a redrawing of the polygons even though it is the same polygons being drawn over and over again! I just want to change the color.
Normally, this wouldn't be an issue, but because it is a relatively large collection of shapefiles it can take a bit before the map updates.
I was wondering if there was any plans in the work to edit an already existing set of polygons or something along those lines? If not, what would you recommend I do? (I've already reduced the shapefiles down in size as much as possible). Would this be an issue for leaflet.extras perhaps?
#170
The above issue seems similar to mine, and it seems like at some point there was work to be done on it? But it's a few years old, and I can't tell if there has been any work on it since then.
Thanks again!
The text was updated successfully, but these errors were encountered: