R/read_gate_data.R

read_batch_gate_data <- function(directory){

  files <- dir(directory)
  file <- paste0(directory, "/", files[3])
  read_gate_data(file)

}


#' Reads PIT tag data recorded at the gate..
#'
#' Reads data from a file generated by the infrared gate used to read PIT tags.
#'
#' @param file Path to the file as a string.
#' @param roost Roost signature
#'
#' @return Dataframe (timestamp, chipID)
#' @export
#'
#' @examples
#'
read_gate_data <- function(file, roost = NA_character_){

  df <- readr::read_csv2(file, col_names = FALSE, col_types = "ccc")
  assertthat::assert_that(assertthat::not_empty(df),
                          msg = "The file may contain no data")

  names(df) <- c("time", "date", "chipID")

  df <- df %>%
    dplyr::transmute(
      file,
      roost,
      timestamp =
        lubridate::dmy_hms(paste0(date, .extract_year(file, "20"), time)),
      chipID = stringr::str_sub(chipID, 4)
    )

  df
}



#' .extract_year
#'
#' Helper function used in read_gate_data to extract year from file name.
#'
#' @param file_name
#'
#' @return string
#'

.extract_year <-function(file_name, prefix){
  file_name <- stringr::str_extract(file_name, ".[0-9]{6}+..txt$")
  paste0(prefix, stringr::str_sub(file_name, 2, 3))
}
zielinskipp/batman documentation built on May 12, 2019, 9:40 a.m.