Zeppelin-With-R icon indicating copy to clipboard operation
Zeppelin-With-R copied to clipboard

charting with leaflet

Open smartinsightsfromdata opened this issue 9 years ago • 2 comments

I tried charting with Leaflet.js in rCharts and with RStudio leaflet library, with no success with either.

Would you be so kind to post an example that works?

Also, have you tried Zeppelin with any htmlwidgets? Do they work? (the method used below, of saving a file and displaying it as iframe was originally proposed by RamnathV as stop gap for jupyter with R kernel - it doesn't work with all htmlwidgets though).

Leaflet The following works with the jupyter notebook and R kernel, but it does not work with Zeppelin R:

library(leaflet)
m <- leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
  # Print the map
tf = 'myfigure.html'
htmlwidgets::saveWidget(m, file = tf, selfcontained = F)
IRdisplay::display_html(paste("<iframe src=' ", tf, " ' ","/>"))

rCharts I tried to display the classic example below, but the chart is displayed on another tab as index.html (on Safari).

%spark.r
# 
library(rCharts)
map3 <- Leaflet$new()
map3$setView(c(51.505, -0.09), zoom = 13)
map3$marker(c(51.5, -0.09), bindPopup = "<p> Hi. I am a popup </p>")
map3$marker(c(51.495, -0.083), bindPopup = "<p> Hi. I am another popup </p>")
map3

smartinsightsfromdata avatar Feb 24 '16 10:02 smartinsightsfromdata

Ok there's two separate issues here so let me address them in turn:

  • Leaflet - leaflet in rCharts definitely works, but IRdisplay is part of the Jupyter R package, so that isn't available. To get rCharts html output inline with rZeppelin, you need to tell rCharts to produce inline HTML output. Here is an example:
%spark.knitr results='asis',echo=F,warning=F,message=F
require(rCharts)
data(economics, package = 'ggplot2')
econ <- transform(economics, date = as.character(date))
m1 <- mPlot(x = 'date', y = c('psavert', 'uempmed'), type = 'Line',  
  data = econ)
m1$set(pointSize = 0, lineWidth = 1)
m1$show('inline',include_assets=TRUE, cdn=TRUE)

Note a couple of key things -- results are set to 'asis', and the plot is displayed with $show with particular parameters.

This example is from the demo notebook. That was included before, but isn't in the current version that's pending merge into zeppelin. So the instructions for doing this should probably get added to the manual

  • htmlwidgets - Do not work. Well, they don't display properly. The reason is that the htmlwidgets package uses js and css files with indirect dependencies. rmarkdown handles this by passing them through pandoc, which bundles those dependencies into the same HTML file. I've exchanged messages with @ramnathv about this, and spent considerable time trying to develop an R or scala implementation to handle the dependencies. It turns out to be highly non-trivial.

If you can suggest an acceptably-licensed R or java/scala library that can handle it, I'll write the code to bridge them together.


I'm going to leave this issue open for a few days in case you run into any problems with rCharts

elbamos avatar Mar 06 '16 07:03 elbamos

@elbamos here is one thought. if widget authors can also provide standalone versions of assets (pre-run through pandoc), then it is fairly trivial to write a dependency bundler in R that inlines all assets. The question is will widget authors be willing to add extra assets to their packages?

ramnathv avatar Mar 06 '16 07:03 ramnathv