Build a barcode in a popup using a flyer library

Quick question. I have some data on sql server that I uploaded to RStudio. I did barchart for the data, and now I use the library of leaflets using latitude and longitude to plot a point on the map. I want to be able to use a popup to show a barcher in it when the user clicks on a point.

BarChart code (maybe this is a problem because I use the googleVis library, so I'm not sure if I can use this in the popup, but again this is the most suitable histogram that I can do and what I need - other suggestions may be useful not a professional in R libraries yet)

Switzerland <- sqlQuery(con, "sql query") SwitzerlandChart <- gvisBarChart(Switzerland, options = list(height=200)) 

For graph, graph code:

 m <- leaflet() %>% addTiles() %>% # Add default OpenStreetMap map tiles addCircles(lng=8.498868, lat=46.9221, popup=paste(plot(SwitzerlandChart))) 

When I run this code, it opens a web page to view my barcode. Then I run the following:

 m #Prints the graph 

This prints a graph with a dot in the right place, but a pop-up window shows me a web page, and also only I can open.

I want to be able to build a bargraph inside the popup.

Hope someone can help

+7
source share
2 answers

Maybe a little late, but here is the solution. The addPopups() function in library(leaflet) seems to be able to handle .svg files. So you can just save your plot with svg() and then read it again using readLines() . Here's a reproducible example using library(mapview) :

 library(lattice) library(mapview) library(sp) data(meuse) coordinates(meuse) <- ~x+y proj4string(meuse) <- CRS("+init=epsg:28992") clr <- rep("grey", length(meuse)) fldr <- tempfile() dir.create(fldr) pop <- lapply(seq(length(meuse)), function(i) { clr[i] <- "red" p <- xyplot(meuse$cadmium ~ meuse$copper, col = clr, pch = 20, alpha = 0.7) svg(filename = paste(fldr, "test.svg", sep = "/"), width = 250 * 0.01334, height = 250 * 0.01334) print(p) dev.off() tst <- paste(readLines(paste(fldr, "test.svg", sep = "/")), collapse = "") return(tst) }) mapview(meuse, popup = pop, cex = "cadmium") 

You will see that each popup is a scatter chart. As for the leaflet example, consider this:

 content <- pop[[1]] leaflet() %>% addTiles() %>% addPopups(-122.327298, 47.597131, content, options = popupOptions(closeButton = FALSE) ) 

If you want the graph to be interactive, you could take a look at the library(gridSVG) , which can create interactive svg graphs, for example. lattice or ggplot2 .

UPDATE:

library(mapview) now designated functionality for this:

  • popupGraph : embed lattice graphics, ggplot2 or interactive hatmlwidgets .
  • popupImage : embed local or remote (web images)

Currently, this is only available in the mapview development version , which can be installed using

 devtools::install_github("environmentalinformatics-marburg/mapview", ref = "develop" 
+9
source

It may be a little late, but there is a complete flyer implementation. First I create a graph, and then use the popupGraph function to add it.

 # make a plot of the two columns in the dataset p <- xyplot(Home ~ Auto, data = Jun, col = "orange", pch = 20, cex = 2) # make one for each data point p <- mget(rep("p", length(Jun))) # color code it so that the corresponding points are dark green clr <- rep("orange", length(Jun)) p <- lapply(1:length(p), function(i) { clr[i] <- "dark green" update(p[[i]], col = clr) }) # now make the leaflet map m1 <- leaflet() %>% addTiles() %>% setView(lng = -72, lat = 41, zoom = 8) %>% # add the markers for the Jun dataset # use the popupGraph function addCircleMarkers(data = Jun, lat = ~Lat, lng = ~Lon, color = ~beatCol(BeatHomeLvl), popup = popupGraph(p), radius = ~sqrt(BeatHome*50), group = 'Home - Jun') %>% # layer control addLayersControl( overlayGroups = c('Home - Jun' ), options = layersControlOptions(collapsed = F) ) %>% # legend for compare to average addLegend('bottomright', pal = beatCol, values = last$BeatTotalLvl, title = 'Compare<br>Quote Count to<br>3Mos State Avg', opacity = 1) m1 

Here is the conclusion. enter image description here

+1
source

All Articles