# ---------- DESCRIPTION ---------- # Read data from Psytoolkit # ---------- SETUP ---------- library(magrittr) library(data.table) library(stringr) library(readr) # Ensure that relative paths start from the same directory as this script rstudioapi::getActiveDocumentContext()$path %>% dirname %>% setwd # ---------- FUNCTIONS ---------- # Read PsyToolkit experiment files readPsyFile <- function(expName) { list.files("raw_data", expName, full.names = TRUE) %>% lapply(function(x) fread(x) %>% .[, pp := x]) %>% rbindlist() } # Try to numerify each column numerify <- function(X) { tryNumeric <- function(y) tryCatch(expr = {as.numeric(y)}, warning = function(cond) y) X[, (names(X)) := lapply(.SD, tryNumeric)] } # Left Outter Join LOT <- function(X = NULL, Y = NULL, onCol = NULL) { giveExemple <- is.null(X) & is.null(Y) & is.null(onCol) if (giveExemple) { cat("\nExemple:") cat("\n>X\n") X <- data.table(id = 1:5, L = letters[1:5]) %T>% print cat("\n>Y\n") Y <- data.table(id = 3:5, L = c(NA, "g", "h"), N = c(10, NA, 12)) %T>% print onCol <- "id" cat('\nLOT(X, Y, "id")\n') } n <- names(Y) X[Y, (n) := mget(paste0("i.", n)), on = onCol] if (giveExemple) {cat(">X\n"); print(X)} } # ---------- READ FILES ---------- # Vaast VAAST <- readPsyFile("VAAST_words(.*?)txt") setnames(VAAST, names(VAAST), c("BlockOrder","BlockName","Stimuli", "Category","RandomFix", "FirstKey","Movement","ACC","RT","pp")) VAAST[, pp := str_match(pp, ".data\\.(.*?).txt") %>% .[,2]] # Metadata .files <- list.files("raw_data/", pattern = "s\\.(.*?).txt", full.names = TRUE) M <- data.table(pp = .files) for (i in .files) { .str <- read_file(i) M[pp %in% i, screenWidth := str_match(.str, "screen_width: (.*?)\n")[2]] M[pp %in% i, screenHeight := str_match(.str, "screen_height: (.*?)\n")[2]] } M[, pp := str_match(pp, "s\\.(.*?).txt") %>% .[,2]] # Survey # (WARNING: open the data file and register as csv with ";" as sep) S <- fread("raw_data/data.csv")[, c(1:9)] setnames(S, names(S), c("pp","NameFile","language","gender","age","comment","TIME_start","TIME_end")) S[, pp := str_match(pp, "s.(.*?).txt") %>% .[,2]] # Merge dataframe LOT(S, M, "pp") LOT(VAAST, S, "pp") # ---------- WRITE FILES ---------- fwrite(VAAST, "raw_data/dataVAAST.csv") save(S, VAAST, file = "dataVAAST.RData")