-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexposure.R
48 lines (44 loc) · 1.09 KB
/
exposure.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#' Cumulative exposure assessment
#'
#' Assessment of the exposure (i.e. overlap) between valued components and environmental drivers in the context of cumulative effects assessments
#'
#' @eval arguments(c("drivers","vc"))
#' @param exportAs string, the type of object that should be created, either a "list" or a "stars" object
#'
#' @export
#'
#' @examples
#' # Data
#' drivers <- rcea:::drivers
#' vc <- rcea:::vc
#'
#' # Exposure
#' (expo <- exposure(drivers, vc, "stars"))
#' plot(expo)
#' expo <- merge(expo, name = "vc") |>
#' split("drivers")
#' plot(expo)
exposure <- function(drivers, vc, exportAs = c("list", "stars")) {
#
exportAs <- match.arg(exportAs)
# Drivers
dr_df <- as.data.frame(drivers) |>
dplyr::select(-x, -y)
# Valued components
vc_df <- as.data.frame(vc) |>
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, `*`)
}
)
# Return
if (exportAs == "list") {
dat
} else if (exportAs == "stars") {
make_stars(dat, drivers, vc)
}
}