mapview icon indicating copy to clipboard operation
mapview copied to clipboard

Strange behavior with more than one popup open on two maps

Open kdybala opened this issue 8 years ago • 12 comments

Hi, thanks for the package and apologies if this isn't a mapview issue directly, but I haven't found this issue reported elsewhere. I am using R Markdown to create an html file that includes two separate leaflet maps, calling two separate sets of popupGraphs made with ggplot2. When I open a popup in the first map, leave it open and then open a popup in the second map, the second popup shows a partial/distorted legend of the first graph instead of its own legend. It doesn't happen if I close the first popup before clicking on the second, or if I go in reverse order, opening a popup in the second map first. I was able to reproduce it with a toy example (see screen shot attached). Any ideas? example

kdybala avatar Dec 06 '17 00:12 kdybala

Can you post the code of the toy example?

tim-salabim avatar Dec 06 '17 08:12 tim-salabim

Yes, here it is:

Map1

dat = data.frame(
  species = c('Proteo', 'Bactero', 'Actino', 'Firmi', 'Other'),
  value = c(0.6, 0.2, 0.15, 0.03, 0.02),
  x = -93,
  y = 41.9)

library(ggplot2)
p = ggplot(dat, aes(x=1, y=value, fill=species)) + geom_col()
pop.plots = mapview::popupGraph(p, type='svg', width=200, height=200)

dat = sp::SpatialPointsDataFrame(
  coords=dat[1, c('x', 'y')], proj4string=sp::CRS('+proj=longlat'), data=dat[1, ])

library(leaflet)
leaflet() %>% 
  addProviderTiles("Stamen.Terrain", group='Terrain') %>% 
  addCircleMarkers(data=dat, popup=~pop.plots)

Map2

dat2 = data.frame(
  guild = c('abc', 'bcd', 'cde', 'def', 'efg'),
  value = c(0.5, 0.2, 0.15, 0.03, 0.12),
  x = -92,
  y = 41.9)

p2 = ggplot(dat2, aes(x=1, y=value, fill=guild)) + geom_col()
pop.plots2 = mapview::popupGraph(p2, type='svg', width=200, height=200)

dat2 = sp::SpatialPointsDataFrame(
  coords=dat2[1, c('x','y')], proj4string=sp::CRS('+proj=longlat'), data=dat2[1,])

leaflet() %>% 
  addProviderTiles("Stamen.Terrain", group='Terrain') %>% 
  addCircleMarkers(data=dat2, popup=~pop.plots2)

kdybala avatar Dec 07 '17 00:12 kdybala

This is a result of duplicate ids that result from native svg creation in R. id in the DOM need to be unique. The only solution for this that I know of is svglite if you want to stick with svg. @tim-salabim, are you willing to accept another dependency? If so, I can submit a pull over the next couple of days.

timelyportfolio avatar Dec 07 '17 01:12 timelyportfolio

@kdybala thanks for the reprex! @timelyportfolio yes, a svglite dependency is fine, especially as we still haven't found a proper way of including png popups in non-standard locations (tempdir() only). In case you have a clever idea addressing this issue too, I'd appreciate any input.

tim-salabim avatar Dec 07 '17 08:12 tim-salabim

@timelyportfolio any news on this?

tim-salabim avatar Jan 09 '18 18:01 tim-salabim

I forgot about this one. I'll try to quickly assemble a prototype this week.

timelyportfolio avatar Jan 10 '18 01:01 timelyportfolio

@timelyportfolio I've merged your PR. When I run the svg example of popupGraph I see some strange behaviour that when we click on a point to open a graph, the fill color of all points disappears.

library(mapview)
library(sp)

data(meuse)
coordinates(meuse) = ~ x + y
proj4string(meuse) = CRS("+init=epsg:28992")

## create plots with points colored according to feature id
library(lattice)
p = xyplot(copper ~ cadmium, data = meuse@data, col = "grey")
p = mget(rep("p", length(meuse)))

clr = rep("grey", length(meuse))
p = lapply(1:length(p), function(i) {
  clr[i] = "red"
  update(p[[i]], col = clr)
})

mapview(meuse, popup = popupGraph(p, type = "svg"))

Any ideas?

tim-salabim avatar Jan 11 '18 18:01 tim-salabim

@tim-salabim, bummer I see it now, and unfortunately I can't immediately think of a good workaround. svglite uses <defs><style> which are not isolated, so the styles of the popup svg will infect the entire dom. For this to work, we would need to add some specificity to the popup styles. I will try to think of some solutions since I still think svglite is a better long-term solution.

timelyportfolio avatar Jan 12 '18 03:01 timelyportfolio

@timelyportfolio I still see the same behaviour as before... ? FWIW, we have a custom CSS for popups in mapview. In popupTable we simply set attr(<popstring>, "popup") = "mapview" which I tried for the svg case also, but it didn't change the behavious described above. Any more ideas?

tim-salabim avatar Jan 16 '18 17:01 tim-salabim

That is odd. Can you send me a screenshot of the dom element for the popup? I just tried again, and this is working for me, but there is no telling all the hacks and dev versions that I have on my system.

mapview_svg

with the popup DOM element

image

timelyportfolio avatar Jan 17 '18 01:01 timelyportfolio

If this is not the correct information let me know.

screenshot at 2018-01-21 12 10 36

tim-salabim avatar Jan 21 '18 11:01 tim-salabim

ok, I think I see what is happening. My un-robust gsub is not adding the element id to the style, so it is not applied. I will try to think through other solutions to this problem.

timelyportfolio avatar Jan 21 '18 13:01 timelyportfolio