R/read_aois.R

#' Read And Tidy PEEK AOIS File
#'
#' This function allows you to read and parse a single PEEK order file generated by the Language
#' Learning Lab
#'
#' @description \code{read_aois()} is a special case of \code{read_delm()}.
#'    It's useful for doing minor cleaning of the AOI coordinates. If you set \code{tidy = FALSE},
#'    the function behaves like the default \code{read_delim()}. Note that
#'    you are able to pass along any arguments that you would use with \code{read_delim()}.
#' @param file Either a path to a file, a connection, or literal data.
#' @param tidy A boolean, if tidy = FALSE this functions behaves just \code{read_delim()}.
#' @param y_max An integer value indicating the largest y-coordinate in the data set. Used to create a
#' @param ... Additional arguments, typically used to control the \code{read_delim() function}.
#' @export
#' @examples
#' \dontrun{d <- read_aois(file = "Habla2_25_Clips_aoi.txt", tidy = TRUE, y_max = 2048)}


read_aois <- function(file, tidy = FALSE, y_max = 2048, ...) {
  if(tidy) {
    readr::read_delim(file, delim = '\t', ...)  %>%
      tidyr::gather(key = key, value = coord_value) %>%
      tidyr::separate(key, into = c("image_location", "coord_type"),
                      sep = "Image") %>%
      dplyr:: mutate(coord_value = ifelse(coord_type == "Y", y_max - coord_value, coord_value)) %>%
      dplyr::arrange(image_location, coord_value, coord_type)
  } else {
    readr::read_delim(file, delim = '\t', ...)
  }
}
kemacdonald/Rtobii documentation built on May 4, 2019, 1:21 p.m.