Skip to content

Commit

Permalink
Generators can now read .qs files
Browse files Browse the repository at this point in the history
  • Loading branch information
japilo committed Apr 18, 2024
1 parent b075235 commit bc1aae6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
22 changes: 16 additions & 6 deletions R/Generator.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
#'
#' @importFrom R6 R6Class
#' @importFrom metRology qtri
#' @importFrom qs qread
#' @include SpatialModel.R
#' @include GenerativeTemplate.R
#' @export Generator
Expand Down Expand Up @@ -254,21 +255,28 @@ Generator <- R6Class("Generator",
},

#' @description
#' Adds a file template for reading raster/RData(RDS)/CSV files for a given model attribute.
#' Adds a file template for reading raster/RData(RDS)/CSV files for a given
#' model attribute.
#' @param param Name of model attribute to be read from a file.
#' @param path_template Template string for the file path with placeholders (see \code{\link{sprintf}}) for simulation sample parameters.
#' @param path_params Array of the names of the simulation sample parameters to be substituted (in order) into the path template.
#' @param file_type File type raster \emph{"GRD"} (default), \emph{"TIF"}, \emph{"RData/RDS"} or \emph{"CSV"} to be read.
#' @param path_template Template string for the file path with placeholders
#' (see \code{\link{sprintf}}) for simulation sample parameters.
#' @param path_params Array of the names of the simulation sample
#' parameters to be substituted (in order) into the path template.
#' @param file_type File type raster \emph{"GRD"} (default), \emph{"TIF"},
#' \emph{"RData/RDS"}, \emph{"QS"}, or \emph{"CSV"} to be read.
add_file_template = function(param, path_template, path_params = c(), file_type = "GRD") {
if (is.character(param) && is.character(path_template)) {
if (length(path_params) == (length(strsplit(paste0("#", path_template, "#"), "%s")[[1]]) - 1)) {
self$file_templates[[param]] <- list()
self$file_templates[[param]]$path_template <- path_template
self$file_templates[[param]]$path_params <- c(path_params)
if (toupper(file_type) == "GRD" || toupper(file_type) == "RDS" || toupper(file_type) == "CSV" || toupper(file_type) == "TIF") {
if (toupper(file_type) == "GRD" || toupper(file_type) == "RDS" ||
toupper(file_type) == "CSV" || toupper(file_type) == "TIF" ||
toupper(file_type) == "QS"
) {
self$file_templates[[param]]$file_type <- toupper(file_type)
} else {
stop("The file type should be GRD (raster), TIF (raster), RDS, or CSV", call. = FALSE)
stop("The file type should be GRD (raster), TIF (raster), RDS, QS, or CSV", call. = FALSE)
}
} else {
stop("Ensure the path template contains a corresponding %s for each path parameter", call. = FALSE)
Expand Down Expand Up @@ -383,6 +391,8 @@ Generator <- R6Class("Generator",
value_list[[param]] <- utils::read.csv(file = file_path)
} else if (self$file_templates[[param]]$file_type == "RDS") { # RDS (RData)
value_list[[param]] <- readRDS(file = file_path)
} else if (self$file_templates[[param]]$file_type == "QS") {
value_list[[param]] <- qread(file = file_path)
} else { # raster
value_list[[param]] <- raster::brick(file_path)
}
Expand Down
1 change: 1 addition & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ knitr::opts_chunk$set(

<!-- badges: start -->
[![Paper_doi](https://img.shields.io/badge/doi-10.1111/2041--210X.13720-orange.svg)](https://doi.org/10.1111/2041-210X.13720)
[![CRAN_version](https://www.r-pkg.org/badges/version/poems)](https://cran.r-project.org/package=poems)
[![Codecov test coverage](https://codecov.io/gh/GlobalEcologyLab/poems/branch/main/graph/badge.svg)](https://app.codecov.io/gh/GlobalEcologyLab/poems?branch=main)
[![Last commit](https://img.shields.io/github/last-commit/GlobalEcologyLab/poems.svg)](https://github.com/GlobalEcologyLab/poems/commits/main)
[![R-CMD-check](https://github.com/GlobalEcologyLab/poems/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/GlobalEcologyLab/poems/actions/workflows/R-CMD-check.yaml)
Expand Down

0 comments on commit bc1aae6

Please sign in to comment.