#' icytracks: Read Icy Tracks XLS
#'
#' Reads the .xls files generated by the Icy 'Export Tracks to XLS' Track
#' Processor and convert it to a data.frame.
#'
#' @docType package
#' @name icytracks
#' @importFrom magrittr "%>%"
#' @importFrom dplyr filter_ select_ mutate_ bind_rows
NULL
utils::globalVariables(c(".", "%>%"))
#' Read an Icy tracks XLS
#'
#' Reads the .xls files generated by the Icy 'Export Tracks to XLS' Track
#' Processor and convert it to a data.frame.
#'
#' @param file The Icy XLS file with the tracks.
#'
#' @family icytracks functions
#'
#' @export
#'
#' @examples
#' \dontrun{
#' read.icy.track.xls('~/icytrack.xls')
#' }
read.icy.track.xls <- function(file) {
xls_data <- readIcyXlsData(file)
track_bounds <- getTrackBoundsFromXlsData(xls_data)
getTrackDF(xls_data, track_bounds)
}
readIcyXlsData <- function(file) {
# Read in data, remove first column (which contains the "track #")
xls_data <- gdata::read.xls(file, header = F, stringsAsFactors=F) %>% select_(-1)
# Give sensible names
names(xls_data) <- c('track', 't', 'x', 'y', 'z' )
xls_data
}
getTrackBoundsFromXlsData <- function(xls_data) {
track_df <- xls_data %>%
tibble::rownames_to_column() %>%
filter_(~!is.na(track)) %>%
select_(~track, begin = ~rowname)
# Begin is 2 lines after track line, end is one line before next track line.
# (blank lines are skipped while reading in xls_data)
track_df <- track_df %>%
mutate_(
begin = ~as.numeric(begin),
end = ~lead(begin) - 1,
begin = ~begin + 2
)
# Fix last entry
track_df[nrow(track_df),]$end <- nrow(xls_data)
track_df
}
getTrackDF <- function(xls_data, track_bounds) {
track_bound_l <- split(track_bounds, seq(nrow(track_bounds)))
track_df_l <- invisible(lapply(track_bound_l, function(x) {
# Get correct rows from xls_data
t <- xls_data[x$begin:x$end,]
# Put in the correct track
t$track <- x$track
t
}))
# Bind list together to get the DF, then convert to data matrix (make numeric),
# then back to DF
as.data.frame(data.matrix(bind_rows(track_df_l)))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.