simple dplyr + leaflet issue

1,211 views
Skip to first unread message

ArjunaCap

unread,
Jan 14, 2016, 11:41:41 AM1/14/16
to manipulatr
given the following:

library('dplyr')
library
('leaflet')


df
<- data_frame(lat = c(41,40,41,40), lng = c(-70, -70, -69.5, -69.5))


it's unclear why this fails:

df %>% leaflet() %>% addTiles() %>% addPolygons()

error: Don't know how to get path data from object of class tbl_df

the following also fails:

df
%>% leaflet() %>% addTiles() %>% addPolygons(lat = lat, lng = lng)


Error in inherits(f, "formula") : object 'lng' not found


and so does:


df
%>% leaflet() %>% addTiles() %>% addPolygons(lat, lng)


Error in inherits(f, "formula") : object 'lat' not found

this works:

df %>% leaflet() %>% addTiles() %>% addPolygons(lat = df$lat, lng = df$lng)

Am I missing something fundamental?


but of course having to re-specify "df" in the addPolygons call seems to defeat the convenience of piping

Dennis Murphy

unread,
Jan 14, 2016, 12:43:17 PM1/14/16
to ArjunaCap, manipulatr
Hi:

The Details section of the help page for leaflet::leaflet() provides a
clue: use ~ before the variable names. Applying this to your problem,

df %>% leaflet() %>% addTiles() %>% addPolygons(lat = ~lat, lng = ~lng)

produces a bow-tie shaped object in my web browser. Is that what you expected?

Dennis
> --
> You received this message because you are subscribed to the Google Groups
> "manipulatr" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to manipulatr+...@googlegroups.com.
> To post to this group, send email to manip...@googlegroups.com.
> Visit this group at https://groups.google.com/group/manipulatr.
> For more options, visit https://groups.google.com/d/optout.

Michael Cawthon

unread,
Jan 14, 2016, 12:58:06 PM1/14/16
to Dennis Murphy, manipulatr
Ah. This detail I'd forgotten. Thank you. 

Strangely enough, addCircleMarkers() in the pipe works- it's unclear why addPolygons() would be different in how explicitly lat/lng would need to be specified. 

-- 

Michael Cawthon
Chief Investment Officer
Green Street Energy LLC
mcaw...@greenstenergy.com
p: 479-442-1407
Reply all
Reply to author
Forward
0 new messages