Install the devtools package then you can install the FITfileR package from github.
https://www.karada-good.net/analyticsr/r-747/
Code
# Check if 'FITfileR' package is installed, if not, install it using 'remotes'if (!requireNamespace("FITfileR", quietly =TRUE)) {if (!requireNamespace("remotes", quietly =TRUE)) {install.packages("remotes") } remotes::install_github("grimbough/FITfileR")}library(FITfileR)
2. Read fit file
2.1 Using the readFitFile function.
Continuous data loading for improved reproducibility.
---title: "1. Create RHRV-readable ASCII files from fit."subtitle: "fit_to_csv.qmd"author: - Kizen Sasakidate: 2023-11-30date-modified: todaydate-format: 'YYYY MMMM DD'language: title-block-published: First version titel-block-modified: This versionformat: html: html-math-method: katex code-tools: true code-fold: true code-link: true pdf: papersize: a4 geometry: - top=25mm - left=30mm docx: defaultcode-line-numbers: true---##### 1. Install packages###### 1.1 FITfileR package- Install the devtools package then you can install the FITfileR package from github.- https://www.karada-good.net/analyticsr/r-747/```{r}#| message: false#| echo: true# Check if 'FITfileR' package is installed, if not, install it using 'remotes'if (!requireNamespace("FITfileR", quietly =TRUE)) {if (!requireNamespace("remotes", quietly =TRUE)) {install.packages("remotes") } remotes::install_github("grimbough/FITfileR")}library(FITfileR)``````{r}#| message: false#| echo: false# Check if 'tidyverse' package is installed, if not, install itif (!require("tidyverse", quietly =TRUE)) {install.packages("tidyverse")}library(tidyverse)# Check if 'openxlsx' package is installed, if not, install itif (!require("openxlsx", quietly =TRUE)) {install.packages("openxlsx")}library(openxlsx)# Check if 'lubridate' package is installed, if not, install itif (!require("lubridate", quietly =TRUE)) {install.packages("lubridate")}```##### 2. Read fit file###### 2.1 Using the readFitFile function.- Continuous data loading for improved reproducibility.```{r}library(lubridate)#| message: truesetwd("~/Documents/brevet")fit_file_name <-"fit_data/1040/2023-12-29-01-42-14.fit"ReadFit <-readFitFile(fit_file_name)ReadFit```##### 2.1.1 Verigy message_type```{r}listMessageTypes(ReadFit)```###### 2.1.2 Retrieve record data```{r}RecordData <-getMessagesByType(ReadFit,message_type ="record")names(RecordData)head(RecordData$record_1,2)```###### 2.1.3 Extract timestamp and heart rate data from###### 2.1.3.1 data storage argument```{r}MasterRecord <-NULL```- Loop processing for RecordData```{r}for(i inseq(RecordData)){ Record <-bind_cols("id"= i, RecordData[[i]]) MasterRecord <-bind_rows(MasterRecord, Record)}```###### 2.1.3.2 Sort MasterRecord by time.```{r}MasterRecord <- MasterRecord %>%arrange(timestamp)```- Create time_hr.csv to replace example.beta in RHVR.- Extract timestamp and heart_rate from MasterRecord to time_hr.- possible to add other data in c("timestamp", "heart_rate", "cadence", "speed", "distance", "altitude", "power", "temperature", "etc", "etc")```{r}time_hr_01 <- MasterRecord[, c("timestamp", "heart_rate")] %>%na.omit() # delete NA```###### 2.1.4 Convert the date to Japan Standard Time(JST).```{r}MasterRecord <- MasterRecord %>%mutate_if(is.POSIXt, ~with_tz(., tz ="Asia/Tokyo"))```###### 2.1.4.1 Sort MasterRecord by time.```{r}MasterRecord <- MasterRecord %>%arrange(timestamp)```- Create time_hr.csv to replace example.beta in RHVR.- Extract timestamp and heart_rate from MasterRecord to time_hr.```{r}time_hr <- MasterRecord[, c("timestamp", "heart_rate")] %>%na.omit() # delete NA```###### 2.2 Write csv file and save to csv folder```{r}# Reproducibility needs improvementfile_name <-"20231229_night.csv"# input file namewrite.csv(time_hr, file = file_name, row.names =FALSE)```###### 2.3 Read csv file as data for next step.```{r}# Reproducibility needs improvementfile_name <-"20231229_night.csv"folderPath <-"csv"```###### 2.2.2 Write csv file to csv_xls folder```{r}# Reproducibility needs improvementsetwd("~/Documents/brevet")write.table(time_hr, file.path(folderPath, file_name),sep =",", row.names =FALSE)```##### 3. Read csv file as data for next step.```{r}# Reproducibility needs improvementsetwd("~/Documents/brevet")data <-read.table(file.path(folderPath, file_name),header =TRUE, sep =",", colClasses =c("character", "numeric"))```