Skip to content

Commit

Permalink
Minor features and improvements for next version (#150)
Browse files Browse the repository at this point in the history
* Feat(vignettes):
-added tables of contents to vignettes
-added tip for how to edit rda files to a11y vignette
-added link to faqs vignette to a11y vignette re: how to edit alt text and captions

* fix to incorrect reference in .bib file (#149)

* fix incorrect reference

* add url to reference fix

* remove replicate nmfs palette and add_theme

* remove files

* Custom template fixes (#143)

* update(create_template): comment out skeleton pop up at the end

* update(add_section): add functionality for putting new subsection into section

* Update(add_section): fix pathing for locating section to place code and change from add_chunk to add_child

* update(create_template): select only custom_sections when user wants to make a different outline of report

custom must = TRUE otherwise copies all files

* Fix(create_template): adjust if statement argument for custom_sections

* Add nmfspalette package back in

* removed add_theme and associated test and documentation

* remove static NOAA Fishies in authorship

* update NOAA fisheries affiliations

Co-authored-by: Schiano-NOAA <[email protected]>

---------

Co-authored-by: sbreitbart-NOAA <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Schiano-NOAA <[email protected]>

* Fix(create_template):
-Fix so that rda files can be added as input, if already created, OR generated if model results are included as an argument.
-Update documentation

* update(format_quarto): add functionality for html formatting with associated files and changes to create_template (#152)

* small comment update

* update(create_template): custom_sections adds all selected plus refs, acknow, and tables and figs if desired

* Custom template fixes (#143)

* update(create_template): comment out skeleton pop up at the end

* update(add_section): add functionality for putting new subsection into section

* Update(add_section): fix pathing for locating section to place code and change from add_chunk to add_child

* update(create_template): select only custom_sections when user wants to make a different outline of report

custom must = TRUE otherwise copies all files

* Fix(create_template): adjust if statement argument for custom_sections

* Add nmfspalette package back in

* removed add_theme and associated test and documentation

* remove static NOAA Fishies in authorship

* update NOAA fisheries affiliations

Co-authored-by: sbreitbart-NOAA <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Schiano-NOAA <[email protected]>

* Inject accessibility (#124)

* Custom template fixes (#143)

* update(create_template): comment out skeleton pop up at the end

* update(add_section): add functionality for putting new subsection into section

* Update(add_section): fix pathing for locating section to place code and change from add_chunk to add_child

* update(create_template): select only custom_sections when user wants to make a different outline of report

custom must = TRUE otherwise copies all files

* Fix(create_template): adjust if statement argument for custom_sections



* Removed captions_alt_text_template.csv because it's not used in asar... only satf. Then updated a11y vignette to reflect where users should look for the file.

* changed eval for first chunk to true, and eval for indices table setup chunk to false, re: #102 . Will change eval to true for all chunks once indices table fxn is completed.

* feature(add new template partial for accessibility and add fxn)

* Update(add_tagging): ability to add alternative text to tex file from rdas

* fix(add_taggig): change directory for rda files to arg

* fix(add_tagging): change pathing to rda_dir

* fix(add-tagging): rename accessibility tex for output

* feature(add_accessibility): rename function and load documentation

* Initial commit for add_accessibility test - commented out bc unfinished

* update(test-add_accessibility): add snapshot for tex file and add proper code for testing

* feature(alter accessibility fxns): split up funxtion into two and called both using the add_accessibility function

Also adjusted testing function for add_alttext and removed old snapshot - was not working properly

* Remove nmfspallete from dependencies temporarily (#139)

* Remove nmfs-pallete from dependencies temporarily

* Comment out reference to nmfs pallete in add_theme and tests

* style and docs: run devtools::document() and styler::style_pkg() (#140)

Co-authored-by: Schiano-NOAA <[email protected]>

* Add nmfspalette back into package

* Merge newer dev branch into old (#148)

* Minor grammatical changes just to set up new dev-1.1 branch (changed quarto --> Quarto)

* Feat(vignettes):
-added tables of contents to vignettes
-added tip for how to edit rda files to a11y vignette
-added link to faqs vignette to a11y vignette re: how to edit alt text and captions

* remove old package from namespace and add in new

* add notes for new alttext test

* Removed captions_alt_text_template.csv because it's not used in asar... only satf. Then updated a11y vignette to reflect where users should look for the file.

* feature(alter accessibility fxns): split up funxtion into two and called both using the add_accessibility function

Also adjusted testing function for add_alttext and removed old snapshot - was not working properly

* update(create_template): custom_sections adds all selected plus refs, acknow, and tables and figs if desired

* add spp_conout.csv to gitignore during testing

* feature(add_alttext): add accessibility for user to add alt text to figures inserted into the outline (png)

* fix(add_alttext): improve fxning for adding alt text to custom figs either from images or code chunks

* feature(add_alttext): add example to function

* comment out alttext fxn test

* clean up fxns and add examples

* add in support_file/accessibilit.tex

* fix(create_tables_doc): remove incorrect and duplicate indices table code

* update(accessibility fxns): update to fix minor issues with functions and documentation as mentioned by @sbreitbart-NOAA

* update documentation

* update(test-alttext): change test for alttext to mimic changes to the function - sdoes not work still

* update(a11y fxns): improvements to functions as well as tests

* update(add_alttext): update example to show to to use rename arg

* minor updates and changes to fix test failures and checks

* Feat(create_tables_doc): automatically test if tables are wide enough to require changing page orientation from portrait to landscape. Alter the Tables qmd to allow orientation changes for each table, if necessary.

* Update orientation-related code for still-in-dev landings table

* Extend landscape table width from 7.5 to 8.5"; add page breaks between tables

* bug fixes

* Started coding new function to handle super wide tables and split into smaller ones

* Finished first full draft of function that will render large tables by splitting into multiple tables

* Added essential_columns arg to render_lg_table()

* Move orientation-detecting code into own function in utils, then update create_tables_doc

* Moved render_lg_table fxn to own R script, then updated documentation

* Update tests to remove temporary files upon test completion

* Add more params to render_lg_table() and ID_pg_orientation(), then update documentation and usage in create_tables_doc()

* Added code into create_tables_doc that splits extra-wide tables into multiple smaller tables, but still a bug in render_lg_table (producing "Error" as part of output when testing)

* Found a solution for sourcing render_lg_table in 08_tables.qmd and successfully rendering extra-wide table with new solution, but need to check if this will work in other circumstances

* found a successful workflow and applied it to indices table. Next step: apply to other tables, and simplify/clarify the new functions

* Copied new changes to bnc table, but referencing a chunk with multiple tables will only reference the last table in the chunk, not the first. So next step: make a chunk for each table.
Also removed now irrelevant function: ID_split_tbls

* Redesigned create_tables_doc so that each table has its own chunk and can be referenced.

* Updated landings table code

* Removed "-plot" from create_figures/tables_doc chunks

* Add note to create_tables_doc explaining why new rda with split tables is made; update documentation

* First draft of render_lg_table test; update documentation

* Remove code from create_tables_doc that reduces row height in tables

* update terminal sb naming to match other conventions

* Added Q&A about figure/table order to FAQs vignette

* Updated create_tables_doc so that split tables include header values in parentheses after caption (example: <indices caption> (Fleet 6, Fleet 7))

* Updated examples

* Clarified comments; renamed some variables to be unique and more clear; removed unnecessary and repetitious code

* Add vignette for convert_output fxn (#161)

* feature(convert output vignette): initial commit for article describing the convert output function

* feature(conout vignette): add in more information including example dataframe (needs adjustment to code chunk)

* minor improvements to convert_output vignette

* update(convert output vignette): add links to function and parts of code; add useful functions outside of asar used in function

* finish(convert output vignette): add in flowchart and final details

* add(conout-vignette): update chunk displatying example data

* Update vignettes/convert_output_details.Rmd

Co-authored-by: Sophie Breitbart <[email protected]>

* Update vignettes/convert_output_details.Rmd

Co-authored-by: Sophie Breitbart <[email protected]>

* Update vignettes/convert_output_details.Rmd

Co-authored-by: Sophie Breitbart <[email protected]>

* Update vignettes/convert_output_details.Rmd

Co-authored-by: Sophie Breitbart <[email protected]>

* fix(conout vignette): clarity to the text per comments made from @sbreitbart-NOAA

* update(conout vignette): add finishing touches to convert output vignette

* Fix translation of R0 parameter from BAM in convert_output (#163)

* fix(convert_output): fix error when parameters containing a number were incorrectly labelled in the final df

* First full draft of vignette re: #156

* fix(dev): remove add_accessibility and add_tagging functions since they are still in development until we can make the change to work alt text and tagging together

* Converting vignette's option comparison table to flextable

* update(add_alttext): change search name for figures to reflect change to rendering naming convention

* update(add_alttext): additional line to find old naming conventions from figures processed from quarto

* note(add_alttext): add TODO in fxn for rendering to html or docx if applicable down the line

---------

Co-authored-by: sbreitbart-NOAA <[email protected]>
Co-authored-by: Ian Taylor <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Schiano-NOAA <[email protected]>
  • Loading branch information
5 people authored Feb 24, 2025
1 parent c8db61f commit b264b58
Show file tree
Hide file tree
Showing 45 changed files with 2,460 additions and 392 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ converted_file_1126.csv
rda_files
Meta
report
spp_conout.csv
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ Imports:
officer,
openxlsx,
prodlim,
quarto,
stats,
stringr,
svDialogs,
tibble,
tidyr,
tidyselect,
utils
tinytex,
utils,
withr
Suggests:
gt,
kableExtra,
Expand All @@ -52,7 +55,6 @@ Suggests:
VignetteBuilder:
knitr
Remotes:
nmfs-fish-tools/nmfspalette,
nmfs-fish-tools/nmfspalette,
nmfs-ost/satf,
r4ss/r4ss
Expand Down
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Generated by roxygen2: do not edit by hand

export(add_alttext)
export(add_base_section)
export(add_child)
export(add_chunk)
export(add_section)
export(add_theme)
export(convert_output)
export(create_citation)
export(create_figures_doc)
Expand All @@ -15,3 +15,4 @@ export(create_title)
export(create_titlepage_tex)
export(export_object)
export(format_quarto)
export(render_lg_table)
212 changes: 212 additions & 0 deletions R/add_alttext.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
#' Add alternative text into latex
#'
#' @param x .tex file containing report. Typically produced after initially
#' rendering the skeleton made from create_template.
#' @param dir directory where the tex file is located that will be edited
#' @param rda_dir folder where rda files containing alternative text is located
#' @param compile Indicate whether the document (X) should be
#' rendered after these files are changed. Default TRUE.
#' @param rename Indicate a name for the new tex file produced from this
#' function. There is no need to include ".tex" in the name. Defaults to current
#' name and overwrites the current tex file.
#' @param alttext_csv_dir Directory for the csv file containing alternative
#' text and captions generated when running satf::exp_all_figs_tables
#'
#' @return This function was made to help add in
#' alternative text to latex documents generated from
#' quarto. Quarto does not currently contain a way to
#' add alternative text to PDF documents, so this function
#' was developed as a work around. The addition of alternative
#' text needs to be found in either the rda files produced from
#' satf::exp_all_figs_tables or in the captions_alt_text.csv also produced from
#' the same function. Users not using this format should create a csv file with
#' columns containing "label" and "alt_text".
#'
#' @export
#'
#' @examples
#' \dontrun{
#' create_template(
#' new_template = TRUE,
#' format = "pdf",
#' office = "NWFSC",
#' region = "U.S. West Coast",
#' species = "Dover sole",
#' spp_latin = "Microstomus pacificus",
#' year = 2010,
#' author = c("John Snow", "Danny Phantom", "Patrick Star"),
#' include_affiliation = TRUE,
#' convert_output = TRUE,
#' resdir = "C:/Users/Documents/Example_Files",
#' model_results = "Report.sso",
#' model = "SS3",
#' new_section = "an_additional_section",
#' section_location = "after-introduction",
#' rda_dir = getwd()
#' )
#'
#' path <- getwd()
#'
#' quarto::quarto_render(file.path(path, "report", "SAR_USWC_Dover_sole_skeleton.qmd"))
#'
#' withr::with_dir(
#' file.path(path, "report"),
#' add_alttext(
#' x = "SAR_USWC_Dover_sole_skeleton.tex",
#' dir = getwd(),
#' alttext_csv_dir = getwd(),
#' rda_dir = path,
#' compile = TRUE,
#' rename = "SAR_Dover_sole_tagged")
#' )
#' }
#'
add_alttext <- function(
x = list.files(getwd())[grep("skeleton.tex", list.files(getwd()))],
dir = getwd(),
rda_dir = getwd(),
alttext_csv_dir = getwd(),
compile = TRUE,
rename = NULL
) {
# Read latex file
tex_file <- readLines(file.path(dir, x))

# Check: count instances of pattern
# sub_count <- length(
# grep("1",
# stringr::str_count(
# tex_file,
# pattern = stringr::coll("\\pandocbounded{\\includegraphics["))
# )
# )

# Identify lines with figures
# this approach allows us to not mistake the replacement for other figures
fig_lines <- grep("fig-([a-z]+|[a-z]+_[a-z]+)-1.pdf", tex_file) # -plot
# Find images from previous naming conventions after quarto render
fig_lines <- c(fig_lines, grep("fig-([a-z]+|[a-z]+_[a-z]+)-plot-1.pdf", tex_file))

# TODO:
# create check to see if there are any instances where the suffix is not plot-1
# Replace instances of macro in the tex file
# replace_macro <- gsub(
# "\\pandocbounded",
# "\\pdftooltip",
# tex_file
# )

# TODO:
# Create alternative options for render to html or docx

# Replace pandocbounded with pdftooltip so alt text can be added
tex_file[fig_lines] <- lapply(
tex_file[fig_lines],
function(line) {
gsub("\\pandocbounded", "\\pdftooltip", line)
}
)

# Check instance of pandocbounded that haven't been replaced
# these are the additional images
addl_figs <- grep("\\pandocbounded", tex_file)[-1]
# ignore line 82 - this is the department of commerce logo
# replace as above
tex_file[addl_figs] <- lapply(
tex_file[addl_figs],
function(line) {
gsub("\\pandocbounded", "\\pdftooltip", line)
}
)
# Add alt text to custom images
# read in alt text csv file to match with labels
alttext <- utils::read.csv(file.path(alttext_csv_dir, "captions_alt_text.csv"))
for (i in addl_figs) {
# Find line label
line <- tex_file[i]
# Find line following target to extract label
matches <- grep("\\label", tex_file)
label_line <- matches[matches > i][1]
line_label <- stringr::str_extract(tex_file[label_line], "\\\\label\\{([^}]*)\\}") |>
stringr::str_remove_all("^\\\\label\\{|\\}$")
# Match label name to label in csv and extract alttext
alttext_i <- alttext |>
dplyr::filter(label == line_label) |>
dplyr::pull(alt_text)
if (is.na(label_line)) {
alttext_i <- ""
warning(glue::glue(
"No alternative text found for {line_label}."
))
}
# Add selected alttext onto end of the line
tex_file[i] <- paste(tex_file[i], "{", alttext_i, "}", sep = "")
}

# Insert alt text for figures
# Call alt text in list with names
obj_files <- list.files(file.path(rda_dir, "rda_files"))

# read all files in obj_files and put into list
alt_text_list <- list()
for (i in seq_along(obj_files)) {
load(file.path(rda_dir, "rda_files", obj_files[i]))
# extract name to add into the list for placement
rda_name <- stringr::str_replace(obj_files[i], "_figure.rda", "")
# if name is >1 word then replace the _ with - to follow naming convention for
# figures in tex file
if (grepl("_", rda_name)) rda_name <- stringr::str_replace(rda_name, "_", "-")
# convert to name in tex file to find where the line is located
tex_name <- glue::glue("fig-{rda_name}-plot-1.pdf")
# extract alt. text with figure
alt_text <- rda$alt_text
# names(alt_text) <- tex_name
# place obj into list
alt_text_list[[tex_name]] <- alt_text
# call tex obj name using names()
# call alt text using list[[i]]
# remove rda file to declutter
rm(rda)
}

# Find where figure is located and append the alt. text
for (i in seq_along(alt_text_list)) {
fig_line <- grep(names(alt_text_list[i]), tex_file)
# Check that line we are adding the alt text to is for correct fig
if (!grepl(names(alt_text_list[i]), tex_file[fig_line])) {
warning(glue::glue("Non-matching object name to tex file line."))
next
}
# Check that selected tex_line contains a marked figure - aka correct placement
file_name <- stringr::str_remove(x, ".tex")
if (!grepl(glue::glue("{file_name}_files/figure-pdf/fig-"), tex_file[fig_line])) {
warning(glue::glue("Improper line for appendment: \n Skipped adding alternative text for {names(alt_text_list[i])}"))
next
}
tex_file[fig_line] <- paste(tex_file[fig_line], "{", alt_text_list[[i]], "}", sep = "")
# tex_file[fig_line] <- strwrap(paste(tex_file[fig_line], "{", alt_text_list[[1]], "}", sep = "")) # remove strwrap if does not render
}
# tex_file[430] <- paste(tex_file[fig_line], "{", alt_text_list[[1]], "}", sep = "")
# strwrap(paste(tex_file[fig_line], "{", alt_text_list[[1]], "}", sep = ""), width = 80)

# Checks
# add check if there are more plots that did not have alt text added
# if (length(obj_files) != length(fig_lines)) {
# # Find which ones were not changed
# # figs_miss_alt <-
# warning("Missing alternative text for the followiing figures:")
# }
# TODO: test case where additional figure is added into the .tex file that is not included in the rda files

# Save overwrite tex file
write(unlist(tex_file), file = file.path(dir, ifelse(!is.null(rename), glue::glue("{rename}.tex"), x)))
# utils::capture.output(cat(tex_file), file = file.path(dir, ifelse(!is.null(rename), glue::glue("{rename}.tex"), x)), append = FALSE)
message("______Alternative text added to tex file.______")
# Render the .tex file after edits
if (compile) {
message("______Compiling in progress - This can take a while...______")
# test if this can be done when skeleton is in different folder than the wd
tinytex::lualatex(file.path(dir, ifelse(!is.null(rename), glue::glue("{rename}.tex"), x)))
message("______Compiling finished______")
}
}
72 changes: 59 additions & 13 deletions R/add_section.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#' @return Add an additional section or subsection to the report template
#' if it is not already present in the default template. This provides
#' the option to add it as a section before or after an existing section,
#' or within a section as a child document.
#' or within a section as a child document. For developers: this function
#' creates a list of sections that will be added to the skeleton file
#' made from create_template.
#' @export
#'
#' @examples add_section(
Expand All @@ -20,22 +22,35 @@ add_section <- function(
new_section = NULL,
section_location = NULL,
custom_sections = NULL,
custom = TRUE,
subdir = NULL) {
# Location options
# before-section
# after-section
# in-section (will always append to the end of the section)
for (i in 1:length(new_section)) {
section_i_name <- paste0(gsub(" ", "_", tolower(new_section[i])), ".qmd")
local_section <- forstringr::str_extract_part(section_location[i], "-", before = FALSE)
local_section_prev <- forstringr::str_extract_part(section_location[i - 1], "-", before = FALSE)
local_section <- forstringr::str_extract_part(
section_location[i], "-", before = FALSE
)
local_section_prev <- forstringr::str_extract_part(
section_location[i - 1], "-", before = FALSE
)

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

section_i <- paste0(
Expand All @@ -45,21 +60,52 @@ add_section <- function(
"\n",
add_chunk("# Insert code", label = "example_chunk"), "\n"
)
utils::capture.output(cat(section_i), file = paste0(subdir, "/", section_i_name), append = FALSE)
utils::capture.output(
cat(section_i),
file = paste0(subdir, "/", section_i_name),
append = FALSE
)

if (locality == "before") {
custom_sections <- append(custom_sections, section_i_name, after = (which(grepl(local_section, custom_sections)) - 1))
custom_sections <- append(
custom_sections,
section_i_name,
after = (which(grepl(local_section, custom_sections)) - 1)
)
} else if (locality == "after") {
if (locality %in% locality_prev & local_section %in% local_section_prev) {
custom_sections <- append(custom_sections, section_i_name, after = which(grepl(gsub(" ", "_", tolower(new_section[i - 1])), custom_sections)))
custom_sections <- append(
custom_sections,
section_i_name,
after = which(grepl(
gsub(" ", "_", tolower(new_section[i - 1])),
custom_sections)
)
)
} else {
custom_sections <- append(custom_sections, section_i_name, after = max(which(grepl(local_section, custom_sections))))
custom_sections <- append(
custom_sections,
section_i_name,
after = max(which(grepl(local_section, custom_sections)))
)
}
} else if (locality == "in") {
stop("No available option for adding a new section 'in' another quarto document.", call. = FALSE)
# stop("No available option for adding a new section 'in' another quarto document.", call. = FALSE)
# recognize locality_prev file
file_for_subsection <- list.files(file.path(subdir))[grep(local_section, list.files(file.path(subdir)))]
# create code for reading in child doc
child_sec <- add_child(
section_i_name,
label = gsub(" ", "_", tolower(new_section[i]))
)
# append that text to file
utils::capture.output(cat(child_sec), file = fs::path(subdir, file_for_subsection), append = TRUE)
# section does not need to be added to appended custom sections as stated above
# creating qmd is already done in line 48

} else {
stop("Invalid selection for placement of section. Please name the follow the format 'placement-section_name' for adding a new section.")
}
}
} # close for loop
custom_sections
}
Loading

0 comments on commit b264b58

Please sign in to comment.