Skip to content

Instantly share code, notes, and snippets.

@joebrew
Created August 4, 2017 16:39
Show Gist options
  • Save joebrew/bbf8d8b7a71518b5392a1fb2179b8142 to your computer and use it in GitHub Desktop.
Save joebrew/bbf8d8b7a71518b5392a1fb2179b8142 to your computer and use it in GitHub Desktop.
View Strava data with leaflet
# Define function for leaflet plot of only a country
leaf_country <- function(location,
limit = NULL,
city = FALSE){ # or NULL
if(city){
sub_data <- df %>%
filter(city == location)
} else {
sub_data <- df %>%
filter(country == location)
}
l <- leaflet() %>%
addProviderTiles(provider = 'CartoDB.DarkMatter',
options = providerTileOptions(opacity = 0.95))
captured_activities <- sub_data %>%
group_by(activity_id) %>%
tally %>%
.$activity_id
if(!is.null(limit)){
if(limit < length(captured_activities)){
captured_activities <- sample(captured_activities,
limit)
}
}
the_alpha <- ifelse(length(captured_activities) > 50,
0.4,
ifelse(length(captured_activities) > 100,
0.3,
0.5))
for (i in 1:length(captured_activities)){
message(i)
this_activity_id <- captured_activities[i]
sub_sub_data <- sub_data %>%
filter(activity_id == this_activity_id)
l <- l %>%
addPolylines(data = sub_sub_data, lng = ~lon, lat = ~lat,
color = 'red',
opacity = 1 - the_alpha,
weight = 0.3)
}
return(l)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment