Skip to content
forked from fawda123/rStrava

Functions to access data from Strava's v3 API.

Notifications You must be signed in to change notification settings

niklasvm/rStrava

 
 

Repository files navigation

rStrava

Marcus W. Beck, mbafs2012@gmail.com, Pedro Villarroel, pedrodvf@gmail.com, Daniel Padfield, dp323@exeter.ac.uk, Lorenzo Gaborini, lorenzo.gaborini@unil.ch

Linux: Travis-CI Build Status

Windows: AppVeyor Build Status

DOI

Overview and installation

This is the development repository for rStrava, an R package to access data from the Strava API. The package can be installed and loaded as follows:

install.packages('devtools')
devtools::install_github('fawda123/rStrava')

Issues and suggestions

Please report any issues and suggestions on the issues link for the repository.

Package overview

The functions are in two categories depending on mode of use. The first category of functions scrape data from the public Strava website and the second category uses the API functions or relies on data from the API functions. The second category requires an authentication token. The help files for each category can be viewed using help.search:

help.search('notoken', package = 'rStrava')
help.search('token', package = 'rStrava')

Scraping functions (no token)

An example using the scraping functions:

# get athlete data 
athl_fun(2837007, trace = FALSE)
## $`2837007`
## $`2837007`$units
## [1] "mi" "h"  "m"  "ft"
## 
## $`2837007`$location
## [1] "Irvine, California"
## 
## $`2837007`$current_month
##    Distance        Time   Elevation 
##   76.600000    5.516667 1410.000000 
## 
## $`2837007`$monthly
## Jun 2017      Jul      Aug      Sep      Oct      Nov      Dec Jan 2018 
## 318.4947 326.5579 173.3579 108.8526 286.2421 225.7684 258.0211 282.2105 
##      Feb      Mar      Apr      May      Jun 
## 298.3368 362.8421 274.1474 306.4000  76.6000 
## 
## $`2837007`$year_to_date
##       Distance           Time Elevation Gain          Rides 
##     1451.00000       95.01667    18878.00000      136.00000 
## 
## $`2837007`$all_time
##  Total Distance      Total Time Total Elev Gain     Total Rides 
##       21958.000        1343.367      168894.000        1686.000

API functions (token)

Setup

These functions require a Strava account and a personal API, both of which can be obtained on the Strava website. The user account can be created by following instructions on the Strava homepage. After the account is created, a personal API can be created under API tab of profile settings. The user must have an application name (chosen by the user), client id (different from the athlete id), and an application secret to create the authentication token. Additional information about the personal API can be found here. Every API retrieval function in the rStrava package requires an authentication token (called stoken in the help documents). The following is a suggested workflow for using the API functions with rStrava.

First, create the authentication token using your personal information from your API. Replace the app_name, app_client_id, and app_secret objects with the relevant info from your account.

app_name <- 'myappname' # chosen by user
app_client_id  <- 'myid' # an integer, assigned by Strava
app_secret <- 'xxxxxxxx' # an alphanumeric secret, assigned by Strava

# create the authentication token
stoken <- httr::config(token = strava_oauth(app_name, app_client_id, app_secret))

Setting cache = TRUE for strava_oauth will create an authentication file in the working directory. This can be used in later sessions as follows:

stoken <- httr::config(token = readRDS('.httr-oauth')[[1]])

Finally, the get_heat_map and get_elev_prof functions optionally retrieve elevation data from the Google Maps Elevation API. To use these features, an additional authentication key is required. Follow the instructions here. The key can be added to the R environment file for later use:

# save the key, do only once
cat("google_key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n",
    file=file.path(normalizePath("~/"), ".Renviron"),
    append=TRUE)

# retrieve the key, restart R if not found
mykey <- Sys.getenv("google_key")

Using the functions

The API retrieval functions are used with the token.

myinfo <- get_athlete(stoken, id = '2837007')
head(myinfo)
## $id
## [1] 2837007
## 
## $username
## [1] "beck_marcus"
## 
## $resource_state
## [1] 3
## 
## $firstname
## [1] "Marcus"
## 
## $lastname
## [1] "Beck"
## 
## $city
## [1] "Irvine"

An example creating a heat map of activities:

# get activities, get activities by location, plot
my_acts <- get_activity_list(stoken)
acts <- lapply(my_acts, function(x) x$location_city) %in% c('Pensacola', 'Pensacola Beach', 'Milton') 
get_heat_map(my_acts, acts = which(acts), col = 'darkgreen', size = 2, dist = F, f = 0.5)

Plotting elevation and grade for a single ride:

# plot elevation along a single ride
get_heat_map(my_acts, acts = 1, alpha = 1, add_elev = T, f = 0.3, key = mykey, size = 2, col = 'Spectral', maptype = 'satellite', units = 'imperial')
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.

# plot % gradient along a single ride
get_heat_map(my_acts, acts = 1, alpha = 1, add_elev = T, f = 0.3, as_grad = T, key = mykey, size = 2, col = 'Spectral', expand = 5, maptype = 'satellite', units = 'imperial')
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.

Get elevation profiles for activities:

# get activities
my_acts <- get_activity_list(stoken) 

get_elev_prof(my_acts, acts = 1, key = mykey, units = 'imperial')

get_elev_prof(my_acts, acts = 1, key = mykey, units = 'imperial', total = T)

Plot average speed per split (km or mile) for an activity:

# plots for most recent activity
plot_spdsplits(my_acts, stoken, acts = 1, units = 'imperial')

Additional functions are provided to get "stream" information for individual activities. Streams provide detailed information about location, time, speed, elevation, gradient, cadence, watts, temperature, and moving status (yes/no) for an individual activity.

Use get_activity_streams for detailed info about activites:

# get streams for the first activity in my_acts
strms_data <- get_activity_streams(my_acts, stoken, acts = 1)
head(strms_data)
##   altitude distance grade_smooth moving time velocity_smooth      lat
## 1      2.2   0.0000         -2.9  FALSE    0            0.00 30.41038
## 2      1.9   0.0067         -2.1  FALSE   60            0.36 30.41002
## 3      1.9   0.0104         -1.5   TRUE   62            0.72 30.41005
## 4      1.9   0.0143          0.0   TRUE   64            0.72 30.41007
## 5      1.9   0.0200          0.0   TRUE   66            7.92 30.41010
## 6      1.9   0.0234          0.0   TRUE   67            9.36 30.41011
##         lng        id
## 1 -87.22191 849369847
## 2 -87.22221 849369847
## 3 -87.22219 849369847
## 4 -87.22216 849369847
## 5 -87.22211 849369847
## 6 -87.22208 849369847
# make a plot
library(ggplot2)
ggplot(strms_data, aes(x = lng, y = lat, group = id, col = velocity_smooth)) + 
	geom_path(size = 2) +
	coord_equal() + 
	theme_void() +
	scale_colour_distiller('Speed (km/hr)', palette = 'Spectral')

License

This package is released in the public domain under the creative commons license CC0.

About

Functions to access data from Strava's v3 API.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • R 85.4%
  • JavaScript 12.7%
  • CSS 1.9%