Advanced Parsing with activatr

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

activatr can parse additional information beyond the basic lat/lon data from GPX/TCX files, as well as sample the files for quicker manipulation.

All of the advanced functionality is included in the parse_gpx or parse_tcx function with optional arguments. As a reminder, this is what the default parsing would look like and return:

library(activatr)

# Get the running_example.gpx file included with this package.
filename <- system.file(
  "extdata",
  "running_example.gpx.gz",
  package = "activatr"
)

df <- parse_gpx(filename)
knitr::kable(head(df, 5))

Parsing extension data

If your GPX file contains additional extension information, activatr can parse that as well. In this case, running_example.gpx contains heart rate, cadence, and temperature information. We can parse that by setting detail = "advanced" in parse_gpx:

df_advanced <- parse_gpx(filename, detail = "advanced")
knitr::kable(head(df_advanced, 5))

Now we can do plots like heart rate over time, or a distribution of cadences:

library(ggplot2)
library(dplyr)
ggplot(df_advanced) +
  geom_line(aes(x = time, y = hr), color = "red")
ggplot(filter(df_advanced, cad > 80)) +
  geom_density(aes(x = cad * 2), fill = "blue", bw = 1)

Sampling datapoints

If you're parsing many GPX files or GPX files sampled every second, you often don't need a "full resolution" view of the activity. The every argument to parse_gpx allows you to only sample some points from the GPX, speeding up the parsing:

# Parsing as normal gets all of the rows, but takes longer
full_time <- system.time({
  df_full <- parse_gpx(filename)
})
nrow(df_full)
full_time

# Grabbing every hundredth data point runs much faster
sample_time <- system.time({
  df_sample <- parse_gpx(filename, every = 100)
})
nrow(df_sample)
sample_time


Try the activatr package in your browser

Any scripts or data that you put into this service are public.

activatr documentation built on Aug. 22, 2023, 9:12 a.m.