Skip to content

Commit

Permalink
small edits to functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Melanie Frazier authored and Melanie Frazier committed Jun 29, 2024
1 parent eee3486 commit 62066a0
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 40 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ Imports:
dplyr (>= 0.3),
ggplot2 (>= 1.0.0),
plyr (>= 1.8.0),
reshape2,
stringr,
git2r,
methods,
tidyr,
RColorBrewer,
htmlwidgets,
here,
rlang
rlang,
plotly,
tidyverse
Suggests:
knitr,
rmarkdown
Expand Down
2 changes: 1 addition & 1 deletion R/CalculateAll.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions R/CalculateResilienceAll.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ', ')))
}

Expand Down Expand Up @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions R/PlotFlower.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
#'
#'

Expand Down
2 changes: 1 addition & 1 deletion R/WriteRefPoint.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))

}
21 changes: 9 additions & 12 deletions R/layers_eez_base_updater.R
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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")

Expand Down
32 changes: 16 additions & 16 deletions R/split_regions.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -69,34 +69,34 @@ 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
if (!duplicate) {
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 {
Expand All @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions man/PlotFlower.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 62066a0

Please sign in to comment.