R/read_telemetry.R

Defines functions read_telemetry

Documented in read_telemetry

#' Import telemetry data
#'
#' @description Import telemetry data from one folder Data files must formatted in .csv.
#'
#'
#' @param folder a character vector containing full path names to the folder with the .csv files; the default corresponds to the working directory getwd().
#' @param read_SMM a logical value. If TRUE, reads .csv containing data of state-space models. If FALSE, reads telemetry data.
#' @param ... arguments can be passed to read.csv() to improve readability of .csv files.
#'
#' @details  Returns a  list  with all .csv data from the folder
#'
#' @example read_telemetry(folder = folder, stringsAsFactors = FALSE, sep = ",")
#'
#' @export

read_telemetry <- function(folder = NULL, read_SMM = FALSE, ...){

  if(is.null(folder))
    folder <- getwd()


  tmp.names <- dir(folder, "*.csv")

  if(isTRUE(length(tmp.names) == 0))
    stop(paste("There are no .csv files in", folder))

  #if(length(grep(tmp.names, pattern = "SSM")) > 0)

  if (read_SMM == FALSE) {

    tmp.names <- tmp.names[!tmp.names %in% grep(tmp.names, pattern = "SSM", value = T)]

  } else {

    tmp.names <- tmp.names[tmp.names %in% grep(tmp.names, pattern = "SSM", value = T)]

  }

  tmp.list.files <- list()

  for(i in 1:length(tmp.names)){

    if (read_SMM == TRUE){

      tmp.list.files[[i]] <- read.csv(file = paste0(folder, "/", tmp.names[i]), ...)

      tmp.list.files[[i]]$ID <- paste0(strsplit(tmp.names[i], split = "_")[[1]][1], "_", strsplit(tmp.names[i], split = "_")[[1]][2])

      } else {

      tmp.list.files[[i]] <- read.csv(file = paste0(folder, "/", tmp.names[i]), ...) %>%
        dplyr::mutate(Data = as.POSIXct(Date,
                                        tz = "America/Sao_Paulo",
                                        format = "%d/%m/%y %H:%M")) %>%
        dplyr::arrange(Data)

      tmp.list.files[[i]]$ID <- paste0(strsplit(tmp.names[i], split = "_")[[1]][1], "_",
                                       strsplit(tmp.names[i], split = "_")[[1]][2])

    }

  }

  names(tmp.list.files) <- gsub(tmp.names, pattern = ".csv", replacement = "")

  return(tmp.list.files)

}
machadoams/botomapr documentation built on Aug. 22, 2020, 9:44 p.m.