R/read_order_file.R

#' Read And Parse PEEK Order File
#'
#' This function allows you to read and parse a single PEEK order file generated by the Language
#' Learning Lab
#'
#' @description \code{read_order_file()} is a special case of \code{read_delm()}.
#'    It's useful for doing minor cleaning of variable names. 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 ... Additional arguments, typically used to control the \code{read_delim() function}.
#' @export
#' @examples
#' \dontrun{d <- read_order_file(file = "Habla2_25_Clips_order.txt", tidy = TRUE)}

read_order_file <- function(file, tidy = TRUE, ...) {
  if(tidy) {
    d <- readr::read_delim(file, delim = '\t', ...) %>%
      dplyr::select(name:used)

    names(d) <- names(d) %>% make.names(unique = TRUE)
    names(d) <- stringr::str_replace(names(d), "[:punct:]", "_") %>% stringr::str_to_lower()

    # create target_word variable
    d <- d %>%
      dplyr::mutate(
        target_word = ifelse(target_side == "r", right_image, left_image),
        target_word = stringr::str_remove(target_word, pattern = "[:digit:]"))

    d
  } else {
    readr::read_delim(path, delim = '\t', ...)
  }
}
kemacdonald/Rtobii documentation built on May 4, 2019, 1:21 p.m.