0

I am working from (very helpful) code provided here https://github.com/timelyportfolio/leaftime/blob/master/inst/examples/example_leaftime.R to build a timeseries map using leaflet in R. I can't seem to figure out how to define the radius of each point on my map, and hoping someone might be able to point me in the right direction.

My test data are as follows:

data <- data.frame(

"latitude" = c(36.998953, 38.998607, 40.547953, 36.475103, 34.634023),

"longitude" = c(-78.766255, -81.051412, -78.969508, -83.298121, -78.541041),

"start" = seq.Date(as.Date("2009-01-01"), by="day", length.out = 5),

"end" = seq.Date(as.Date("2009-01-01"), by = "day", length.out = 5) + 1,

"radius" = c(10, 8, 5, 12, 7)

)

I'm able to create a basic timeseries map using the code here:

library(leaflet)
library(leaftime)
library(htmltools)
library(geojsonio)
library(geojsonlint)

leaflet(geojsonio::geojson_json(data, lat="latitude", lon="longitude")) %>%
  addTiles() %>%
  setView(-79.771504, 36.854041, 2) %>%
  addTimeline(
    sliderOpts = sliderOptions(
      formatOutput = htmlwidgets::JS("function(date) {return new 
      Date(date).toDateString()}"), position = "bottomright", duration = 3000),
    timelineOpts = timelineOptions(
      styleOptions = styleOptions(radius = 5, color = "black", fillColor = "blue", fillOpacity = 0.60)))

But modifying this code to customize the radius for each point doesn't work:

leaflet(geojsonio::geojson_json(data, lat="latitude", lon="longitude")) %>%
  addTiles() %>%
  setView(-79.771504, 36.854041, 2) %>%
  addTimeline(
    sliderOpts = sliderOptions(
      formatOutput = htmlwidgets::JS("function(date) {return new 
      Date(date).toDateString()}"), position = "bottomright", duration = 3000),
    timelineOpts = timelineOptions(
      styleOptions = styleOptions(radius = htmlwidgets::JS("function getRadius(d) {return +d.properties.radius}"), 
      color = "black", fillColor = "blue", fillOpacity = 0.60)))

It seems that the error comes in when calling the java script function getRadius from the JS htmlwidget, but I'm not familiar with the java script language and not sure how to fix. Thanks for any help!

nrcombs
  • 503
  • 3
  • 17
  • 1
    Update: for any other users, a fix is provided here https://github.com/timelyportfolio/leaftime/issues/2 – nrcombs Jan 10 '20 at 14:02

1 Answers1

0

If you mean its the JS Code, try: function getRadius(d) {return d.properties.radius}

Falke Design
  • 10,635
  • 3
  • 15
  • 30
  • thanks for the suggestion @Falke Design but it did not fix the problem, is there another way to define the radius of each point? – nrcombs Jan 08 '20 at 13:22
  • Does it work if you hardcode the radius? instead: `radius = htmlwidgets::JS(...` `radius = 5`, – Falke Design Jan 08 '20 at 13:24
  • Ups, you had tried this already. What do you get in the console with: `htmlwidgets::JS("function getRadius(d) {console.log(d); return d.properties.radius;}") – Falke Design Jan 08 '20 at 14:09
  • Thanks @Falke Design. Nothing is printed to the console when I run the code you suggested...the map appears but without points, as it did before. Perhaps I am doing something wrong? Another thought on why my code may not be working, am I pointing the htmlwidgets::JS to the correct source (data) for the radius information? – nrcombs Jan 08 '20 at 15:25
  • Yes it looks like you get no data in the function. You can try to use the debugger in the Browser. But sorry I can't help you more, I never programmed in R and also i don't know anything about htmlwidgets – Falke Design Jan 08 '20 at 15:36