Skip to content

Commit

Permalink
closes #20 and addresses partially the #19
Browse files Browse the repository at this point in the history
  • Loading branch information
hejtmy committed Sep 10, 2019
1 parent b5cdb9b commit a5c08ef
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
43 changes: 25 additions & 18 deletions R/eyer-loading.R
Original file line number Diff line number Diff line change
@@ -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)
}

Expand All @@ -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){
Expand All @@ -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)
}
}
2 changes: 1 addition & 1 deletion tests/testthat/test-loading.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
Expand Down
9 changes: 4 additions & 5 deletions vignettes/main.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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}
```

0 comments on commit a5c08ef

Please sign in to comment.