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

charting with leaflet #11

Open
smartinsightsfromdata opened this issue Feb 24, 2016 · 2 comments
Open

charting with leaflet #11

smartinsightsfromdata opened this issue Feb 24, 2016 · 2 comments

Comments

@smartinsightsfromdata
Copy link

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
@elbamos
Copy link
Owner

elbamos commented Mar 6, 2016

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

@ramnathv
Copy link

ramnathv commented Mar 6, 2016

@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?

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

3 participants