From a5c08ef36a9d2d17b50dcffb1f9fb07d2b6ebfbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Hejtm=C3=A1nek?= Date: Tue, 10 Sep 2019 21:55:47 +0200 Subject: [PATCH] closes #20 and addresses partially the #19 --- R/eyer-loading.R | 43 ++++++++++++++++++++--------------- tests/testthat/test-loading.R | 2 +- vignettes/main.Rmd | 9 ++++---- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/R/eyer-loading.R b/R/eyer-loading.R index d6c315d..bb3cea7 100644 --- a/R/eyer-loading.R +++ b/R/eyer-loading.R @@ -1,20 +1,26 @@ #' Loads data from a folder. Loads preprocessed events, gaze and fixations files #' #' @description eyer allows saving of preprocessed data with the function `eyer_save()` -#' this function then allows to load directly those prepared files into its structure +#' this function then allows to load directly those prepared files into its structure. This function +#' returns NULL if there is no [name]_eyer_info.json file found in the directory. #' #' @param folder what directory should be searcher? Not recursive +#' @param name name of the files to find in case there are multiple saved eyer objects in the folder #' -#' @return EyerObject with filled in data +#' @return EyerObject with filled in data or NULL if no files are found #' @export #' #' @examples -load_eyer_data <- function(folder, silent = T, ...){ +load_eyer_data <- function(folder, name = "", ...){ obj <- EyerObject() #checks if there are already computed files - ls_filepaths <- find_preprocessed_files(folder) - #if (override) delete_preprocessed_files(ls_filepaths) - obj <- load_preprocessed_files(obj, ls_filepaths, silent = silent) + ls_filepaths <- find_preprocessed_files(folder, name) + #if (overrĂșide) delete_preprocessed_files(ls_filepaths) + if(is.null(ls_filepaths$info)){ + warning("There are no info files of name ", name, " in folder", folder, "Returning NULL") + return(NULL) + } + obj <- load_preprocessed_files(obj, ls_filepaths) return(obj) } @@ -24,23 +30,24 @@ load_eyer_data <- function(folder, silent = T, ...){ #' #' @return list with "fixations" and "events" fileds containing filepaths #' @examples -find_preprocessed_files <- function(folder){ +find_preprocessed_files <- function(folder, name = ""){ #actually search for it ls <- list() for(field in DATA_FIELDS){ - preprocessed_ptr <- paste0("_eyer_", field) + preprocessed_ptr <- paste0(name, "_eyer_", field) ls[[field]] <- find_file_or_null(folder, preprocessed_ptr) } - ls$info <- find_file_or_null(folder, "_eyer_info.json") + info_ptr <- paste0(name, "_eyer_info.json") + ls$info <- find_file_or_null(folder, info_ptr) return(ls) } #' @param folder where to look #' @param ptr pattern to search for #' @noRd -find_file_or_null <- function(folder, ptr, allow_multiples = F){ +find_file_or_null <- function(folder, ptr, allow_multiples = FALSE){ ptr <- paste("*", ptr, sep = "") - filepath <- list.files(folder, pattern = ptr, full.names = T) + filepath <- list.files(folder, pattern = ptr, full.names = TRUE) if(length(filepath) == 0) return(NULL) if(length(filepath) == 1) return(filepath) if(allow_multiples){ @@ -50,27 +57,27 @@ find_file_or_null <- function(folder, ptr, allow_multiples = F){ } } -load_preprocessed_files <- function(obj, ls_filepaths, silent = F){ +load_preprocessed_files <- function(obj, ls_filepaths){ for(field in c("fixations", "gaze", "diameter", "events")){ if(!is.null(ls_filepaths[[field]])){ - if(!silent) cat("Loading preprocessed", field, "log at", ls_filepaths[[field]], "\n") - obj$data[[field]] <- read.table(ls_filepaths[[field]], sep = ";", header = T) + message("Loading preprocessed ", field, " log at ", ls_filepaths[[field]]) + obj$data[[field]] <- read.table(ls_filepaths[[field]], sep = ";", header = TRUE) } } if(!is.null(ls_filepaths$info)){ - if(!silent) cat("Loading preprocessed info log", ls_filepaths$gaze) - obj$info <- jsonlite::read_json(ls_filepaths$info, simplifyVector = T) + message("Loading preprocessed info log ", ls_filepaths$gaze) + obj$info <- jsonlite::read_json(ls_filepaths$info, simplifyVector = TRUE) } return(obj) } delete_preprocessed_files <- function(ls_filepaths){ if(file.exists(ls_filepaths$fixations)) { - cat("Removing preprocessed fixations log", ls_filepaths$fixations) + message("Removing preprocessed fixations log ", ls_filepaths$fixations) file.remove(ls_filepaths$fixations) } if(file.exists(ls_filepaths$events)) { - cat("Removing preprocessed events log", ls_filepaths$events) + message("Removing preprocessed events log ", ls_filepaths$events) file.remove(ls_filepaths$events) } } diff --git a/tests/testthat/test-loading.R b/tests/testthat/test-loading.R index 3347ab2..3626eff 100644 --- a/tests/testthat/test-loading.R +++ b/tests/testthat/test-loading.R @@ -4,7 +4,7 @@ test_folder <- system.file("extdata", package = "eyer") FIXATIONS_N_ROW <- 378 test_that("Can load preprocessed data", { - expect_silent(eye <- load_eyer_data(test_folder)) + expect_message(eye <- load_eyer_data(test_folder)) expect_s3_class(eye, "eyer") expect_equal(FIXATIONS_N_ROW, nrow(eye$data$fixations)) }) diff --git a/vignettes/main.Rmd b/vignettes/main.Rmd index 043edfd..deda663 100644 --- a/vignettes/main.Rmd +++ b/vignettes/main.Rmd @@ -5,9 +5,8 @@ date: "`r Sys.Date()`" output: html_document --- -```{r setup, include = FALSE} -knitr::opts_chunk$set( - collapse = TRUE, - comment = "#>" -) +Eyer allows for saving data in a predefined structure and then loading them from passed folder + +```{r} + ```