read_fit: Decode a FIT file

Description Usage Arguments Value Examples

View source: R/read_fit.R

Description

Decode a FIT file

Usage

1
read_fit(file_path)

Arguments

file_path

string; path to the FIT file to be read.

Value

decoded data messages from the FIT file.

Examples

 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
## An example of generating a table of record messages
## from the file provided with this package:

fp <- system.file("extdata/example.fit", package = "fitdc")
data_mesgs <- read_fit(fp)

## Filter out the record messages:

is_record <- function(mesg) mesg$name == "record"
records <- Filter(is_record, data_mesgs)

format_record <- function(record) {
  out <- record$fields
  names(out) <- paste(names(out), record$units, sep = ".")
  out
}

records <- lapply(records, format_record)

## Some records have missing fields:

colnames_full <- names(records[[which.max(lengths(records))]])
empty <- setNames(
  as.list(rep(NA, length(colnames_full))),
  colnames_full)

merge_lists <- function(ls_part, ls_full) {
  extra <- setdiff(names(ls_full), names(ls_part))
  append(ls_part, ls_full[extra])[names(ls_full)]  # order as well
}

records <- lapply(records, merge_lists, empty)
records <- data.frame(
  do.call(rbind, records))

head(records)  # voila

jmackie4/fitdc documentation built on May 19, 2019, 1:50 p.m.