Defines functions process_multi_cinderella process_cinderella_chat

Documented in process_cinderella_chat process_multi_cinderella

#' Process Narrative Metrics from Raw CHAT Output
#' Takes the output from CHAT for a narrative sample and converts it into a form
#' that can be directly read into Redcap. This can be done through an API write
#' call or through uploading it manually.
#' @param filepath Character, Filepath to a .csv or .xlsx file created by CHAT
#' @param record_id Character or Numeric, Record ID for this participant
#' @param instance Character or Numeric, Instance number for this participant's
#' narrative sample
#' @param date Character, Date of the sample in MM-DD-YYYY format
#' @param study Character, Which study is this sample for?
#' @param timepoint Character, Which timepoint of the study is this sample for?
#' @param record_id_col Defaults to record_id
#' @param inst_name Defaults to cinderella_narrative_summary
#' @param entry Person doing data entry
#' @param administrator Character, researcher NetID
#' @return A dataframe ready to be exported to redcap
#' @export
process_cinderella_chat <- function(filepath,
                                    record_id = NA,
                                    instance = NA,
                                    entry = NA,
                                    administrator = NA,
                                    date = NA,
                                    study = NA,
                                    timepoint = NA,
                                    record_id_col = "demo_record_id",
                                    inst_name = "cinderella_narrative_summary") {

  # Error handling for missing parameters
  if (is.na(record_id)) stop("Error: Must provide a record_id")
  if (is.na(instance)) stop("Error: Must provide an instance number (an integer)")
  if (!is.na(date)) {
    if (grepl("\\d{2}-\\d{2}-\\d{4}")) {
      stop("Error: Date provided must be in MM-DD-YYYY format.")

  # Read in file, handle invalid file extensions
  extension <- stringr::str_extract(filepath, "\\.[[:alnum:]]+$")
  if (is.na(extension)) {
    stop("Error: Filepath has no file extension")
  } else if (extension == ".csv") {
    raw_chat <- utils::read.csv(filepath)
  } else if (extension == ".xlsx") {
    raw_chat <- readxl::read_xlsx(filepath)
  } else {
    stop("Error: File must be a .csv or .xlsx file")

  # Hard code the raw column names and the redcap column names
  raw_cols <-
      "Duration (sec)",
      "Total Utts",
      "Total Words",
      "MLU Words",
      "% open-class/all words",
      "% closed-class/all words",
      "% Nouns/all words",
      "% Verbs/all words",
      "negation markers",
      "infinitival markers",
      "comparative suffixes",
      "superlative suffixes",
      "possessive markers",
      "regular plural markers",
      "irregular plural forms",
      "3rd person present tense markers",
      "regular past tense markers",
      "irregular past tense markers",
      "regular perfect aspect markers",
      "irregular perfect participles",
      "progressive aspect markers",
      "% correct regular verb inflection",
      "% correct irregular verb inflection",
      "% sentences produced",
      "% sentences with correct syntax, semantics*",
      "% sentences with flawed syntax",
      "% sentences with flawed semantics*",
      "sentence complexity ratio",
      "# embedded clauses/sentence"
  fix_cols <-
  # Keep only the relevant fields
  output <- raw_chat[, raw_cols]

  # Rename fields to redcap's variable names
  colnames(output) <- fix_cols

  # Set the patient and meta data if provided (record_id and instance required)
  output[[record_id_col]] <- record_id
  output[["redcap_repeat_instance"]] <- instance
  output[["redcap_repeat_instrument"]] <- inst_name
  output[["narr_info_user"]] <- entry
  output[["narr_info_admin"]] <- administrator
  output[["narr_info_date"]] <- date
  output[["narr_info_study"]] <- study
  output[["narr_info_timepoint"]] <- timepoint


#' Process Multiple Cinderella Narratives
#' Given a dataframe with filepaths and metadata, use it to process multiple
#' narrative samples. This will convert all files into Redcap-readable form.
#' @param pathdf A dataframe with the filepaths and relevant metadata
#' @param record_id_col Defaults to record_id
#' @param administrator Character, researcher NetID
#' @param entry Person doing data entry
#' @return A dataframe with multiple narrative samples ready to be imported
#' into redcap
#' @export
process_multi_cinderella <- function(pathdf,
                                     entry = NA,
                                     administrator = NA,
                                     record_id_col = "record_id") {
  if (colnames(pathdf) != c("filepath", "record_id", "instance")) {
    stop("Error: Malformed path data frame. Must have columns filepath, record_id, instance")
      function(filepath, record_id, instance) {
          record_id = record_id,
          entry = entry,
          instance = instance,
          administrator = administrator,
          record_id_col = record_id_col
tsostarics/anrlab documentation built on Dec. 13, 2021, 10:11 a.m.