Learn R Programming

fitdc (version 0.0.1)

read_fit: Decode a FIT file

Description

Decode a FIT file

Usage

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

Run this code
## 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

Run the code above in your browser using DataLab