Skip to content

Commit

Permalink
use shape as first parameter for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-smart88 committed Aug 22, 2024
1 parent 35ffca5 commit 5f461af
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 18 deletions.
6 changes: 3 additions & 3 deletions R/agg_upload_f.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#' This function is called by the agg_upload module and loads a
#' raster image.
#'
#' @param path character. The location of the file to be loaded.
#' @param shape sf. sf object containing the area of interest
#' @param path character. The location of the file to be loaded.
#' @param logger Stores all notification messages to be displayed in the Log
#' Window. Insert the logger reactive list here for running in
#' shiny, otherwise leave the default NULL
Expand All @@ -16,11 +16,11 @@
#' shp_file <- list.files(system.file("extdata/shapes",
#' package="disagapp"), pattern = ".shp", full.names = TRUE)
#' shape <- sf::st_read(shp_file, quiet = TRUE)
#' raster <- agg_upload(path = path, shape = shape)
#' raster <- agg_upload(shape = shape, path = path)
#'
#' @export

agg_upload <- function(path, shape, logger = NULL) {
agg_upload <- function(shape, path, logger = NULL) {

if (!inherits(path, "character")){
logger |> writeLog(type = "error", "path must be a character string")
Expand Down
7 changes: 4 additions & 3 deletions R/cov_bioclim_f.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
#' poly <- sf::st_polygon(list(poly_matrix))
#' shape <- sf::st_sf(1, geometry = list(poly))
#' sf::st_crs(shape) = 4326
#' raster <- cov_bioclim(country_code = "LIE",
#' variables = c("Mean temperature", "Mean diurnal range"),
#' shape = shape)
#' raster <- cov_bioclim(shape = shape,
#' country_code = "LIE",
#' variables = c("Mean temperature", "Mean diurnal range"))
#'
#'
#' @export

Expand Down
6 changes: 3 additions & 3 deletions R/cov_upload_f.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#' overlap with the area of interest and crops and masks them to the area of
#' interest.
#'
#' @param shape sf. sf object containing the area of interest
#' @param path_df data.frame. Containing `datapath` and `name` columns of the
#' file(s) to be uploaded.
#' @param shape sf. sf object containing the area of interest
#' @param logger Stores all notification messages to be displayed in the Log
#' Window. Insert the logger reactive list here for running in
#' shiny, otherwise leave the default NULL
Expand All @@ -21,10 +21,10 @@
#' shp_file <- list.files(system.file("extdata", "shapes", package="disagapp"),
#' pattern = ".shp", full.names = TRUE)
#' shape <- sf::st_read(shp_file, quiet = TRUE)
#' rasters <- cov_upload(path_df = cov_df, shape = shape)
#' rasters <- cov_upload(shape = shape, path_df = cov_df)
#' @export

cov_upload <- function(path_df, shape, logger = NULL) {
cov_upload <- function(shape, path_df, logger = NULL) {

# check inputs
if (!("data.frame" %in% class(path_df))){
Expand Down
2 changes: 1 addition & 1 deletion inst/shiny/modules/agg_upload.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ agg_upload_module_server <- function(id, common, parent_session, map) {
name = list.files(system.file("extdata", "aggregation", package="disagapp")))
}
# FUNCTION CALL ####
agg_raster <- agg_upload(aggdf$datapath, common$shape, common$logger)
agg_raster <- agg_upload(common$shape, aggdf$datapath, common$logger)
if (is.null(agg_raster)){
return()
}
Expand Down
2 changes: 1 addition & 1 deletion inst/shiny/modules/agg_upload.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Upload an aggregation raster. You need to specify the directory where the file i
aggregation_directory <- ""
aggregation_files <- "{{agg_upload_path}}"
aggregation_file_path <- file.path(aggregation_directory, aggregation_files)
aggregation <- disagapp::agg_upload(aggregation_file_path)
aggregation <- disagapp::agg_upload(shape, aggregation_file_path)
names(aggregation) <- "{{agg_upload_name}}"
```
2 changes: 1 addition & 1 deletion inst/shiny/modules/cov_upload.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ cov_upload_module_server <- function(id, common, parent_session, map) {

# FUNCTION CALL ####

cov_list <- cov_upload(covdf, common$shape, common$logger)
cov_list <- cov_upload(common$shape, covdf, common$logger)

if (is.null(cov_list)){
return()
Expand Down
2 changes: 1 addition & 1 deletion inst/shiny/modules/cov_upload.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ covariate_directory <- ""
covariate_files <- {{cov_upload_path}}
covariate_file_paths <- file.path(covariate_directory, covariate_files)
covariate_file_df <- data.frame(datapath = covariate_file_paths, name = covariate_files)
uploaded_covariates <- disagapp::cov_upload(covariate_file_df)
uploaded_covariates <- disagapp::cov_upload(shape, covariate_file_df)
covariates <- append(covariates, uploaded_covariates)
```

2 changes: 1 addition & 1 deletion tests/testthat/test-agg_upload.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

test_that("Check agg_upload function works as expected", {
mdg_shape <- resp_shape(shpdf)
result <- agg_upload(aggdf$datapath, mdg_shape)
result <- agg_upload(mdg_shape, aggdf$datapath)
expect_is(result, "SpatRaster")
})

Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-cov_upload.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mad_shape <- resp_shape(shpdf)
lie_shape <- resp_download(df, area_column, resp_column, country_code[1], admin_level)

test_that("Check cov_upload function works as expected", {
result <- cov_upload(covdf, mad_shape)
result <- cov_upload(mad_shape, covdf)
expect_is(result, "list")
expect_is(result[[1]], "SpatRaster")
expect_equal(length(result), 4)
Expand All @@ -14,16 +14,16 @@ test_that("Check cov_upload handles CRS issues", {
covdf_nocrs <- data.frame(datapath = system.file("extdata", "test_data", "no_crs.tif", package="disagapp"),
name = "no_crs.tif")

expect_error(cov_upload(covdf_nocrs, mad_shape), "Some files do not have a coordinate reference system")
expect_error(cov_upload(mad_shape, covdf_nocrs), "Some files do not have a coordinate reference system")

covdf_difcrs <- data.frame(datapath = system.file("extdata", "test_data", "different_projection.tif", package="disagapp"),
name = "different_projection.tif")

result <- cov_upload(covdf_difcrs, mad_shape)
result <- cov_upload(mad_shape, covdf_difcrs)
result_crs <- terra::crs(result[[1]], describe = TRUE)
expect_equal(result_crs$code, "4326")

expect_error(cov_upload(covdf, lie_shape), "Some files do not overlap with the response data")
expect_error(cov_upload(lie_shape, covdf), "Some files do not overlap with the response data")

})

Expand Down

0 comments on commit 5f461af

Please sign in to comment.