R/file_IO.R

Defines functions read_results load_DAISIE_data load_DAISIE_results

Documented in load_DAISIE_data load_DAISIE_results read_results

#' @title Read generic saved results
#'
#' @param project_name String containing project name as returned by
#'  \code{utilSIE::get_project_name()}
#'
#' @author Giovanni Laudanno
#' @description Read saved results
#' @return results
read_results <- function(project_name = utilSIE::get_project_name()) {
  project_folder <- utilSIE::get_project_folder(project_name)
  if (is.null(project_folder)) {
    if (.Platform$OS.type == "windows") {
      project_folder <- system.file(
        "extdata",
        package = utilSIE::get_project_name()
      )
    }
  }
  if (!dir.exists(project_folder)) {
    stop("This directory does not exist")
  }
  if (length(list.files(project_folder)) == 0) {
    stop(paste0(project_folder, " is empty."))
  }
  dir_results <- file.path(project_folder, "results")
  files_results <- c(
    list.files(dir_results, pattern = ".txt"),
    list.files(dir_results, pattern = ".csv")
  )
  if (length(files_results) > 0) {
    if (length(files_results) == 0) {
      stop(paste0(dir_results, " is empty."))
    }
    all_results <- data.frame()
    for (file_results in files_results) {
      x <- utils::read.csv(
        file.path(
          dir_results,
          file_results
        )
      )
      if (tools::file_ext(file_results) == "txt") {
        x <- x[, -1]
      }
      all_results <- rbind(all_results, x)
    }
    return(all_results)
  } else {
    return(NULL)
  }
}

#' Title
#'
#' @param file_name String with file name to be loaded.
#' @param scenario String with
#'
#' @return DAISIE simulation objects
#' @export
#'
load_DAISIE_data <- function(file_name = NULL, scenario = NULL) { # nolint
  testit::assert(is.character(file_name) || is.null(file_name))
  testit::assert(is.character(scenario) || is.null(scenario))


  project_name <- utilSIE::get_project_name()
  project_folder <- utilSIE::get_project_folder(project_name)
  platform <- .Platform$OS.type

  # Get data folder
  if (platform == "windows") {
    if (!is.null(scenario)) {
      data_folder <- local_data_folder <- file.path(
        project_folder,
        "data",
        scenario
      )
    } else {
      data_folder <- local_data_folder <- file.path(
        project_folder,
        "data"
      )
    }
    testit::assert(dir.exists(data_folder))
  } else {
    data_folder <- file.path(utilSIE::get_project_name(), "data")
  }

  out <- NULL # nolint; keep R CMD CHECK happy

  # Load file(s)
  if (is.null(file_name)) {
    files <- list.files(data_folder)
    for (file in seq_along(files)) {
      load(file = file.path(local_data_folder, files[file]))
      assign(paste0("output_", files[file]), out) # nolint
      assign(paste0("args_", files[[file]]), args) # nolint
    }
    # Simulation output when file name is unknown
    simulations_output <- mget(ls(pattern = "output"))
  } else {

    load(file = file.path(local_data_folder, file_name))

    # Simulation output when file name is known
    simulations_output <- mget(ls(pattern = "out"))
  }

  args_output <- mget(ls(pattern = "args"))
  return(list(simulations_output, args_output))
}


#' Load DAISIE results from file
#'
#' @param sim_file_name String with name of simulation file to load
#' @param ontogeny Boolean stating if ontogeny scenario is used
#'
#' @return Loaded
#' @export
#'
#' @examples
#' \dontrun{
#' load_DAISIE_results(sim_file_name = "sim_file.RData", ontogeny = TRUE)
#' }
load_DAISIE_results <- function(sim_file_name, ontogeny) { # nolint
  project_name <- utilSIE::get_project_name()
  project_folder <- utilSIE::get_project_folder(project_name)

  platform <- .Platform$OS.type

  if (ontogeny) {
    ont_path <- "ont"
  } else {
    ont_path <- "no_ont"
  }

  if (platform == "windows") {
    results_folder <- file.path(project_folder, "results", ont_path)
    testit::assert(dir.exists(results_folder))
  } else {
    results_folder <- file.path(
      utilSIE::get_project_name(),
      "results",
      ont_path
    )
  }

  result_files <- list.files(results_folder)
  sim_file_name <- sub( '(?<=.{10})', ' - ', sim_file_name, perl = TRUE ) # temp extra spaces
  match_sims_to_load <- paste0("res- ", substring(sim_file_name, 5)) # temp added space
  files_to_load <-
    result_files[grepl(
      tools::file_path_sans_ext(match_sims_to_load),
      result_files
    )]
  final_list <- list()
  out_results <- NULL # nolint; Keep R CMD CHECK happy
  for (file in seq_along(files_to_load)) {
    load(file.path(results_folder, files_to_load[file]))
    assign(paste("result", file, sep = "_"), out_results) # nolint
    final_list <- rbind(
      final_list,
      do.call(rbind.data.frame, get(paste("result", file, sep = "_")))
    )
  }

  return(final_list)
}
Neves-P/utilSIE documentation built on Nov. 20, 2019, 7 a.m.