Skip to content

Commit

Permalink
package pass 0 0 0
Browse files Browse the repository at this point in the history
  • Loading branch information
KevCaz committed Jan 11, 2024
1 parent 3ecf1d8 commit 2e4abf3
Show file tree
Hide file tree
Showing 20 changed files with 524 additions and 540 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
^_pkgdown\.yml$
^docs$
^pkgdown$
^\.here$
14 changes: 9 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Package: rcea
Type: Package
Title: Cumulative effects assessments package
Version: 1.0.0
Version: 1.0.0.9000
Authors@R: c(
person(given = "David",
family = "Beauchesne",
role = c("aut", "cre", "cph"),
email = "[email protected]",
comment = c(ORCID = "0000-0002-3590-8161")))
Description: Package providing functionalities to perform spatially explicit cumulative effects assessments
Description: Package providing functionalities to perform spatially explicit cumulative effects assessments.
URL: https://ecosystem-assessments.github.io/rcea/
BugReports: https://ecosystem-assessments.github.io/rcea/issues
License: GPL (>= 2)
Expand All @@ -18,18 +18,22 @@ Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Imports:
cli,
crayon,
data.table,
dplyr,
fs,
glue,
grDevices,
graphics,
here,
motifcensus,
purrr,
rlang,
sf,
stars,
stats,
stars,
stringr,
tidyr,
vroom,
whisker,
yaml
Suggests:
Expand All @@ -41,4 +45,4 @@ Remotes:
Config/testthat/edition: 3
VignetteBuilder: knitr
Depends:
R (>= 2.10)
R (>= 4.1)
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ export(ncea_species)
export(ncea_split)
export(triads)
importFrom(cli,symbol)
importFrom(crayon,blue)
importFrom(glue,glue)
importFrom(glue,glue_sql)
importFrom(grDevices,colorRampPalette)
importFrom(grDevices,dev.off)
importFrom(grDevices,png)
Expand All @@ -37,6 +35,8 @@ importFrom(graphics,par)
importFrom(graphics,polygon)
importFrom(graphics,text)
importFrom(rlang,sym)
importFrom(stars,st_as_stars)
importFrom(stats,setNames)
importFrom(yaml,read_yaml)
importFrom(yaml,write_yaml)
importFrom(yaml,yaml.load_file)
5 changes: 3 additions & 2 deletions R/args.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ arguments <- function(arg) {
dat$sensitivity <- "@param sensitivity matrix of environmental drivers and valued component, with same name as those used in `drivers` and `vc`"
dat$metaweb <- "@param metaweb matrix of valued component by valued component describing the binary interations structuring the network of valued components"
dat$trophic_sensitivity <- "@param trophic_sensitivity data.frame of trophic sensitivities, default from Beauchesne. Available as data package with `data(trophic_sensitivity)`"
dat$w_d <- dat$w_i <- "@param weights weight for the direct (`w_d`) and indirect (`w_i`) modules when calculating network-scale cea scores; w_d + 2*w_i should be equal to 1."
dat$output <- "@param relative path to export results of assessment."
dat$weights <- "@param w_d,w_i weight for the direct (`w_d`) and indirect (`w_i`) modules when calculating network-scale cea scores; w_d + 2*w_i should be equal to 1."
dat$output <- "@param output relative path to export results of assessment."
dat$motif_effects <- "@param motif_effects TODO"

dat[names(dat) %in% arg] |>
unlist()
Expand Down
12 changes: 8 additions & 4 deletions R/cea.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#'
#' Assessment of cumulative effects using the Halpern et al. 2008 method.
#'
#' @eval arguments(c("drivers","vc","sensitivity"))
#' @param exportAs string, the type of object that should be created, either a "list" or a "stars" object
#' @eval arguments(c("drivers", "vc", "sensitivity"))
#' @param exportAs string, the type of object that should be created, either a "list" or a "stars" object.
#'
#' @export
#'
Expand All @@ -19,8 +19,11 @@
#' halpern <- merge(halpern, name = "vc") |>
#' split("drivers")
#' plot(halpern)
#' get_cekm_cea(halpern, vc)
#' # do not work
#' # get_cekm_cea(halpern, vc)
cea <- function(drivers, vc, sensitivity, exportAs = "list") {
# needed as
# requireNamespace("stars", quietly = TRUE)
# Exposure
dat <- exposure(drivers, vc)

Expand All @@ -30,7 +33,7 @@ cea <- function(drivers, vc, sensitivity, exportAs = "list") {
sensitivity <- sensitivity[nmVC, nmDr]

# Effect of drivers on valued components (D * VC * u)
for (i in 1:length(dat)) {
for (i in seq_len(length(dat))) {
dat[[i]] <- sweep(dat[[i]], MARGIN = 2, sensitivity[, i], `*`)
}

Expand All @@ -45,6 +48,7 @@ cea <- function(drivers, vc, sensitivity, exportAs = "list") {
}

#' @describeIn cea get effects per km2
#' @param dat TODO
#' @export
get_cekm_cea <- function(dat, vc) {
dat2 <- dat
Expand Down
23 changes: 13 additions & 10 deletions R/exposure.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,36 @@
#'
#' @examples
#' # Data
#' drivers <- rcea:::drivers
#' drivers <- rcea:::drivers
#' vc <- rcea:::vc
#'
#' # Exposure
#' (expo <- exposure(drivers, vc, "stars"))
#' plot(expo)
#' expo <- merge(expo, name = "vc") |>
#' split("drivers")
#' split("drivers")
#' plot(expo)
exposure <- function(drivers, vc, exportAs = "list") {
exposure <- function(drivers, vc, exportAs = c("list", "stars")) {

#
exportAs <- match.arg(exportAs)
# Drivers
dr_df <- as.data.frame(drivers) |>
dplyr::select(-x,-y)
dplyr::select(-x, -y)

# Valued components
vc_df <- as.data.frame(vc) |>
dplyr::select(-x,-y)
dplyr::select(-x, -y)

# Exposure of valued components to drivers (Dj * VCi)
dat <- apply(
dr_df,
MARGIN = 2,
function(x) {
sweep(vc_df, MARGIN=1, x, `*`)
sweep(vc_df, MARGIN = 1, x, `*`)
}
)
)

# Return
if (exportAs == "list") {
dat
Expand Down
10 changes: 5 additions & 5 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ make_stars <- function(dat, drivers, vc) {
)

# For data exported as list format (exposure and cea assessments)
if ("list" %in% class(dat)) {
if (inherits(dat, "list")) {
for (i in seq_len(length(dat))) {
dat[[i]] <- cbind(xy, dat[[i]]) |>
dplyr::mutate(drivers = drNames[i])
Expand All @@ -47,7 +47,7 @@ make_stars <- function(dat, drivers, vc) {
}

# For data exported in long format, from the ncea assessment
if ("data.frame" %in% class(dat)) {
if (inherits(dat, "data.frame")) {
# Get coordinates with repeated driver names
xy$id_cell <- seq_len(nrow(xy))
xyd <- cbind(
Expand Down Expand Up @@ -77,9 +77,9 @@ make_stars <- function(dat, drivers, vc) {
dat
}

#' ========================================================================================
#' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' ----------------------------------------------------------------------------------------
# ==============================================================================
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ------------------------------------------------------------------------------
#' @describeIn helpers create stars object from list of cea matrices or cea array
#' @export
make_stars2 <- function(dat, drivers, vc) {
Expand Down
Loading

0 comments on commit 2e4abf3

Please sign in to comment.