-
Notifications
You must be signed in to change notification settings - Fork 31
/
README.Rmd
179 lines (130 loc) · 7.29 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
---
output: github_document
---
```{r, echo = F, warning = F, message = F}
library(knitr)
opts_chunk$set(fig.path = 'man/figures/', warning = F, message = F)
```
# rStrava
##### *Marcus W. Beck, mbafs2012@gmail.com, Pedro Villarroel, pedrodvf@gmail.com, Daniel Padfield, dp323@exeter.ac.uk, Lorenzo Gaborini, lorenzo.gaborini@unil.ch, Niklas von Maltzahn, niklasvm@gmail.com*
[![R-CMD-check](https://github.com/fawda123/rStrava/workflows/R-CMD-check/badge.svg)](https://github.com/fawda123/rStrava/actions)
[![CRAN status](https://www.r-pkg.org/badges/version/rStrava)](https://CRAN.R-project.org/package=rStrava)
[![DOI](https://zenodo.org/badge/23404183.svg)](https://zenodo.org/badge/latestdoi/23404183)
[![](http://cranlogs.r-pkg.org/badges/grand-total/rStrava)](https://cran.rstudio.com/web/packages/rStrava/index.html)
<img src="man/figures/api_logo_pwrdBy_strava_horiz_light.png" align="left" width="300" />
<br></br>
<br></br>
### 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 from CRAN. It is also available on [r-universe](https://fawda123.r-universe.dev/).
```{r eval = F}
install.packages('rStrava')
```
The development version from this repository can be installed as follows:
```{r, eval = F}
install.packages('rStrava', repos = c('https://fawda123.r-universe.dev', 'https://cloud.r-project.org'))
```
### Issues and suggestions
Please report any issues and suggestions on the [issues link](https://github.com/fawda123/rStrava/issues) 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```:
```{r eval = F}
help.search('notoken', package = 'rStrava')
help.search('token', package = 'rStrava')
```
### Scraping functions (no token)
An example using the scraping functions is below. Some users may have privacy settings that block public access to account data.
```{r echo = FALSE, message = FALSE}
devtools::load_all('.')
```
```{r message = FALSE}
# get athlete data
athl_fun('2837007', trace = FALSE)
```
### 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](https://www.strava.com/). After the account is created, a personal API can be created under API tab of [profile settings](https://www.strava.com/settings/api). 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](https://strava.github.io/api/). 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.
```{r, eval = FALSE}
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, app_scope="activity:read_all"))
```
Setting `cache = TRUE` for `strava_oauth` will create an authentication file in the working directory. This can be used in later sessions as follows:
```{r, eval = FALSE}
stoken <- httr::config(token = readRDS('.httr-oauth')[[1]])
```
Finally, the `get_heat_map` and `get_elev_prof` functions require a key from the Google API. Follow the instructions [here](https://developers.google.com/maps/documentation/elevation/#api_key). The key can be added to the R environment file for later use:
```{r eval = FALSE}
# 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
google_key <- Sys.getenv("google_key")
```
#### Using the functions
```{r echo = FALSE, message = FALSE}
source('extra.R')
```
The API retrieval functions are used with the token.
```{r}
myinfo <- get_athlete(stoken, id = '2837007')
head(myinfo)
```
An example creating a heat map of activities:
```{r message = F, fig.height = 5.5, fig.width = 7, message = F, warning = F}
library(dplyr)
# get activities by date range
my_acts <- get_activity_list(stoken, after = as.Date('2020-12-31'))
act_data <- compile_activities(my_acts)
# subset by location
toplo <- act_data %>%
filter(grepl('Run$', name)) %>%
filter(start_latlng2 < -82.63 & start_latlng2 > -82.65) %>%
filter(start_latlng1 < 27.81 & start_latlng1 > 27.78)
get_heat_map(toplo, key = google_key, col = 'darkred', size = 1.5, distlab = F, alpha = 0.6, zoom = 13)
```
Plotting elevation and grade for a single ride:
```{r}
# get data for a single activity
my_acts <- get_activity_list(stoken, id = '1784292574')
act_data <- compile_activities(my_acts)
# plot elevation along a single ride
get_heat_map(my_acts, alpha = 1, add_elev = T, distlab = F, key = google_key, size = 2, col = 'Spectral', units = 'imperial')
# plot % gradient along a single ride
get_heat_map(my_acts, alpha = 1, add_elev = T, distlab = F, as_grad = T, key = google_key, size = 2, col = 'Spectral', units = 'imperial')
```
Get elevation profiles for activities:
```{r message = F, fig.height = 3, fig.width = 9}
get_elev_prof(my_acts, key = google_key, units = 'imperial')
get_elev_prof(my_acts, key = google_key, units = 'imperial', total = T)
```
Plot average speed per split (km or mile) for an activity:
```{r message = F, fig.height = 3, fig.width = 9}
# plots for most recent activity
plot_spdsplits(my_acts, stoken, units = 'imperial')
```
Additional functions are provided to get "stream" information for individual activities. Streams provide more 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 activities:
```{r, fig.height = 4, fig.with = 4}
# get streams for the first activity in my_acts
strms_data <- get_activity_streams(my_acts, stoken)
head(strms_data)
```
Stream data can be plotted using any of the plotting functions.
```{r, warning = F, message = F}
# heat map
get_heat_map(strms_data, alpha = 1, filltype = 'speed', size = 2, col = 'Spectral', distlab = F)
```
```{r message = F, fig.height = 3, fig.width = 9}
# elevation profile
get_elev_prof(strms_data)
# speed splits
plot_spdsplits(strms_data, stoken)
```
### Contributing
Please view our [contributing](.github/CONTRIBUTING.md) guidelines for any changes or pull requests.
### License
This package is released in the public domain under the creative commons license [CC0](https://tldrlegal.com/license/creative-commons-cc0-1.0-universal).