From 06b1924a92426c3a28741417a76a7303c30025ed Mon Sep 17 00:00:00 2001 From: njtierney Date: Fri, 17 Jan 2025 13:35:19 +1100 Subject: [PATCH] use linter, based off of targets linter --- .lintr | 8 ++ R/geotargets-option.R | 9 +- R/release_bullets.R | 5 +- R/tar-stars.R | 152 +++++++++++++----------- R/tar-terra-rast.R | 65 +++++----- R/tar-terra-sprc.R | 141 +++++++++++----------- R/tar-terra-vect.R | 56 +++++---- R/tar_terra_tiles.R | 32 ++--- R/utils.R | 4 +- man/tar_stars.Rd | 10 +- man/tar_terra_rast.Rd | 6 +- man/tar_terra_sds.Rd | 6 +- man/tar_terra_sprc.Rd | 6 +- man/tar_terra_tiles.Rd | 6 +- man/tar_terra_vect.Rd | 6 +- man/tile_helpers.Rd | 6 +- tests/testthat/test-geotargets-option.R | 10 +- tests/testthat/test-tar-stars.R | 22 +++- tests/testthat/test-tar-terra-rast.R | 21 +++- tests/testthat/test-tar-terra-sprc.R | 8 +- tests/testthat/test-tar-terra-tiles.R | 6 +- tests/testthat/test-tar-terra-vect.R | 9 +- 22 files changed, 333 insertions(+), 261 deletions(-) create mode 100644 .lintr diff --git a/.lintr b/.lintr new file mode 100644 index 0000000..6e13610 --- /dev/null +++ b/.lintr @@ -0,0 +1,8 @@ +linters: linters_with_defaults( + infix_spaces_linter = NULL, + object_length_linter = NULL, + object_name_linter = NULL, + object_usage_linter = NULL, + indentation_linter = NULL) +exclusions: list("inst/notes/preserver_metadata_benchmarking.Qmd") +encoding: "UTF-8" diff --git a/R/geotargets-option.R b/R/geotargets-option.R index c9bada6..6d60f88 100644 --- a/R/geotargets-option.R +++ b/R/geotargets-option.R @@ -64,15 +64,16 @@ geotargets_option_set <- function(gdal_raster_driver = NULL, gdal_vector_driver = NULL, gdal_vector_creation_options = NULL, terra_preserve_metadata = NULL) { - # TODO do this programmatically with formals() or something? `options()` also accepts a named list + # TODO do this programmatically with formals() or something? + # `options()` also accepts a named list options( - "geotargets.gdal.raster.driver" = gdal_raster_driver %||% + "geotargets.gdal.raster.driver" = gdal_raster_driver %||% # nolint geotargets_option_get("gdal.raster.driver"), - "geotargets.gdal.raster.creation.options" = gdal_raster_creation_options %||% + "geotargets.gdal.raster.creation.options" = gdal_raster_creation_options %||% # nolint geotargets_option_get("gdal.raster.creation.options"), "geotargets.gdal.vector.driver" = gdal_vector_driver %||% geotargets_option_get("gdal.vector.driver"), - "geotargets.gdal.vector.creation.options" = gdal_vector_creation_options %||% + "geotargets.gdal.vector.creation.options" = gdal_vector_creation_options %||% #nolint geotargets_option_get("gdal.vector.creation.options"), "geotargets.terra.preserve.metadata" = terra_preserve_metadata %||% geotargets_option_get("terra.preserve.metadata") diff --git a/R/release_bullets.R b/R/release_bullets.R index 1b35807..8a493ab 100644 --- a/R/release_bullets.R +++ b/R/release_bullets.R @@ -1,6 +1,7 @@ -# see ?usethis::use_release_issue() +# see ?usethis::use_release_issue() #nolint release_bullets <- function() { c( - "update CITATION.cff with `cffr::cff_write(dependencies = FALSE)` (after incrementing version)" + "update CITATION.cff with `cffr::cff_write(dependencies = FALSE)` + (after incrementing version)" ) } diff --git a/R/tar-stars.R b/R/tar-stars.R index 9f18b33..f4c7e67 100644 --- a/R/tar-stars.R +++ b/R/tar-stars.R @@ -8,9 +8,9 @@ #' `tar_stars()` has the same limitations as [stars::write_stars()], so use with #' caution. #' -#' @param name Symbol, name of the target. A target name must be a valid name for -#' a symbol in R, and it must not start with a dot. See [targets::tar_target()] -#' for more information. +#' @param name Symbol, name of the target. A target name must be a valid name +#' for a symbol in R, and it must not start with a dot. See +#' [targets::tar_target()] for more information. #' @param command R code to run the target. #' @param pattern Code to define a dynamic branching pattern for a target. See #' [targets::tar_target()] for more information. @@ -45,7 +45,9 @@ #' list( #' geotargets::tar_stars( #' stars_example, -#' stars::read_stars(system.file("tif", "olinda_dem_utm25s.tif", package = "stars")) +#' stars::read_stars( +#' system.file("tif", "olinda_dem_utm25s.tif", package = "stars") +#' ) #' ) #' ) #' }) @@ -53,29 +55,31 @@ #' x <- targets::tar_read(stars_example) #' }) #' } -tar_stars <- function(name, - command, - pattern = NULL, - proxy = FALSE, - mdim = FALSE, - ncdf = FALSE, - driver = geotargets_option_get("gdal.raster.driver"), - options = geotargets_option_get("gdal.raster.creation.options"), - ..., - tidy_eval = targets::tar_option_get("tidy_eval"), - packages = targets::tar_option_get("packages"), - library = targets::tar_option_get("library"), - repository = targets::tar_option_get("repository"), - error = targets::tar_option_get("error"), - memory = targets::tar_option_get("memory"), - garbage_collection = targets::tar_option_get("garbage_collection"), - deployment = targets::tar_option_get("deployment"), - priority = targets::tar_option_get("priority"), - resources = targets::tar_option_get("resources"), - storage = targets::tar_option_get("storage"), - retrieval = targets::tar_option_get("retrieval"), - cue = targets::tar_option_get("cue"), - description = targets::tar_option_get("description")) { +tar_stars <- function( + name, + command, + pattern = NULL, + proxy = FALSE, + mdim = FALSE, + ncdf = FALSE, + driver = geotargets_option_get("gdal.raster.driver"), + options = geotargets_option_get("gdal.raster.creation.options"), + ..., + tidy_eval = targets::tar_option_get("tidy_eval"), + packages = targets::tar_option_get("packages"), + library = targets::tar_option_get("library"), + repository = targets::tar_option_get("repository"), + error = targets::tar_option_get("error"), + memory = targets::tar_option_get("memory"), + garbage_collection = targets::tar_option_get("garbage_collection"), + deployment = targets::tar_option_get("deployment"), + priority = targets::tar_option_get("priority"), + resources = targets::tar_option_get("resources"), + storage = targets::tar_option_get("storage"), + retrieval = targets::tar_option_get("retrieval"), + cue = targets::tar_option_get("cue"), + description = targets::tar_option_get("description") +) { check_pkg_installed("stars") if (ncdf) { check_pkg_installed("ncmeta") @@ -124,28 +128,30 @@ tar_stars <- function(name, #' @export #' @rdname tar_stars -tar_stars_proxy <- function(name, - command, - pattern = NULL, - mdim = FALSE, - ncdf = FALSE, - driver = geotargets_option_get("gdal.raster.driver"), - options = geotargets_option_get("gdal.raster.creation.options"), - ..., - tidy_eval = targets::tar_option_get("tidy_eval"), - packages = targets::tar_option_get("packages"), - library = targets::tar_option_get("library"), - repository = targets::tar_option_get("repository"), - error = targets::tar_option_get("error"), - memory = targets::tar_option_get("memory"), - garbage_collection = targets::tar_option_get("garbage_collection"), - deployment = targets::tar_option_get("deployment"), - priority = targets::tar_option_get("priority"), - resources = targets::tar_option_get("resources"), - storage = targets::tar_option_get("storage"), - retrieval = targets::tar_option_get("retrieval"), - cue = targets::tar_option_get("cue"), - description = targets::tar_option_get("description")) { +tar_stars_proxy <- function( + name, + command, + pattern = NULL, + mdim = FALSE, + ncdf = FALSE, + driver = geotargets_option_get("gdal.raster.driver"), + options = geotargets_option_get("gdal.raster.creation.options"), + ..., + tidy_eval = targets::tar_option_get("tidy_eval"), + packages = targets::tar_option_get("packages"), + library = targets::tar_option_get("library"), + repository = targets::tar_option_get("repository"), + error = targets::tar_option_get("error"), + memory = targets::tar_option_get("memory"), + garbage_collection = targets::tar_option_get("garbage_collection"), + deployment = targets::tar_option_get("deployment"), + priority = targets::tar_option_get("priority"), + resources = targets::tar_option_get("resources"), + storage = targets::tar_option_get("storage"), + retrieval = targets::tar_option_get("retrieval"), + cue = targets::tar_option_get("cue"), + description = targets::tar_option_get("description") +) { check_pkg_installed("stars") if (ncdf) { check_pkg_installed("ncmeta") @@ -196,29 +202,31 @@ tar_stars_proxy <- function(name, #' tar_stars method with no tidy eval etc. #' @noRd -tar_stars_raw <- function(name, - command, - pattern = NULL, - proxy, - mdim = FALSE, - ncdf = FALSE, - driver = geotargets_option_get("gdal.raster.driver"), - options = geotargets_option_get("gdal.raster.creation.options"), - ..., - tidy_eval = targets::tar_option_get("tidy_eval"), - packages = targets::tar_option_get("packages"), - library = targets::tar_option_get("library"), - repository = targets::tar_option_get("repository"), - error = targets::tar_option_get("error"), - memory = targets::tar_option_get("memory"), - garbage_collection = targets::tar_option_get("garbage_collection"), - deployment = targets::tar_option_get("deployment"), - priority = targets::tar_option_get("priority"), - resources = targets::tar_option_get("resources"), - storage = targets::tar_option_get("storage"), - retrieval = targets::tar_option_get("retrieval"), - cue = targets::tar_option_get("cue"), - description = targets::tar_option_get("description")) { +tar_stars_raw <- function( + name, + command, + pattern = NULL, + proxy, + mdim = FALSE, + ncdf = FALSE, + driver = geotargets_option_get("gdal.raster.driver"), + options = geotargets_option_get("gdal.raster.creation.options"), + ..., + tidy_eval = targets::tar_option_get("tidy_eval"), + packages = targets::tar_option_get("packages"), + library = targets::tar_option_get("library"), + repository = targets::tar_option_get("repository"), + error = targets::tar_option_get("error"), + memory = targets::tar_option_get("memory"), + garbage_collection = targets::tar_option_get("garbage_collection"), + deployment = targets::tar_option_get("deployment"), + priority = targets::tar_option_get("priority"), + resources = targets::tar_option_get("resources"), + storage = targets::tar_option_get("storage"), + retrieval = targets::tar_option_get("retrieval"), + cue = targets::tar_option_get("cue"), + description = targets::tar_option_get("description") +) { driver <- driver %||% "GTiff" options <- options %||% character(0) diff --git a/R/tar-terra-rast.R b/R/tar-terra-rast.R index 3c223f4..c3696ae 100644 --- a/R/tar-terra-rast.R +++ b/R/tar-terra-rast.R @@ -12,9 +12,9 @@ #' the `SpatRaster` object itself. #' #' -#' @param name Symbol, name of the target. A target -#' name must be a valid name for a symbol in R, and it -#' must not start with a dot. See [targets::tar_target()] for more information. +#' @param name Symbol, name of the target. A target name must be a valid name +#' for a symbol in R, and it must not start with a dot. See +#' [targets::tar_target()] for more information. #' @param command R code to run the target. #' @param pattern Code to define a dynamic branching pattern for a target. See #' [targets::tar_target()] for more information. @@ -56,27 +56,29 @@ #' x <- targets::tar_read(terra_rast_example) #' }) #' } -tar_terra_rast <- function(name, - command, - pattern = NULL, - filetype = geotargets_option_get("gdal.raster.driver"), - gdal = geotargets_option_get("gdal.raster.creation.options"), - preserve_metadata = geotargets_option_get("terra.preserve.metadata"), - ..., - tidy_eval = targets::tar_option_get("tidy_eval"), - packages = targets::tar_option_get("packages"), - library = targets::tar_option_get("library"), - repository = targets::tar_option_get("repository"), - error = targets::tar_option_get("error"), - memory = targets::tar_option_get("memory"), - garbage_collection = targets::tar_option_get("garbage_collection"), - deployment = targets::tar_option_get("deployment"), - priority = targets::tar_option_get("priority"), - resources = targets::tar_option_get("resources"), - storage = targets::tar_option_get("storage"), - retrieval = targets::tar_option_get("retrieval"), - cue = targets::tar_option_get("cue"), - description = targets::tar_option_get("description")) { +tar_terra_rast <- function( + name, + command, + pattern = NULL, + filetype = geotargets_option_get("gdal.raster.driver"), + gdal = geotargets_option_get("gdal.raster.creation.options"), + preserve_metadata = geotargets_option_get("terra.preserve.metadata"), + ..., + tidy_eval = targets::tar_option_get("tidy_eval"), + packages = targets::tar_option_get("packages"), + library = targets::tar_option_get("library"), + repository = targets::tar_option_get("repository"), + error = targets::tar_option_get("error"), + memory = targets::tar_option_get("memory"), + garbage_collection = targets::tar_option_get("garbage_collection"), + deployment = targets::tar_option_get("deployment"), + priority = targets::tar_option_get("priority"), + resources = targets::tar_option_get("resources"), + storage = targets::tar_option_get("storage"), + retrieval = targets::tar_option_get("retrieval"), + cue = targets::tar_option_get("cue"), + description = targets::tar_option_get("description") +) { filetype <- filetype %||% "GTiff" # check that filetype option is available @@ -114,10 +116,18 @@ tar_terra_rast <- function(name, library = library, format = targets::tar_format( read = tar_rast_read(preserve_metadata = preserve_metadata), - write = tar_rast_write(filetype = filetype, gdal = gdal, preserve_metadata = preserve_metadata), + write = tar_rast_write( + filetype = filetype, + gdal = gdal, + preserve_metadata = preserve_metadata + ), marshal = function(object) terra::wrap(object), unmarshal = function(object) terra::unwrap(object), - substitute = list(filetype = filetype, gdal = gdal, preserve_metadata = preserve_metadata) + substitute = list( + filetype = filetype, + gdal = gdal, + preserve_metadata = preserve_metadata + ) ), repository = repository, iteration = "list", # only "list" works right now @@ -151,7 +161,8 @@ tar_rast_read <- function(preserve_metadata) { tar_rast_write <- function(filetype, gdal, preserve_metadata) { switch(preserve_metadata, zip = function(object, path) { - # write the raster in a fresh local tempdir() that disappears when function is done + # write the raster in a fresh local tempdir() that disappears when + # function is done tmp <- withr::local_tempdir() raster_tmp_file <- file.path(tmp, basename(path)) zip_tmp_file <- file.path(tmp, "object.zip") diff --git a/R/tar-terra-sprc.R b/R/tar-terra-sprc.R index 5fbf8cd..e1aa5b9 100644 --- a/R/tar-terra-sprc.R +++ b/R/tar-terra-sprc.R @@ -3,9 +3,9 @@ #' Provides a target format for [terra::SpatRasterCollection] objects, #' which have no restriction in the extent or other geometric parameters. #' -#' @param name Symbol, name of the target. A target -#' name must be a valid name for a symbol in R, and it -#' must not start with a dot. See [targets::tar_target()] for more information. +#' @param name Symbol, name of the target. A target name must be a valid name +#' for a symbol in R, and it must not start with a dot. See +#' [targets::tar_target()] for more information. #' @param command R code to run the target. #' @param pattern Code to define a dynamic branching pattern for a target. See #' [targets::tar_target()] for more information. @@ -52,26 +52,28 @@ #' x <- targets::tar_read(raster_elevs) #' }) #' } -tar_terra_sprc <- function(name, - command, - pattern = NULL, - filetype = geotargets_option_get("gdal.raster.driver"), - gdal = geotargets_option_get("gdal.raster.creation.options"), - ..., - tidy_eval = targets::tar_option_get("tidy_eval"), - packages = targets::tar_option_get("packages"), - library = targets::tar_option_get("library"), - repository = targets::tar_option_get("repository"), - error = targets::tar_option_get("error"), - memory = targets::tar_option_get("memory"), - garbage_collection = targets::tar_option_get("garbage_collection"), - deployment = targets::tar_option_get("deployment"), - priority = targets::tar_option_get("priority"), - resources = targets::tar_option_get("resources"), - storage = targets::tar_option_get("storage"), - retrieval = targets::tar_option_get("retrieval"), - cue = targets::tar_option_get("cue"), - description = targets::tar_option_get("description")) { +tar_terra_sprc <- function( + name, + command, + pattern = NULL, + filetype = geotargets_option_get("gdal.raster.driver"), + gdal = geotargets_option_get("gdal.raster.creation.options"), + ..., + tidy_eval = targets::tar_option_get("tidy_eval"), + packages = targets::tar_option_get("packages"), + library = targets::tar_option_get("library"), + repository = targets::tar_option_get("repository"), + error = targets::tar_option_get("error"), + memory = targets::tar_option_get("memory"), + garbage_collection = targets::tar_option_get("garbage_collection"), + deployment = targets::tar_option_get("deployment"), + priority = targets::tar_option_get("priority"), + resources = targets::tar_option_get("resources"), + storage = targets::tar_option_get("storage"), + retrieval = targets::tar_option_get("retrieval"), + cue = targets::tar_option_get("cue"), + description = targets::tar_option_get("description") +) { # ensure that user-passed `resources` doesn't include `custom_format` check_user_resources(resources) @@ -127,9 +129,9 @@ tar_terra_sprc <- function(name, #' Provides a target format for [terra::SpatRasterDataset] objects, #' which hold sub-datasets, each a `SpatRaster` that can have multiple layers. #' -#' @param name Symbol, name of the target. A target -#' name must be a valid name for a symbol in R, and it -#' must not start with a dot. See [targets::tar_target()] for more information. +#' @param name Symbol, name of the target. A target name must be a valid name +#' for a symbol in R, and it must not start with a dot. See +#' [targets::tar_target()] for more information. #' @param command R code to run the target. #' @param pattern Code to define a dynamic branching pattern for a target. See #' [targets::tar_target()] for more information. @@ -173,26 +175,28 @@ tar_terra_sprc <- function(name, #' targets::tar_read(raster_elevs) #' }) #' } -tar_terra_sds <- function(name, - command, - pattern = NULL, - filetype = geotargets_option_get("gdal.raster.driver"), - gdal = geotargets_option_get("gdal.raster.creation.options"), - ..., - tidy_eval = targets::tar_option_get("tidy_eval"), - packages = targets::tar_option_get("packages"), - library = targets::tar_option_get("library"), - repository = targets::tar_option_get("repository"), - error = targets::tar_option_get("error"), - memory = targets::tar_option_get("memory"), - garbage_collection = targets::tar_option_get("garbage_collection"), - deployment = targets::tar_option_get("deployment"), - priority = targets::tar_option_get("priority"), - resources = targets::tar_option_get("resources"), - storage = targets::tar_option_get("storage"), - retrieval = targets::tar_option_get("retrieval"), - cue = targets::tar_option_get("cue"), - description = targets::tar_option_get("description")) { +tar_terra_sds <- function( + name, + command, + pattern = NULL, + filetype = geotargets_option_get("gdal.raster.driver"), + gdal = geotargets_option_get("gdal.raster.creation.options"), + ..., + tidy_eval = targets::tar_option_get("tidy_eval"), + packages = targets::tar_option_get("packages"), + library = targets::tar_option_get("library"), + repository = targets::tar_option_get("repository"), + error = targets::tar_option_get("error"), + memory = targets::tar_option_get("memory"), + garbage_collection = targets::tar_option_get("garbage_collection"), + deployment = targets::tar_option_get("deployment"), + priority = targets::tar_option_get("priority"), + resources = targets::tar_option_get("resources"), + storage = targets::tar_option_get("storage"), + retrieval = targets::tar_option_get("retrieval"), + cue = targets::tar_option_get("cue"), + description = targets::tar_option_get("description") +) { # ensure that user-passed `resources` doesn't include `custom_format` check_user_resources(resources) @@ -246,26 +250,28 @@ tar_terra_sds <- function(name, #' @noRd -tar_terra_collection_raw <- function(name, - command, - type = type, - filetype = geotargets_option_get("gdal.raster.driver"), - gdal = geotargets_option_get("gdal.raster.creation.options"), - pattern = NULL, - packages = targets::tar_option_get("packages"), - library = targets::tar_option_get("library"), - repository = targets::tar_option_get("repository"), - error = targets::tar_option_get("error"), - memory = targets::tar_option_get("memory"), - garbage_collection = targets::tar_option_get("garbage_collection"), - deployment = targets::tar_option_get("deployment"), - priority = targets::tar_option_get("priority"), - resources = targets::tar_option_get("resources"), - storage = targets::tar_option_get("storage"), - retrieval = targets::tar_option_get("retrieval"), - cue = targets::tar_option_get("cue"), - description = targets::tar_option_get("description")) { - targets::tar_target_raw( +tar_terra_collection_raw <- function( + name, + command, + type = type, + filetype = geotargets_option_get("gdal.raster.driver"), + gdal = geotargets_option_get("gdal.raster.creation.options"), + pattern = NULL, + packages = targets::tar_option_get("packages"), + library = targets::tar_option_get("library"), + repository = targets::tar_option_get("repository"), + error = targets::tar_option_get("error"), + memory = targets::tar_option_get("memory"), + garbage_collection = targets::tar_option_get("garbage_collection"), + deployment = targets::tar_option_get("deployment"), + priority = targets::tar_option_get("priority"), + resources = targets::tar_option_get("resources"), + storage = targets::tar_option_get("storage"), + retrieval = targets::tar_option_get("retrieval"), + cue = targets::tar_option_get("cue"), + description = targets::tar_option_get("description") + ) { + targets::tar_target_raw( name = name, command = command, pattern = pattern, @@ -288,8 +294,7 @@ tar_terra_collection_raw <- function(name, # The warning message "[rast] skipped sub-datasets..." # is printed because the return value of writeRaster() # is rast(). In this context it is not - # informative since the read function is sprc(), not - # rast() + # informative since the read function is sprc(), not rast() if (grepl("\\[rast\\] skipped sub-datasets", cnd$message)) { rlang::cnd_muffle(cnd) } diff --git a/R/tar-terra-vect.R b/R/tar-terra-vect.R index 426445a..ee220ed 100644 --- a/R/tar-terra-vect.R +++ b/R/tar-terra-vect.R @@ -11,9 +11,9 @@ #' the target as a geospatial file (specified by `filetype`) rather than saving #' the `SpatVector` object itself. #' -#' @param name Symbol, name of the target. A target -#' name must be a valid name for a symbol in R, and it -#' must not start with a dot. See [targets::tar_target()] for more information. +#' @param name Symbol, name of the target. A target name must be a valid name +#' for a symbol in R, and it must not start with a dot. See +#' [targets::tar_target()] for more information. #' @param command R code to run the target. #' @param pattern Code to define a dynamic branching pattern for a target. See #' [targets::tar_target()] for more information. @@ -57,28 +57,30 @@ #' x <- targets::tar_read(terra_vect_example) #' }) #' } -tar_terra_vect <- function(name, - command, - pattern = NULL, - filetype = geotargets_option_get("gdal.vector.driver"), - gdal = geotargets_option_get("gdal.vector.creation.options"), - ..., - packages = targets::tar_option_get("packages"), - tidy_eval = targets::tar_option_get("tidy_eval"), - library = targets::tar_option_get("library"), - repository = targets::tar_option_get("repository"), - error = targets::tar_option_get("error"), - memory = targets::tar_option_get("memory"), - garbage_collection = targets::tar_option_get("garbage_collection"), - deployment = targets::tar_option_get("deployment"), - priority = targets::tar_option_get("priority"), - resources = targets::tar_option_get("resources"), - storage = targets::tar_option_get("storage"), - retrieval = targets::tar_option_get("retrieval"), - cue = targets::tar_option_get("cue"), - description = targets::tar_option_get("description")) { - filetype <- filetype %||% "GeoJSON" - gdal <- gdal %||% "ENCODING=UTF-8" +tar_terra_vect <- function( + name, + command, + pattern = NULL, + filetype = geotargets_option_get("gdal.vector.driver"), + gdal = geotargets_option_get("gdal.vector.creation.options"), + ..., + packages = targets::tar_option_get("packages"), + tidy_eval = targets::tar_option_get("tidy_eval"), + library = targets::tar_option_get("library"), + repository = targets::tar_option_get("repository"), + error = targets::tar_option_get("error"), + memory = targets::tar_option_get("memory"), + garbage_collection = targets::tar_option_get("garbage_collection"), + deployment = targets::tar_option_get("deployment"), + priority = targets::tar_option_get("priority"), + resources = targets::tar_option_get("resources"), + storage = targets::tar_option_get("storage"), + retrieval = targets::tar_option_get("retrieval"), + cue = targets::tar_option_get("cue"), + description = targets::tar_option_get("description") + ) { + filetype <- filetype %||% "GeoJSON" + gdal <- gdal %||% "ENCODING=UTF-8" # Check that filetype is available drv <- get_gdal_available_driver_list("vector") @@ -123,7 +125,9 @@ tar_terra_vect <- function(name, write = function(object, path) { terra::writeVector( object, - filename = ifelse(filetype == "ESRI Shapefile", paste0(path, ".shz"), path), + filename = ifelse(filetype == "ESRI Shapefile", + yes = paste0(path, ".shz"), + no = path), filetype = filetype, overwrite = TRUE, options = gdal diff --git a/R/tar_terra_tiles.R b/R/tar_terra_tiles.R index 51be0b4..01848e8 100644 --- a/R/tar_terra_tiles.R +++ b/R/tar_terra_tiles.R @@ -3,9 +3,9 @@ #' This target factory is useful when a raster is too large or too high #' resolution to work on in-memory. It can instead be split into tiles that can #' be iterated over using dynamic branching. -#' @param name Symbol, name of the target. A target -#' name must be a valid name for a symbol in R, and it -#' must not start with a dot. See [targets::tar_target()] for more information. +#' @param name Symbol, name of the target. A target name must be a valid name +#' for a symbol in R, and it must not start with a dot. See +#' [targets::tar_target()] for more information. #' @param raster a `SpatRaster` object to be split into tiles. #' @param tile_fun a helper function that returns a list of numeric vectors such #' as [tile_grid()], [tile_n()] or [tile_blocksize] specified in one of the @@ -227,7 +227,9 @@ tar_terra_tiles_raw <- function( #' terra::ext(r2) #' set_window <- function(raster, window) { - raster_out <- c(raster) #forces copying the raster, not just the R object pointing to the same raster in memory + # forces copying the raster, not just the R object pointing to the same + # raster in memory + raster_out <- c(raster) terra::window(raster_out) <- window raster_out } @@ -294,7 +296,11 @@ set_window <- function(raster, window) { #' tar_terra_tiles( #' name = rast_split_2blocks, #' raster = my_map, -#' tile_fun = \(x) tile_blocksize(x, n_blocks_row = 2, n_blocks_col = 1), +#' tile_fun = \(x) tile_blocksize( +#' x, +#' n_blocks_row = 2, +#' n_blocks_col = 1 +#' ), #' description = "Each tile is 2 blocks tall, 1 block wide" #' ) #' tar_terra_tiles( @@ -325,7 +331,7 @@ tile_grid <- function(raster, ncol, nrow) { n_tiles <- seq_len(nrow(tile_ext)) tile_list <- lapply( n_tiles, - \(i) tile_ext[i,] + \(i) tile_ext[i, ] ) tile_list } @@ -336,12 +342,12 @@ tile_blocksize <- function(raster, n_blocks_row = 1, n_blocks_col = 1) { tile_ext <- terra::getTileExtents( raster, - terra::fileBlocksize(raster)[1,] * c(n_blocks_row, n_blocks_col) + terra::fileBlocksize(raster)[1, ] * c(n_blocks_row, n_blocks_col) ) n_tiles <- seq_len(nrow(tile_ext)) tile_list <- lapply( n_tiles, - \(i) tile_ext[i,] + \(i) tile_ext[i, ] ) tile_list } @@ -359,20 +365,16 @@ tile_n <- function(raster, n) { } sq <- sqrt(n) sq_round <- floor(sq) - quotient <- n/sq_round + quotient <- n / sq_round is_even <- rlang::is_integerish(quotient) is_odd <- !is_even if (is_even) { nrow <- sq_round - ncol <- n/nrow + ncol <- n / nrow } if (is_odd) { nrow <- sq_round ncol <- ceiling(quotient) #round up - - # #alternatively, only use rows?? - # nrow <- n - # ncol <- 1 } cli::cli_inform("creating {nrow} * {ncol} = {nrow*ncol} tile extents\n") @@ -390,7 +392,7 @@ tile_n <- function(raster, n) { n_tiles <- seq_len(nrow(tile_ext)) tile_list <- lapply( n_tiles, - \(i) tile_ext[i,] + \(i) tile_ext[i, ] ) tile_list } diff --git a/R/utils.R b/R/utils.R index 1699c8b..489c16c 100644 --- a/R/utils.R +++ b/R/utils.R @@ -34,7 +34,7 @@ check_gdal_shz <- function(min_version = "3.1", } get_gdal_available_driver_list <- function(driver_type) { - # get list of drivers available for writing depending on what the user's GDAL supports + # get list of drivers available for writing based on what user's GDAL supports drv <- terra::gdal(drivers = TRUE) if (utils::packageVersion("terra") > "1.7-74") { drv <- drv[drv[[driver_type]] & grepl("write", drv$can), ] @@ -45,7 +45,7 @@ get_gdal_available_driver_list <- function(driver_type) { } check_user_resources <- function(resources, - call = rlang::caller_env()){ + call = rlang::caller_env()) { if ("custom_format" %in% names(resources)) { cli::cli_abort( message = c( diff --git a/man/tar_stars.Rd b/man/tar_stars.Rd index 5ce20c7..7dd16bd 100644 --- a/man/tar_stars.Rd +++ b/man/tar_stars.Rd @@ -57,9 +57,9 @@ tar_stars_proxy( ) } \arguments{ -\item{name}{Symbol, name of the target. A target name must be a valid name for -a symbol in R, and it must not start with a dot. See \code{\link[targets:tar_target]{targets::tar_target()}} -for more information.} +\item{name}{Symbol, name of the target. A target name must be a valid name +for a symbol in R, and it must not start with a dot. See +\code{\link[targets:tar_target]{targets::tar_target()}} for more information.} \item{command}{R code to run the target.} @@ -276,7 +276,9 @@ if (Sys.getenv("TAR_LONG_EXAMPLES") == "true") { list( geotargets::tar_stars( stars_example, - stars::read_stars(system.file("tif", "olinda_dem_utm25s.tif", package = "stars")) + stars::read_stars( + system.file("tif", "olinda_dem_utm25s.tif", package = "stars") + ) ) ) }) diff --git a/man/tar_terra_rast.Rd b/man/tar_terra_rast.Rd index 4f78792..1a4242b 100644 --- a/man/tar_terra_rast.Rd +++ b/man/tar_terra_rast.Rd @@ -29,9 +29,9 @@ tar_terra_rast( ) } \arguments{ -\item{name}{Symbol, name of the target. A target -name must be a valid name for a symbol in R, and it -must not start with a dot. See \code{\link[targets:tar_target]{targets::tar_target()}} for more information.} +\item{name}{Symbol, name of the target. A target name must be a valid name +for a symbol in R, and it must not start with a dot. See +\code{\link[targets:tar_target]{targets::tar_target()}} for more information.} \item{command}{R code to run the target.} diff --git a/man/tar_terra_sds.Rd b/man/tar_terra_sds.Rd index 9b9bb3c..11d8ec1 100644 --- a/man/tar_terra_sds.Rd +++ b/man/tar_terra_sds.Rd @@ -28,9 +28,9 @@ tar_terra_sds( ) } \arguments{ -\item{name}{Symbol, name of the target. A target -name must be a valid name for a symbol in R, and it -must not start with a dot. See \code{\link[targets:tar_target]{targets::tar_target()}} for more information.} +\item{name}{Symbol, name of the target. A target name must be a valid name +for a symbol in R, and it must not start with a dot. See +\code{\link[targets:tar_target]{targets::tar_target()}} for more information.} \item{command}{R code to run the target.} diff --git a/man/tar_terra_sprc.Rd b/man/tar_terra_sprc.Rd index c414029..973b233 100644 --- a/man/tar_terra_sprc.Rd +++ b/man/tar_terra_sprc.Rd @@ -28,9 +28,9 @@ tar_terra_sprc( ) } \arguments{ -\item{name}{Symbol, name of the target. A target -name must be a valid name for a symbol in R, and it -must not start with a dot. See \code{\link[targets:tar_target]{targets::tar_target()}} for more information.} +\item{name}{Symbol, name of the target. A target name must be a valid name +for a symbol in R, and it must not start with a dot. See +\code{\link[targets:tar_target]{targets::tar_target()}} for more information.} \item{command}{R code to run the target.} diff --git a/man/tar_terra_tiles.Rd b/man/tar_terra_tiles.Rd index b84cc36..4ed7ca2 100644 --- a/man/tar_terra_tiles.Rd +++ b/man/tar_terra_tiles.Rd @@ -27,9 +27,9 @@ tar_terra_tiles( ) } \arguments{ -\item{name}{Symbol, name of the target. A target -name must be a valid name for a symbol in R, and it -must not start with a dot. See \code{\link[targets:tar_target]{targets::tar_target()}} for more information.} +\item{name}{Symbol, name of the target. A target name must be a valid name +for a symbol in R, and it must not start with a dot. See +\code{\link[targets:tar_target]{targets::tar_target()}} for more information.} \item{raster}{a \code{SpatRaster} object to be split into tiles.} diff --git a/man/tar_terra_vect.Rd b/man/tar_terra_vect.Rd index 01717df..08ca86f 100644 --- a/man/tar_terra_vect.Rd +++ b/man/tar_terra_vect.Rd @@ -28,9 +28,9 @@ tar_terra_vect( ) } \arguments{ -\item{name}{Symbol, name of the target. A target -name must be a valid name for a symbol in R, and it -must not start with a dot. See \code{\link[targets:tar_target]{targets::tar_target()}} for more information.} +\item{name}{Symbol, name of the target. A target name must be a valid name +for a symbol in R, and it must not start with a dot. See +\code{\link[targets:tar_target]{targets::tar_target()}} for more information.} \item{command}{R code to run the target.} diff --git a/man/tile_helpers.Rd b/man/tile_helpers.Rd index d948654..8d553f2 100644 --- a/man/tile_helpers.Rd +++ b/man/tile_helpers.Rd @@ -78,7 +78,11 @@ list( tar_terra_tiles( name = rast_split_2blocks, raster = my_map, - tile_fun = \(x) tile_blocksize(x, n_blocks_row = 2, n_blocks_col = 1), + tile_fun = \(x) tile_blocksize( + x, + n_blocks_row = 2, + n_blocks_col = 1 + ), description = "Each tile is 2 blocks tall, 1 block wide" ) tar_terra_tiles( diff --git a/tests/testthat/test-geotargets-option.R b/tests/testthat/test-geotargets-option.R index 3b8a68c..9d57642 100644 --- a/tests/testthat/test-geotargets-option.R +++ b/tests/testthat/test-geotargets-option.R @@ -1,4 +1,5 @@ -targets::tar_test("geotargets_options_get() retrieves options in correct priority", { +targets::tar_test( + "geotargets_options_get() retrieves options in correct priority", { # options takes precedent over env var withr::with_envvar( c("GEOTARGETS_GDAL_RASTER_DRIVER" = "COG"), @@ -12,7 +13,9 @@ targets::tar_test("geotargets_options_get() retrieves options in correct priorit ) }) targets::tar_make() - expect_equal(geotargets::geotargets_option_get("gdal.raster.driver"), "GIF") + expect_equal( + geotargets::geotargets_option_get("gdal.raster.driver"), "GIF" + ) }) ) }) @@ -28,7 +31,8 @@ test_that("geotargets_option_set() works", { expect_equal(geotargets_option_get("gdal_raster_driver"), "COG") }) -test_that("options aren't reset with multiple calls to geotargets_option_set()", { +test_that( + "options aren't reset with multiple calls to geotargets_option_set()", { op_rast <- getOption("geotargets.gdal.raster.driver") withr::defer(options("geotargets.gdal.raster.driver" = op_rast)) op_vect <- getOption("geotargets.gdal.vector.driver") diff --git a/tests/testthat/test-tar-stars.R b/tests/testthat/test-tar-stars.R index b93cced..5ba2647 100644 --- a/tests/testthat/test-tar-stars.R +++ b/tests/testthat/test-tar-stars.R @@ -1,13 +1,17 @@ -# test_that() #Included to make RStudio recognize this file as a test +# test_that() #Included to make RStudio recognize this file as a test # nolint skip_if_not_installed("stars") targets::tar_test("tar_stars() works", { - geotargets::geotargets_option_set(gdal_raster_creation_options = c("COMPRESS=DEFLATE", "TFW=YES")) + geotargets::geotargets_option_set( + gdal_raster_creation_options = c("COMPRESS=DEFLATE", "TFW=YES") + ) targets::tar_script({ list(geotargets::tar_stars( test_stars, - stars::read_stars(system.file("tif", "olinda_dem_utm25s.tif", package = "stars")) + stars::read_stars( + system.file("tif", "olinda_dem_utm25s.tif", package = "stars") + ) )) }) targets::tar_make() @@ -19,11 +23,15 @@ targets::tar_test("tar_stars() works", { }) targets::tar_test("tar_stars_proxy() works", { - geotargets::geotargets_option_set(gdal_raster_creation_options = c("COMPRESS=DEFLATE", "TFW=YES")) + geotargets::geotargets_option_set( + gdal_raster_creation_options = c("COMPRESS=DEFLATE", "TFW=YES") + ) targets::tar_script({ list(geotargets::tar_stars_proxy( test_stars_proxy, - stars::read_stars(system.file("tif", "olinda_dem_utm25s.tif", package = "stars")) + stars::read_stars( + system.file("tif", "olinda_dem_utm25s.tif", package = "stars") + ) )) }) targets::tar_make() @@ -94,7 +102,9 @@ targets::tar_test("tar_stars() works with dynamic branching", { list( geotargets::tar_stars( test_stars, - stars::read_stars(system.file("tif", "olinda_dem_utm25s.tif", package = "stars")) + stars::read_stars( + system.file("tif", "olinda_dem_utm25s.tif", package = "stars") + ) ), targets::tar_target( to_add, diff --git a/tests/testthat/test-tar-terra-rast.R b/tests/testthat/test-tar-terra-rast.R index 7ed8228..5d25d22 100644 --- a/tests/testthat/test-tar-terra-rast.R +++ b/tests/testthat/test-tar-terra-rast.R @@ -1,4 +1,4 @@ -# test_that() #Included to make RStudio recognize this file as a test +# test_that() #Included to make RStudio recognize this file as a test # nolint library(targets) targets::tar_test("tar_terra_rast() works", { targets::tar_script({ @@ -17,9 +17,12 @@ targets::tar_test("tar_terra_rast() works", { ) }) -targets::tar_test("tar_terra_rast() works with multiple workers (tests marshaling/unmarshaling)", { +targets::tar_test( + "tar_terra_rast() works with multiple workers (tests un/marshaling)", { targets::tar_script({ - targets::tar_option_set(controller = crew::crew_controller_local(workers = 2)) + targets::tar_option_set( + controller = crew::crew_controller_local(workers = 2) + ) list( geotargets::tar_terra_rast( rast1, @@ -76,21 +79,27 @@ targets::tar_test("user resources are passed correctly", { testthat::expect_equal( tar_terra_rast( x, 1, - resources = tar_resources(crew = tar_resources_crew(controller = "persistent")) + resources = tar_resources( + crew = tar_resources_crew(controller = "persistent") + ) )$settings$resources$crew, tar_resources_crew(controller = "persistent") ) testthat::expect_equal( tar_terra_vect( x, 1, - resources = tar_resources(crew = tar_resources_crew(controller = "persistent")) + resources = tar_resources( + crew = tar_resources_crew(controller = "persistent") + ) )$settings$resources$crew, tar_resources_crew(controller = "persistent") ) testthat::expect_equal( tar_terra_sprc( x, 1, - resources = tar_resources(crew = tar_resources_crew(controller = "persistent")) + resources = tar_resources( + crew = tar_resources_crew(controller = "persistent") + ) )$settings$resources$crew, tar_resources_crew(controller = "persistent") ) diff --git a/tests/testthat/test-tar-terra-sprc.R b/tests/testthat/test-tar-terra-sprc.R index 2debae9..036e319 100644 --- a/tests/testthat/test-tar-terra-sprc.R +++ b/tests/testthat/test-tar-terra-sprc.R @@ -1,4 +1,4 @@ -# test_that() #Included to make RStudio recognize this file as a test +# test_that() #Included to make RStudio recognize this file as a test # nolint targets::tar_test("tar_terra_sprc() works", { geotargets::geotargets_option_set( gdal_raster_creation_options = @@ -67,9 +67,9 @@ targets::tar_test("tar_terra_sds() works", { expect_snapshot(x) }) -# difficult to test for this warning from tar_terra_sprc() because it doesn't end -# up in tar_meta() in current version of `targets`. Added in dev version: -# https://github.com/ropensci/targets/discussions/1345#discussioncomment-10908585. +# difficult to test for this warning from tar_terra_sprc() because it doesn't +# end up in tar_meta() in current version of `targets`. Added in dev version: +# https://github.com/ropensci/targets/discussions/1345 # Once this is released, this test can be replaced with a targets pipeline and a # check on `tar_meta(target, warnings)` diff --git a/tests/testthat/test-tar-terra-tiles.R b/tests/testthat/test-tar-terra-tiles.R index f663b3b..2ee4251 100644 --- a/tests/testthat/test-tar-terra-tiles.R +++ b/tests/testthat/test-tar-terra-tiles.R @@ -1,4 +1,4 @@ -# test_that() #Included to make RStudio recognize this file as a test +# test_that() #Included to make RStudio recognize this file as a test # nolint targets::tar_test("tar_terra_tiles() works", { targets::tar_script({ library(targets) @@ -7,7 +7,6 @@ targets::tar_test("tar_terra_tiles() works", { list( tar_target( my_file, - # system.file("ex/elev.tif", package="terra"), system.file("ex/logo.tif", package = "terra"), format = "file" ), @@ -39,7 +38,8 @@ targets::tar_test("tar_terra_tiles() works", { "tile_blocksize(my_map)" ) # expect_equal(manifest[manifest$name == "rast_split_n_exts", ][["command"]], - # "tile_grid(my_map, ncol = 2, nrow = 2)") #TODO: haven't figured out how to make the manifest look nice with anonymous funs yet + # "tile_grid(my_map, ncol = 2, nrow = 2)") + #TODO: haven't figured out how to make manifest look nice with anon funs yet expect_equal( manifest[manifest$name == "rast_split", ][["command"]], "set_window(my_map, terra::ext(rast_split_exts))" diff --git a/tests/testthat/test-tar-terra-vect.R b/tests/testthat/test-tar-terra-vect.R index 2720135..bdc8684 100644 --- a/tests/testthat/test-tar-terra-vect.R +++ b/tests/testthat/test-tar-terra-vect.R @@ -1,4 +1,4 @@ -# test_that() +# test_that() # nolint targets::tar_test("tar_terra_vect() works", { targets::tar_script({ lux_area <- function(projection = "EPSG:4326") { @@ -54,9 +54,12 @@ targets::tar_test("tar_terra_vect() works with dynamic branching", { expect_length(targets::tar_read(my_vect_subs), 2) }) -targets::tar_test("tar_terra_vect() works with multiple workers (tests marshaling/unmarshaling)", { +targets::tar_test( + "tar_terra_vect() works with multiple workers (tests un/marshaling)", { targets::tar_script({ - targets::tar_option_set(controller = crew::crew_controller_local(workers = 2)) + targets::tar_option_set( + controller = crew::crew_controller_local(workers = 2) + ) list( geotargets::tar_terra_vect( vect1,