Code
if(!requireNamespace("remotes")) {
install.packages("remotes")
}
remotes::install_github("grimbough/FITfileR")
library("FITfileR")
library(tidyverse)Read Garmin FIT file and extract the necessary data for mapping
if(!requireNamespace("remotes")) {
install.packages("remotes")
}
remotes::install_github("grimbough/FITfileR")
library("FITfileR")
library(tidyverse)TargetDir <- paste(as.character(tkchooseDirectory(title = "Select the fit data folder"),sep = "", collapse =""))
library("tcltk")
FitNameList <- list.files(path = TargetDir) %>%
str_extract("^.*fit*$") %>%
na.omit()
#FitNameList # checkIf the FIT file is the third, give 3 to FileNo. FileNo <- 2
setwd(TargetDir)
FileNo <- 65
ReadFit <- readFitFile(FitNameList[FileNo])
ReadFit # checkFit File
├╴File created: 2023-10-15 06:41:42
├╴Device: garmin NA
└╴Number of data messages: 13676
listMessageTypes(ReadFit) # check [1] "file_id" "device_settings"
[3] "user_profile" "zones_target"
[5] "sport" "session"
[7] "lap" "record"
[9] "event" "device_info"
[11] "activity" "file_creator"
[13] "device_aux_battery_info"
“records” often contain data such as heart rat, latitude and longtitude, etc.
# Read the record message
RecodeData <- getMessagesByType(ReadFit,
message_type = "record")
#RecodeData #check
# Merge all the message together into a single tibble.(dplyr)
MasterRecord <- RecodeData %>%
bind_rows() %>% arrange(timestamp)
#日付けデータを変換する
MasterRecord <-
MasterRecord %>%
mutate_if(is.POSIXt, ~with_tz(., tz = "Asia/Tokyo"))
#MasterRecordを時間で並び替え
MasterRecord <- MasterRecord %>%
arrange(timestamp)coords <- MasterRecord %>%
select(position_long, position_lat) library(leaflet)
# Leafletを使用して地図を描画
m_leaflet <- coords %>% as.matrix() %>%
leaflet( ) %>%
addTiles() %>%
addPolylines( )
m_leaflet