Tour de trackeR

Hannah Frick and Ioannis Kosmidis

2018-10-12

The trackeR package provides infrastructure for handling running and cycling data from GPS-enabled tracking devices. A short demonstration of its functionality is provided below, based on data from running activities. A more comprehensive introduction to the package can be found in the vignette “Infrastructure for Running and Cycling Data”, which can be accessed by typing

vignette("trackeR", package = "trackeR")

Reading data

trackeR can currently import files in the Training Centre XML (TCX) format and .db3 files (SQLite databases, used, for example, by devices from GPSports) through the corresponding functions readTCX() and readDB3(). It also offers experimental support for JSON files from Golden Cheetah via readJSON().

library("trackeR")
filepath <- system.file("extdata/tcx/", "2013-06-01-183220.TCX", package = "trackeR")
runDF <- readTCX(file = filepath, timezone = "GMT")

These read functions return a data.frame of the following structure

str(runDF)
#> 'data.frame':    3881 obs. of  11 variables:
#>  $ time           : POSIXct, format: "2013-06-01 17:32:20" "2013-06-01 17:32:21" ...
#>  $ latitude       : num  50.8 50.8 50.8 50.8 50.8 ...
#>  $ longitude      : num  -1.7 -1.7 -1.7 -1.7 -1.7 ...
#>  $ altitude       : num  83.4 83.8 84 83.8 83.6 ...
#>  $ distance       : num  1.26 3.3 7.12 11.12 16.76 ...
#>  $ heart_rate     : num  56 61 61 71 71 74 74 85 85 85 ...
#>  $ speed          : num  0.885 1.209 1.801 2.205 2.756 ...
#>  $ cadence_running: num  60 63 70 78 83 84 84 85 85 86 ...
#>  $ cadence_cycling: logi  NA NA NA NA NA NA ...
#>  $ power          : logi  NA NA NA NA NA NA ...
#>  $ temperature    : logi  NA NA NA NA NA NA ...
#>  - attr(*, "sport")= chr "running"
#>  - attr(*, "file")= chr "/private/var/folders/3t/00tlvfn14zq5v45q3q3y63cm0000gn/T/Rtmprf3ZGJ/Rinst177c464ffa51/trackeR/extdata/tcx//2013"| __truncated__

That data.frame can be used as an input to the constructor function for trackeR’s trackeRdata class, to produce a session-based and unit-aware object that can be used for further analyses.

runTr0 <- trackeRdata(runDF)
#> Warning in sanity_checks(dat = dat, silent = silent): Observations with
#> duplicated time stamps have been removed.

The read_container() function combines the two steps of importing the data and constructing the trackeRdata object.

runTr1 <- read_container(filepath, type = "tcx", timezone = "GMT")
#> Warning in sanity_checks(dat = dat, silent = silent): Observations with
#> duplicated time stamps have been removed.
identical(runTr0, runTr1)
#> [1] TRUE

The read_directory() function can be used to read all supported files in a directory and produce the corresponding trackeRdata objects.

Visualisations

The package includes an example data set which can be accessed through

data("runs", package = "trackeR")

The default behaviour of the plot method for trackeRdata objects is to show how heart rate and pace evolve over the session.

plot(runs, session = 1:7)

The elevation profile of a training session is also accessible, here along with the pace.

plot(runs, session = 8, what = c("altitude", "pace"))

The route taken during a training session can also be plotted on maps from various sources e.g., from Google or OpenStreetMap. This can be done either on a static map

tryCatch(plot_route(runs, session = 1, source = "stamen"),
         error = function(x) "Failed to donwload map data")
#> [1] "Failed to donwload map data"

or on an interactive map.

tryCatch(leaflet_route(runs, session = c(1, 6, 12)),
         error = function(x) "Failed to donwload map data")

Session summaries

The summary of sessions includes basic statistics like duration, time spent moving, average speed, pace, and heart rate. The speed threshold used to distinguish moving from resting can be set by the argument moving_threshold.

summary(runs, session = 1, moving_threshold = c(cycling = 2, running = 1, swimming = 0.5))
#> 
#>  *** Session 1 : running ***
#> 
#>  Session times: 2013-06-01 18:32:15 - 2013-06-01 19:37:56 
#>  Distance: 14130.7 m 
#>  Duration: 65.68 mins 
#>  Moving time: 64.17 mins 
#>  Average speed: 3.59 m_per_s 
#>  Average speed moving: 3.67 m_per_s 
#>  Average pace (per 1 km): 4:38 min:sec
#>  Average pace moving (per 1 km): 4:32 min:sec
#>  Average cadence running: 88.66 steps_per_min 
#>  Average cadence cycling: NA rev_per_min 
#>  Average cadence running moving: 88.87 steps_per_min 
#>  Average cadence cycling moving: NA rev_per_min 
#>  Average power: NA W 
#>  Average power moving: NA W 
#>  Average heart rate: 141.11 bpm 
#>  Average heart rate moving: 141.13 bpm 
#>  Average heart rate resting: 136.76 bpm 
#>  Average temperature: NA C 
#>  Work to rest ratio: 42.31 
#> 
#>  Moving thresholds: 2.0 (cycling) 1.0 (running) 0.5 (swimming) m_per_s 
#>  Unit reference sport: running

It is usually desirable to visualise summaries from multiple sessions. This can be done using the plot method for summary objects. Below, we produce such a plot for average heart rate, average speed, distance, and duration.

runs_summary <- summary(runs)
plot(runs_summary, group = c("total", "moving"),
     what = c("avgSpeed", "distance", "duration", "avgHeartRate"))

The timeline plot is useful to visualise the date and time that the sessions took place and provide information of their relative duration.

timeline(runs_summary)
#> Warning in `[<-.data.frame`(`*tmp*`, nl, value = list(sport =
#> c("running", : replacement element 1 has 27 rows to replace 25 rows

Time in zones

The time spent training in certain zones, e.g., speed zones, can also be calculated and visualised.

run_zones <- zones(runs[1:4], what = "speed", breaks = c(0, 2:6, 12.5))
plot(run_zones)