Generating internal lists of data types and messages

Many of the data points in a FIT file are integers that correspond to specific values a variety of enum types. These are defined in the file Profile.xlsx, which is included in the FIT SDK. In order to convert these into their intended values when reading a FIT file I have included the contents of this file as two lists saved in .rda files. The code below details how these two objects were creates.

Note: For some reason I am unable to read the original version of Profile.xlsx in any of the R/Excel packages I tried. Instead, I uploaded the file to Google Sheets, converted it to that format, and then downloaded it again as a .xlsx. This version is included in the package as Profile_gsheets.xlsx

library(tibble)
library(dplyr)
library(openxlsx)

Here we read the message definitions:

messages <- read.xlsx(system.file("extdata/Profile_gsheets_March22.xlsx", package = "FITfileR"), 
                      sheet = 2)
rm.idx <- which(is.na(messages[,1]) & is.na(messages[,2]) & is.na(messages[,3]))
messages <- messages[-rm.idx,c(1:4,7:9)]
names(messages) <- c("message_type", "key", "value", "type", "scale", "offset", "units")
messages <- mutate(messages, type = as.factor(type), scale = as.numeric(scale), offset = as.numeric(offset))

idx <- which(!is.na(messages[,1]))
messages[,1] <- rep(messages[idx,1], 
                diff(c(idx, nrow(messages)+1)))
messages <- as_tibble(messages[-which(is.na(messages[,2])),])

fit_message_types_new <- split(messages[,2:7], messages[[1]])

Here we read the data type definitions:

data_types <- read.xlsx("extdata/Profile_gsheets_March22.xlsx", 
                             sheet = 1)[,c(1,4,3,2)]
data_types <- setNames(data_types, c("data_type", "key", "value", "key_type"))
idx <- which(!is.na(data_types[,1]))
data_types[,1] <- rep(data_types[idx,1], 
              diff(c(idx, nrow(data_types)+1)))
data_types[,4] <- rep(data_types[idx,4], 
              diff(c(idx, nrow(data_types)+1)))
data_types <- as_tibble(data_types[-which(is.na(data_types[,2])),])
fit_data_types <- split(data_types[,2:4], data_types[[1]])
fit_data_types_new <- lapply(fit_data_types, FUN = function(x) {
    mutate(x, key = ifelse(grepl('uint32', key_type), 
                         yes = as.numeric(key), 
                         no = as.integer(key))) %>%
    select(-key_type)
})
load("R/sysdata.rda")
## overwrite the old versions
fit_data_types <- fit_data_types_new
fit_message_types <- fit_message_types_new

usethis::use_data(data_type_lookup, fit_data_types, fit_message_types, 
                  internal = TRUE, overwrite = TRUE)


grimbough/fitFileR documentation built on April 24, 2024, 12:07 a.m.