grimbough / FITfileR

R package for reading data from FIT files using only native R code, rather than relying on external libraries.
https://msmith.de/FITfileR
53 stars 13 forks source link

Object 'res' not found error #38

Closed pbaylis closed 6 months ago

pbaylis commented 11 months ago

Hi, this is a great package. Thank you for writing it.

I am having trouble loading some of the .fit files Strava produces, such as the one from this activity. I'm including a minimum example to document the issue and have attached a compressed version of the .fit file below.

Ecola_Beach_morning.fit.zip

library(FITfileR)
fitfile = "~/Downloads/Ecola_Beach_morning.fit"
readFitFile(fitfile)
# Error in (function (fieldType, sizes, definition, con)  : 
# object 'res' not found

And here is my sessionInfo():

> sessionInfo()
R version 4.2.3 (2023-03-15)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS 14.1.1

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] FITfileR_0.1.8

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.11      pillar_1.9.0     compiler_4.2.3   plyr_1.8.8       bitops_1.0-7     remotes_2.4.2   
 [7] tools_4.2.3      bit_4.0.5        timechange_0.2.0 lubridate_1.9.3  lifecycle_1.0.4  tibble_3.2.1    
[13] gtable_0.3.4     lattice_0.20-45  pkgconfig_2.0.3  png_0.1-8        rlang_1.1.1      cli_3.6.1       
[19] rstudioapi_0.14  warp_0.2.0       xml2_1.3.3       dplyr_1.1.3      httr_1.4.5       stringr_1.5.1   
[25] generics_0.1.3   vctrs_0.6.4      bit64_4.0.5      grid_4.2.3       tidyselect_1.2.0 glue_1.6.2      
[31] R6_2.5.1         jpeg_0.1-10      fansi_1.0.5      pacman_0.5.1     sp_2.1-1         ggmap_4.0.0     
[37] slider_0.3.1     tidyr_1.3.0      activatr_0.2.0   ggplot2_3.4.4    purrr_1.0.2      magrittr_2.0.3  
[43] scales_1.2.1     geosphere_1.5-18 colorspace_2.1-0 utf8_1.2.3       stringi_1.7.12   munsell_0.5.0  
pbaylis commented 11 months ago

Following up, this seems to be some sort of corruption issue with some of my .FIT files. So fixing it in this package may be out of scope. Fortunately there is a pretty straightforward workaround: Garmin provides a Java tool that can repair these files. I ran it on this one and had no trouble importing the repaired version.

grimbough commented 6 months ago

I finally got round to fixing this. It was also reported in #41 with another file from a Coros watch. It seems the 'start' event i.e. when you press start on the watch, has a data field that doesn't actually contain any values. That package wasn't expecting this and would try to use the 'nothing' and fail.

For now I've included an NA value in this place as you can see below.

library(FITfileR)

## download and extract the example file
destfile = tempfile(fileext = ".zip")
url = "https://github.com/grimbough/FITfileR/files/13468357/Ecola_Beach_morning.fit.zip"
dl = download.file(url = url, destfile = destfile)
example_file = unzip(destfile, exdir = tempdir())

## read and extract the events
fitFile <- readFitFile(example_file[1])
events(fitFile) 
#> $event_1
#> # A tibble: 3 × 4
#>   timestamp           event event_type event_group
#>   <dttm>              <chr> <chr>            <int>
#> 1 2021-06-30 16:07:22 timer start                0
#> 2 2021-06-30 16:25:24 timer stop_all             0
#> 3 2021-06-30 16:47:33 timer stop_all             0
#> 
#> $event_2
#> # A tibble: 1 × 5
#>   timestamp           event event_type event_group data 
#>   <dttm>              <chr> <chr>            <int> <lgl>
#> 1 2021-06-30 16:29:04 timer start                0 NA