Skip to content

Commit

Permalink
Merge pull request #9 from Schiano-NOAA/style-doc-code
Browse files Browse the repository at this point in the history
Style code and document
  • Loading branch information
Schiano-NOAA authored May 6, 2024
2 parents 741650d + 9fc2ed7 commit 2403b0d
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 50 deletions.
10 changes: 5 additions & 5 deletions R/add_section.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ add_section <- function(
for (i in 1:length(sec_names)) {
section_i_name <- paste0(gsub(" ", "_", sec_names[i]), ".qmd")
local_section <- forstringr::str_extract_part(location[i], "-", before = FALSE)
local_section_prev <- forstringr::str_extract_part(location[i-1], "-", before = FALSE)
local_section_prev <- forstringr::str_extract_part(location[i - 1], "-", before = FALSE)

if (any("TRUE" %in% grepl("-", location))) {
locality <- forstringr::str_extract_part(location[i], "-", before = TRUE)
locality_prev <- forstringr::str_extract_part(location[i-1], "-", before = TRUE)
locality_prev <- forstringr::str_extract_part(location[i - 1], "-", before = TRUE)
} else if (any("TRUE" %in% grepl(" ", location))) {
locality <- forstringr::str_extract_part(location[i], " ", before = TRUE)
locality_prev <- forstringr::str_extract_part(location[i-1], "-", before = TRUE)
locality_prev <- forstringr::str_extract_part(location[i - 1], "-", before = TRUE)
}

section_i <- paste0(
Expand All @@ -60,8 +60,8 @@ add_section <- function(
if (locality == "before") {
other_sections <- append(other_sections, section_i_name, after = (which(grepl(local_section, other_sections)) - 1))
} else if (locality == "after") {
if(locality %in% locality_prev & local_section %in% local_section_prev){
other_sections <- append(other_sections, section_i_name, after = which(grepl(gsub(" ", "_", sec_names[i-1]), other_sections)))
if (locality %in% locality_prev & local_section %in% local_section_prev) {
other_sections <- append(other_sections, section_i_name, after = which(grepl(gsub(" ", "_", sec_names[i - 1]), other_sections)))
} else {
other_sections <- append(other_sections, section_i_name, after = which(grepl(local_section, other_sections)))
}
Expand Down
14 changes: 8 additions & 6 deletions R/afsc_tier_table.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# AFSC Tier Table
afsc_tier_table <- function(){
afsc_tier_table <- function() {
level <- c(
"Level 1: Normal",
"Level 2: Substantially Increased Concerns",
Expand Down Expand Up @@ -32,11 +32,13 @@ afsc_tier_table <- function(){
)
tier_df <- data.frame(level, ass_considerations, popdy_considerations, eco_considerations, fish_performance)
flextable::flextable(tier_df) |>
flextable::set_header_labels(level = "",
ass_considerations = "Assessment-related considerations",
popdy_considerations = "Population dynamics considerations",
eco_considerations = "Environmental/ecosystems considerations",
fish_performance = "Fishery Performance") |>
flextable::set_header_labels(
level = "",
ass_considerations = "Assessment-related considerations",
popdy_considerations = "Population dynamics considerations",
eco_considerations = "Environmental/ecosystems considerations",
fish_performance = "Fishery Performance"
) |>
flextable::italic(part = "header") |>
flextable::bold(part = "header") |>
flextable::hline(i = c(1, 2, 3))
Expand Down
2 changes: 1 addition & 1 deletion R/create_template.R
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ create_template <- function(
# Add chunk to load in assessment data
ass_output <- chunkr(
paste0(
"convert_output(output.file = ", "c('", paste(model_results, collapse="', '"), "')",
"convert_output(output.file = ", "c('", paste(model_results, collapse = "', '"), "')",
", model = ", "'", model, "'",
", outdir = ", "'", resdir, "'", ")"
),
Expand Down
4 changes: 2 additions & 2 deletions R/generate_citation.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ generate_citation <- function(
}

# Pull affiliation of first author
if(length(unlist(strsplit(author[1], " ")))==3){
if (length(unlist(strsplit(author[1], " "))) == 3) {
primauth_loc <- utils::read.csv(system.file("resources", "authorship.csv", package = "ASAR", mustWork = TRUE)) |>
dplyr::filter(last == unlist(strsplit(author[1], " "))[3])
} else {
Expand All @@ -34,7 +34,7 @@ generate_citation <- function(
}

# Check and fix if there is more than one author with the same last name
if(nrow(primauth_loc)>1){
if (nrow(primauth_loc) > 1) {
primauth_loc <- utils::read.csv(system.file("resources", "authorship.csv", package = "ASAR", mustWork = TRUE)) |>
dplyr::filter(last == unlist(strsplit(author[1], " "))[1])
}
Expand Down
53 changes: 31 additions & 22 deletions R/update_text.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,30 @@ update_text <- function(
office = NULL,
region = NULL,
species = NULL,
year = NULL
){
year = NULL) {
# Kind of wrapper function for trackdown::download_file so all sections in assessment document are updated

# Downloading version of google doc content onto local drive
# Will overwrite current content - add warning box

# Check if want to proceed bc this overwrites the local file(s)
if(interactive() && isFALSE(force)){
if (interactive() && isFALSE(force)) {
response <- utils::menu(
c("Yes", "No"),
title = paste("Downloading the file from Google Drive will overwrite local file.",
"Do you want to proceed?"))
title = paste(
"Downloading the file from Google Drive will overwrite local file.",
"Do you want to proceed?"
)
)

if (response == 1L) {
select_change <- utils::menu(
c("Yes", "No"),
title = paste("Downloading the file from Google Drive will overwrite local file.",
"Do you want to proceed?"))
title = paste(
"Downloading the file from Google Drive will overwrite local file.",
"Do you want to proceed?"
)
)
}

if (response == 2L) {
Expand All @@ -29,28 +34,32 @@ update_text <- function(
}
}

if (select_change == 1L)
if (select_change == 1L) {

# directory where templates were created
if (!is.null(region)) {
subdir <- paste0("~/stock_assessment_templates", "/", office, "/", species, "/", region, "/", year)
} else {
subdir <- paste0("~/stock_assessment_templates", "/", office, "/", species, "/", year)
# directory where templates were created
if (!is.null(region)) {
subdir <- paste0("~/stock_assessment_templates", "/", office, "/", species, "/", region, "/", year)
} else {
subdir <- paste0("~/stock_assessment_templates", "/", office, "/", species, "/", year)
}
}

all_sections <- list.files(subdir, pattern = ".qmd")
sections <- all_sections[!grepl("skeleton.qmd", all_sections)]

for(i in sections){
if(!is.null(region)){
trackdown::download_file(file = i,
gfile = i,
shared_drive = paste("National Stock Assessment Archives", office, region, species, year, sep = "/"))
for (i in sections) {
if (!is.null(region)) {
trackdown::download_file(
file = i,
gfile = i,
shared_drive = paste("National Stock Assessment Archives", office, region, species, year, sep = "/")
)
} else {
trackdown::download_file(file = i,
gfile = i,
shared_drive = paste("National Stock Assessment Archives", office, species, year, sep = "/"))
trackdown::download_file(
file = i,
gfile = i,
shared_drive = paste("National Stock Assessment Archives", office, species, year, sep = "/")
)
}
}

}
4 changes: 2 additions & 2 deletions man/create_template.Rd

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

12 changes: 7 additions & 5 deletions tests/testthat/test-add_section.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ test_that("Adding new sections works.", {
})

test_that("Adding new sections works.", {
sec_list <- add_base_section(c("introduction","data","model","acknowledgements","tables","figures","appendix","references"))
sec_list <- add_base_section(c("introduction", "data", "model", "acknowledgements", "tables", "figures", "appendix", "references"))
sections <- add_section(
sec_names = c("Harvest Control Rules", "Regional Management Considerations","Research and Data Needs"),
sec_names = c("Harvest Control Rules", "Regional Management Considerations", "Research and Data Needs"),
location = c("after-model", "after-model", "after-model"),
other_sections = sec_list,
subdir = tempdir()
)
exp_list <- list("02_introduction.qmd","03_data.qmd","04_model.qmd","Harvest_Control_Rules.qmd",
"Regional_Management_Considerations.qmd","Research_and_Data_Needs.qmd",
"07_acknowledgements.qmd","09_tables.qmd","10_figures.qmd","11_appendix.qmd","08_references.qmd")
exp_list <- list(
"02_introduction.qmd", "03_data.qmd", "04_model.qmd", "Harvest_Control_Rules.qmd",
"Regional_Management_Considerations.qmd", "Research_and_Data_Needs.qmd",
"07_acknowledgements.qmd", "09_tables.qmd", "10_figures.qmd", "11_appendix.qmd", "08_references.qmd"
)
expect_equal(sections, exp_list)
})

Expand Down
17 changes: 10 additions & 7 deletions tests/testthat/test-generate_citation.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
test_that("citaiton generated properly for SWFSC", {
fxn_test <- generate_citation(
author = c("Peter T. Kuriyama",
"Caitlin Allen Akselrud",
"Juan P. Zwolinski",
"Kevin T. Hill"),
title = "Check",
year = 2024,
office = "SWFSC")
author = c(
"Peter T. Kuriyama",
"Caitlin Allen Akselrud",
"Juan P. Zwolinski",
"Kevin T. Hill"
),
title = "Check",
year = 2024,
office = "SWFSC"
)
expected_output <- "{{< pagebreak >}} \n\nPlease cite this publication as \n\nKuriyama, P.T., C.Allen Akselrud, J.P. Zwolinski, K.T. Hill. 2024. Check. NOAA Fisheries Science Center, La Jolla, CA. "
expect_equal(fxn_test, expected_output)
})

0 comments on commit 2403b0d

Please sign in to comment.