Skip to content

Commit

Permalink
Merge pull request #43 from brownag/subst_opt
Browse files Browse the repository at this point in the history
`tar_format()` functions: Use `substitute()` into function body rather than `body<-`
  • Loading branch information
njtierney authored Mar 19, 2024
2 parents 85e5eef + 15e798c commit 8191107
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
14 changes: 4 additions & 10 deletions R/tar-terra-rast.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,15 @@ create_format_terra_raster <- function(filetype, gdal, ...) {

gdal <- gdal %||% geotargets_option_get("gdal.raster.creation_options")

# NOTE: Option getting functions are set in the .write_terra_raster function template
# to resolve issue with body<- not working in some evaluation contexts ({covr}).
# TODO: It should be fine to have filetype and gdal as NULL
.write_terra_raster <- function(object, path) {
.write_terra_raster <- eval(substitute(function(object, path) {
terra::writeRaster(
object,
path,
filetype = geotargets::geotargets_option_get("gdal.raster.driver"),
filetype = filetype,
overwrite = TRUE,
gdal = geotargets::geotargets_option_get("gdal.raster.creation_options")
gdal = gdal
)
}

body(.write_terra_raster)[[2]][["filetype"]] <- filetype
body(.write_terra_raster)[[2]][["gdal"]] <- gdal
}, list(filetype = filetype, gdal = gdal)))

targets::tar_format(
read = function(path) terra::rast(path),
Expand Down
17 changes: 7 additions & 10 deletions R/tar-terra-vect.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,15 @@ create_format_terra_vect <- function(filetype, options, ...) {

filetype <- match.arg(filetype, drv$name)

.write_terra_vector <- function(object, path) {
.write_terra_vector <- eval(substitute(function(object, path) {
terra::writeVector(
object,
path,
filetype = NULL,
filetype = filetype,
overwrite = TRUE,
options = NULL
options = options
)
}
body(.write_terra_vector)[[2]][["filetype"]] <- filetype
body(.write_terra_vector)[[2]][["options"]] <- options
}, list(filetype = filetype, options = options)))

targets::tar_format(
read = function(path) terra::vect(path),
Expand All @@ -158,17 +156,16 @@ create_format_terra_vect_shz <- function(options, ...) {
stop("package 'terra' is required", call. = FALSE)
}

.write_terra_vector <- function(object, path) {
.write_terra_vector <- eval(substitute(function(object, path) {
terra::writeVector(
x = object,
filename = paste0(path, ".shz"),
filetype = "ESRI Shapefile",
overwrite = TRUE,
options = NULL
options = options
)
file.rename(paste0(path, ".shz"), path)
}
body(.write_terra_vector)[[2]][["options"]] <- options
}, list(options = options)))

targets::tar_format(
read = function(path) terra::vect(paste0("/vsizip/{", path, "}")),
Expand Down

0 comments on commit 8191107

Please sign in to comment.