Instantly share code, notes, and snippets.

Embed
What would you like to do?
Calculate and plot your relative running economy with Strava and R
library(rStrava)
library(ggplot2)
library(lubridate)
library(dplyr)
library(googledrive)
# Running Economy function
relative_economy <- function(distance, total_time, restingHR, averageHR){
total_beats <- (averageHR - restingHR) * (total_time/60)
work_per_km <- total_beats/distance
efficiency <- 1 / work_per_km * 100000
return(efficiency)
}
# Strava Account details
app_name <- <your_app_name>
app_client_id <- <your_client_id>
app_secret <- <your_app_secret>
# create the Strava authentication token
stoken <- httr::config(token = strava_oauth(app_name, app_client_id, app_secret))
# get activities from Strava
my_acts <- get_activity_list(stoken)
# Create a dataframe of activities
runs <- compile_activities(my_acts)
# Change the local time to POSIXct format for later processing
runs$start_date_local <- date(runs$start_date_local)
# Filter the dataframe to the required date range
filtered_runs <- filter(runs, ymd(runs$start_date_local) >= '2017-07-07')
# Get restingHR + bodyweight data from Google Drive
x <- drive_find(pattern = "Weight-RHR Form")
drive_download(x[1,], type = "csv")
weight_heart_data <- select(read.csv("Weight-RHR Form (Responses).csv"),
-Timestamp)
weight_heart_data$Date <- date(dmy(weight_heart_data$Date, tz = "Australia/Sydney"))
# Join the two data frame by date
filtered_runs <- left_join(filtered_runs, weight_heart_data, by = c("start_date_local" = "Date"))
# Insert missing Resting Heart Rate data with a default value
filtered_runs$Resting.Heart.Rate[is.na(filtered_runs$Resting.Hear.Rate)] <- 53
# Add relative running economy data
filtered_runs$economy <- relative_economy(filtered_runs$distance,
filtered_runs$moving_time,
filtered_runs$Resting.Heart.Rate,
as.numeric(filtered_runs$average_heartrate))
# Make a plot of the economy data
ggplot(filtered_runs, aes(x = start_date_local, y = economy)) +
geom_point(size = 1) +
geom_smooth() +
xlab("Date") + ylab("Economy") +
ggtitle("Relative Running Economy")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment