Skip to content

Commit

Permalink
Merge pull request #34 from MaxenceMartin/changes_defoliated_trees
Browse files Browse the repository at this point in the history
Update `defoliate_trees()` #2
  • Loading branch information
chguiterman committed Nov 9, 2020
2 parents 1df061e + 57e7b38 commit 7f84be2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Expand Up @@ -28,7 +28,9 @@ Imports:
purrr,
rlang,
stats,
tidyr
tidyr,
glue,
tibble
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.1
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
@@ -1,5 +1,7 @@
# dfoliatR v0.3.0

* Updated `defoliated_trees()` to work without nonhost chronology #2

# dfoliatR v0.2.0

* Readme edits
Expand Down
32 changes: 27 additions & 5 deletions R/main.R
Expand Up @@ -38,6 +38,13 @@
#' require a long-form data frame identifiable as a [defol()] object. Selecting
#' `list_output = TRUE` will trigger errors in running other functions.
#'
#'@importFrom rlang .data :=
#'@importFrom magrittr %>%
#'@importFrom glue glue
#'@importFrom tibble rownames_to_column
#'@importFrom tibble column_to_rownames
#'@importFrom stats na.omit
#'
#'@examples
#'# Load host and non-host data
#'data("dmj_h") # Host trees
Expand All @@ -47,15 +54,17 @@
#'
#'
#'@export
defoliate_trees <- function(host_tree, nonhost_chron, duration_years = 8,
defoliate_trees <- function(host_tree, nonhost_chron = NULL, duration_years = 8,
max_reduction = -1.28, bridge_events = FALSE,
series_end_event = FALSE, list_output = FALSE) {

nonhost_chron <- data.frame(nonhost_chron)

# If there is a nonhost chronology
if (ncol(nonhost_chron) > 1) stop("nonhost_chron can only contain 1 series")
if (max_reduction > 0) max_reduction <- max_reduction * -1
# To DO: Add provision that if only host series are given, no correction is
# made, but series are scanned for defol_status

host_tree <- data.frame(host_tree)
nonhost_chron <- data.frame(nonhost_chron)
nseries <- ncol(host_tree)
tree_list <- lapply(seq_len(nseries), function(i) {
input_series <-
Expand All @@ -64,7 +73,20 @@ defoliate_trees <- function(host_tree, nonhost_chron, duration_years = 8,
host_tree[, i, drop = FALSE],
nonhost_chron)
)
corrected_series <- gsi(input_series)

#If there is no nonhost chronology
if (nrow(nonhost_chron) > 0) corrected_series <- gsi(input_series)
else corrected_series <- host_tree %>%
rownames_to_column(var = "year") %>%
select(year, colnames(host_tree)[i]) %>%
na.omit() %>%
mutate(nonhost = NA,
nonhost_rescale = NA,
!!glue("{colnames(host_tree)[i]}_gsi") := .data[[colnames(host_tree)[i]]],
!!glue("{colnames(host_tree)[i]}_ngsi") := scale(.data[[glue("{colnames(host_tree)[i]}_gsi")]])
) %>%
column_to_rownames(var = "year")

defoliated_series <- id_defoliation(corrected_series,
duration_years = duration_years,
bridge_events = bridge_events,
Expand Down

0 comments on commit 7f84be2

Please sign in to comment.