diff --git a/DESCRIPTION b/DESCRIPTION index 3a3b7c5..116f0c9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -18,7 +18,6 @@ Imports: dplyr (>= 0.3), ggplot2 (>= 1.0.0), plyr (>= 1.8.0), - reshape2, stringr, git2r, methods, @@ -26,7 +25,9 @@ Imports: RColorBrewer, htmlwidgets, here, - rlang + rlang, + plotly, + tidyverse Suggests: knitr, rmarkdown diff --git a/R/CalculateAll.R b/R/CalculateAll.R index c431d2f..ddbc134 100644 --- a/R/CalculateAll.R +++ b/R/CalculateAll.R @@ -98,7 +98,7 @@ CalculateAll = function(conf, layers){ if ( dplyr::is.grouped_df(scores_g) ){ cat(sprintf('Ungrouping scores variable returned from %s model...\n', g)) scores_g <- scores_g %>% - ungroup() + dplyr::ungroup() } ## error if 'status' or 'trend' are missing diff --git a/R/CalculateResilienceAll.R b/R/CalculateResilienceAll.R index 11919a4..1a81657 100644 --- a/R/CalculateResilienceAll.R +++ b/R/CalculateResilienceAll.R @@ -67,8 +67,8 @@ CalculateResilienceAll = function(layers, conf){ paste( unlist( layers$meta %>% - filter(layer %in% r_layers & val_0to1==F) %>% - select(layer)), + dplyr::filter(layer %in% r_layers & val_0to1==F) %>% + dplyr::select(layer)), collapse = ', '))) } @@ -144,7 +144,7 @@ CalculateResilienceAll = function(layers, conf){ r_rgn_layers <- scenario_data_year %>% dplyr::left_join(r_rgn_layers_data, by=c("year", "layer")) %>% - select(region_id, val_num, layer) + dplyr::select(region_id, val_num, layer) ## error check: matrix and region data layers include the same resilience factors check <- setdiff(r_layers, r_rgn_layers$layer) diff --git a/R/PlotFlower.R b/R/PlotFlower.R index 2bb24e3..7d551e3 100644 --- a/R/PlotFlower.R +++ b/R/PlotFlower.R @@ -16,20 +16,19 @@ #' scores.csv; if not provided, defaults to most recent year #' @param assessment_name this is the name that will be given to the weighted #' average of all regions (i.e., region_id=0, usually something like: "Global Average") -#' @scenario_folder name of the scenario folder within the repository +#' @param scenario_folder name of the scenario folder within the repository #' (this is the folder with scores.csv, conf and layers folders, etc.) -#' @scores_file name of the file with the score data used to create the flower plots, +#' @param scores_file name of the file with the score data used to create the flower plots, #' typically: "scores.csv" -#' @dir_fig_save file path to the location the figures (and related csv file) will be +#' @param dir_fig_save file path to the location the figures (and related csv file) will be #' saved -#' @save whether to save csv and png files (otherwise figures are only displayed) +#' @param save whether to save csv and png files (otherwise figures are only displayed) #' #' @return png file/s of flowerplots will be saved in the dir_fig_save location; an #' additional regions_figs.csv file will be saved that describes region_id, region_name, #' and file paths to flower_.png files. #' @export #' -#' @examples #' #' diff --git a/R/WriteRefPoint.R b/R/WriteRefPoint.R index 72dbda5..2687ed5 100644 --- a/R/WriteRefPoint.R +++ b/R/WriteRefPoint.R @@ -32,6 +32,6 @@ WriteRefPoint <- function(goal, method, ref_pt, scenario_name="eez") { goal = goal, method = method, reference_point = ref_pt)) - write_csv(ref_pts, sprintf(here("%s/temp/reference_pts_%s.csv"), scenario_name, scen_year)) + readr::write_csv(ref_pts, sprintf(here("%s/temp/reference_pts_%s.csv"), scenario_name, scen_year)) } \ No newline at end of file diff --git a/R/layers_eez_base_updater.R b/R/layers_eez_base_updater.R index 04977cc..07bdde3 100644 --- a/R/layers_eez_base_updater.R +++ b/R/layers_eez_base_updater.R @@ -11,11 +11,8 @@ layers_eez_base_updater <- function() { - require(here) - require(tidyverse) - # read in the csv - layers_eez_base <- read_csv(here("metadata_documentation/layers_eez_base.csv"), col_types = cols()) + layers_eez_base <- readr::read_csv(here("metadata_documentation/layers_eez_base.csv"), col_types = cols()) # prompt user for current version year (gsub() to remove 'v' if user adds it as well) message("") @@ -46,8 +43,8 @@ layers_eez_base_updater <- function() { # prompt user for layers which have been updated - updated_layers <- str_split(readline(prompt = "enter 'all' or layers separated only by commas: "), ",")[[1]] %>% - str_remove_all(" ") + updated_layers <- stringr::str_split(readline(prompt = "enter 'all' or layers separated only by commas: "), ",")[[1]] %>% + stringr::str_remove_all(" ") if (tolower(updated_layers[1]) == "all") { updated_layers <- possible_layers @@ -71,8 +68,8 @@ layers_eez_base_updater <- function() { message("") } - updated_layers <- str_split(readline(prompt = "enter 'all' or layers separated only by commas: "), ",")[[1]] %>% - str_remove_all(" ") + updated_layers <- stringr::str_split(readline(prompt = "enter 'all' or layers separated only by commas: "), ",")[[1]] %>% + stringr::str_remove_all(" ") if (tolower(updated_layers[1]) == "all") { updated_layers <- possible_layers @@ -81,13 +78,13 @@ layers_eez_base_updater <- function() { # create df with updated file paths layers_eez_base_updated <- layers_eez_base %>% - mutate(dir = case_when(layer %in% updated_layers ~ + mutate(dir = dplyr::case_when(layer %in% updated_layers ~ gsub("v20\\d\\d", version_year, dir), TRUE ~ dir)) # vector of dir names which have changed from the original csv - updated_dirs <- anti_join(layers_eez_base_updated, layers_eez_base, by = c("layer", "dir")) %>% - rename("dir (UPDATED)" = dir) + updated_dirs <- dplyr::anti_join(layers_eez_base_updated, layers_eez_base, by = c("layer", "dir")) %>% + dplyr::rename("dir (UPDATED)" = dir) # make sure the chosen year and layers elicited changes if (nrow(updated_dirs) != 0) { @@ -99,7 +96,7 @@ layers_eez_base_updater <- function() { overwrite <- readline(prompt = "update? ('y' or 'n'): ") if (overwrite == "y") { - write.csv(layers_eez_base_updated, here("metadata_documentation/layers_eez_base.csv"), + write.csv(layers_eez_base_updated, here::here("metadata_documentation/layers_eez_base.csv"), row.names = FALSE) message("\nfile has been updated\n") diff --git a/R/split_regions.R b/R/split_regions.R index 0a4bb4e..de8aa87 100644 --- a/R/split_regions.R +++ b/R/split_regions.R @@ -50,7 +50,7 @@ split_regions <- function(m, country_column = "country", value_column = "value", # Rename data frame columns m <- m %>% - rename(!!paste0(country_column) := country, !!paste0(value_column) := value) + dplyr::rename(!!paste0(country_column) := country, !!paste0(value_column) := value) # Load population weighting data population <- split_pops @@ -69,23 +69,23 @@ split_regions <- function(m, country_column = "country", value_column = "value", # Calculate area weights for the regions area_weights <- population %>% - filter(country %in% regions) %>% - mutate(weight = ifelse(duplicate, 1, population / pop_sum)) %>% # Conditionally set weight - select(country, weight) %>% - mutate(id = row_number()) + dplyr::filter(country %in% regions) %>% + dplyr::mutate(weight = ifelse(duplicate, 1, population / pop_sum)) %>% # Conditionally set weight + dplyr::select(country, weight) %>% + dplyr::mutate(id = row_number()) # Split the data into different regions m_new <- m %>% - filter(country == country_name) %>% - uncount(length(regions), .id = "id") %>% - left_join(area_weights, by = c("id" = "id")) %>% - mutate(value = value * weight) %>% - mutate(country = country.y) %>% - select(-country.x, -country.y, -id, -weight) + dplyr::filter(country == country_name) %>% + tidyr::uncount(length(regions), .id = "id") %>% + dplyr::left_join(area_weights, by = c("id" = "id")) %>% + dplyr::mutate(value = value * weight) %>% + dplyr::mutate(country = country.y) %>% + dplyr::select(-country.x, -country.y, -id, -weight) # Update m to remove the original country and add broken down rows m <- m %>% - filter(!(country %in% country_name)) %>% + dplyr::filter(!(country %in% country_name)) %>% rbind(m_new) # Sum duplicate rows if duplicate is False @@ -93,10 +93,10 @@ split_regions <- function(m, country_column = "country", value_column = "value", grouping_vars <- setdiff(names(m), "value") m <- m %>% - group_by(across(all_of(grouping_vars))) %>% - summarize(value = case_when(all(is.na(value)) ~ NA, + dplyr::group_by(across(all_of(grouping_vars))) %>% + dplyr::summarize(value = case_when(all(is.na(value)) ~ NA, TRUE ~ sum(value, na.rm = TRUE))) %>% - ungroup() + dplyr::ungroup() # Remove duplicates if duplicate is True } else { @@ -112,7 +112,7 @@ split_regions <- function(m, country_column = "country", value_column = "value", # Rename columns to match data frame input m <- m %>% - rename(!!country_column := country, !!value_column := value) + dplyr::rename(!!country_column := country, !!value_column := value) # Return the new data frame return(m) diff --git a/man/PlotFlower.Rd b/man/PlotFlower.Rd index 83e10c1..b23abfa 100644 --- a/man/PlotFlower.Rd +++ b/man/PlotFlower.Rd @@ -26,6 +26,17 @@ scores.csv; if not provided, defaults to most recent year} \item{assessment_name}{this is the name that will be given to the weighted average of all regions (i.e., region_id=0, usually something like: "Global Average")} + +\item{scenario_folder}{name of the scenario folder within the repository +(this is the folder with scores.csv, conf and layers folders, etc.)} + +\item{scores_file}{name of the file with the score data used to create the flower plots, +typically: "scores.csv"} + +\item{dir_fig_save}{file path to the location the figures (and related csv file) will be +saved} + +\item{save}{whether to save csv and png files (otherwise figures are only displayed)} } \value{ png file/s of flowerplots will be saved in the dir_fig_save location; an