Skip to content

Commit

Permalink
doc: style write* functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kellijohnson-NOAA committed Jun 30, 2023
1 parent 6ea5515 commit ebdbf0c
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 198 deletions.
119 changes: 65 additions & 54 deletions R/write_authors.R
Original file line number Diff line number Diff line change
@@ -1,71 +1,82 @@
#' Compile Authors Names in YAML Form
#'
#' Write a file to the disk with all authors and their affiliations
#' using the yaml form for inclusion in the stock assessment.
#' Write a file to the disk with all authors and their affiliations using the
#' yaml form for inclusion in the stock assessment.
#'
#' @template authors
#' @template fileout
#' @author Kelli F. Johnson
#' @family write
#' @export
#' @examples
#' # An example with a standard first name middle initial full stop and last name
#' # then a first initial full stop middle initial full stop and last name and
#' # last a first name and last name with no middle initial.
#' # An example with a standard first name middle initial full stop and last
#' # name then a first initial full stop middle initial full stop and last
#' # name and last a first name and last name with no middle initial.
#' eg <- utils::capture.output(
#' write_authors(
#' c("Kelli F. Johnson", "E. J. Dick", "Qi Lee"),
#' fileout = ""
#' )
#' write_authors(
#' c("Kelli F. Johnson", "E. J. Dick", "Qi Lee"),
#' fileout = ""
#' )
#' )
#' \dontrun{
#' print(eg)
#' }
write_authors <- function(authors,
fileout = "00authors.Rmd") {
affil <- get_affiliation(authors)
affil_u <- affil[!duplicated(affil)]
matches <- match(affil, affil_u)
persons <- utils::as.person(authors)
first <- vapply(lapply(
strsplit(format(persons, include = "given"), "\\s+"),
"[[", 1
), substr, "character", start = 1, stop = 1)
middle <- vapply(gsub("^[A-Z][a-z]*\\.*\\s*", "", format(persons, include = "given")),
FUN = substr, FUN.VALUE = "character", start = 1, stop = 1, USE.NAMES = FALSE
)
initials <- gsub("\\.\\.", "\\.", sprintf("%1$s.%2$s.", first, middle))
last <- format(persons, include = "family")
citation <- paste(last[1], initials[1], sep = ", ")
if (length(last) > 1) {
citation <- paste(citation, paste(initials[-1], last[-1],
sep = " ", collapse = ", "
), sep = ", ")
} else {
citation <- gsub("\\.*$", "", citation)
}
address <- sprintf(
"^%1$s^%2$s",
seq_along(affil[!duplicated(affil)]), affil[!duplicated(affil)]
)
fileout = "00authors.Rmd") {
affil <- get_affiliation(authors)
affil_u <- affil[!duplicated(affil)]
matches <- match(affil, affil_u)
persons <- utils::as.person(authors)
first <- vapply(
lapply(strsplit(format(persons, include = "given"), "\\s+"), "[[", 1),
substr,
"character",
start = 1,
stop = 1
)
middle <- vapply(
gsub("^[A-Z][a-z]*\\.*\\s*", "", format(persons, include = "given")),
FUN = substr,
FUN.VALUE = "character",
start = 1,
stop = 1,
USE.NAMES = FALSE
)
initials <- gsub("\\.\\.", "\\.", sprintf("%1$s.%2$s.", first, middle))
last <- format(persons, include = "family")
citation <- paste(last[1], initials[1], sep = ", ")
if (length(last) > 1) {
citation <- paste(citation, paste(initials[-1], last[-1],
sep = " ", collapse = ", "
), sep = ", ")
} else {
citation <- gsub("\\.*$", "", citation)
}
address <- sprintf(
"^%1$s^%2$s",
seq_along(affil[!duplicated(affil)]), affil[!duplicated(affil)]
)

xfun::write_utf8(
c(
"---\n",
yaml::as.yaml(column.major = FALSE, indent.mapping.sequence = TRUE, list(
author = data.frame(
name = authors, code = matches,
first = first, middle = middle, family = format(persons, include = "family")
),
author_list = citation,
affiliation = data.frame(
code = matches[!duplicated(affil)],
address = affil[!duplicated(affil)]
),
address = address
)),
"---"
),
con = fileout, sep = ""
)
xfun::write_utf8(
c(
"---\n",
yaml::as.yaml(column.major = FALSE, indent.mapping.sequence = TRUE, list(
author = data.frame(
name = authors,
code = matches,
first = first,
middle = middle,
family = format(persons, include = "family")
),
author_list = citation,
affiliation = data.frame(
code = matches[!duplicated(affil)],
address = affil[!duplicated(affil)]
),
address = address
)),
"---"
),
con = fileout, sep = ""
)
}
124 changes: 61 additions & 63 deletions R/write_bibliography.R
Original file line number Diff line number Diff line change
@@ -1,82 +1,80 @@
#' Write yaml header for bibliography to its own .Rmd file
#'
#' Write the yaml header entry for bibliographies based on
#' `.bib` files found within the directory (`basedirectory`).
#' Users can also search through higher-level directories using `up` and
#' lower-level directories using `recursive`.
#' This function is helpful when using bookdown because you will often
#' be sourcing files from multiple levels and have many bibliography files,
#' which can all be included in a single call within your yaml header.
#' No need to put all references into a single bib file or specify
#' the bib files ahead of time. Let us use R for this!
#' Write the yaml header entry for bibliographies based on `.bib` files found
#' within the directory (`basedirectory`). Users can also search through
#' higher-level directories using `up` and lower-level directories using
#' `recursive`. This function is helpful when using bookdown because you will
#' often be sourcing files from multiple levels and have many bibliography
#' files, which can all be included in a single call within your yaml header. No
#' need to put all references into a single bib file or specify the bib files
#' ahead of time. Let us use R for this!
#'
#' @details
#' Within this package, note that this function is not currently
#' called by any other function, which is fine because each template
#' has its own `.bib` file.
#' Within this package, note that this function is not currently called by any
#' other function, which is fine because each template has its own `.bib` file.
#' You can add `.bib` files later by running this function prior to compiling
#' your document. Or, you could add it to your index/skeleton.Rmd file
#' using a code chunk. For example,
#' `write_bibliography(getwd(), up = 1)` would update the yaml header
#' every time that you compile, but be aware that you will have to run
#' [bookdown::render_book] multiple times to get get this to work.
#' Developers are working on using the code within the yaml to get it
#' to work instantaneously; so, stay tuned.
#' your document. Or, you could add it to your index/skeleton.Rmd file using a
#' code chunk. For example, `write_bibliography(getwd(), up = 1)` would update
#' the yaml header every time that you compile, but be aware that you will have
#' to run [bookdown::render_book] multiple times to get get this to work.
#' Developers are working on using the code within the yaml to get it to work
#' instantaneously; so, stay tuned.
#'
#' @param basedirectory The directory that you want to look for bib files
#' in. If `up = 0`, then this will be the only directory that is searched.
#' @param up An integer value specifying how many directory levels you want
#' to search above `basedirectory`.
#' The default is to only search the base directory.
#' in. If `up = 0`, then this will be the only directory that is searched.
#' @param up An integer value specifying how many directory levels you want to
#' search above `basedirectory`. The default is to only search the base
#' directory.
#' @param recursive A logical value specifying if each directory should be
#' searched for using recursion. The default is to not search recursively
#' because you could have other folders higher up that are not relevant.
#' searched for using recursion. The default is to not search recursively
#' because you could have other folders higher up that are not relevant.
#' @template fileout
#'
#' @author Kelli F. Johnson
#' @export
#' @return A yaml header for markdown either printed to a file or
#' returned to the screen if using `fileout = stdout()`.
#' @return
#' A yaml header for markdown either printed to a file or returned to the screen
#' if using `fileout = stdout()`.
#' @family write
#' @examples
#' # print the output to the screen rather than save to a file
#' write_bibliography(getwd(), fileout = stdout())
write_bibliography <- function(basedirectory = getwd(),
up = 0,
recursive = FALSE,
fileout = "00bibliography.Rmd") {
changedirectory <- basedirectory
bibfiles <- vector()
while (up >= 0) {
bibfiles <- append(
x = bibfiles,
after = length(bibfiles),
values = dir(
path = changedirectory,
recursive = recursive,
pattern = "\\.bib$",
full.names = TRUE
)
)
up <- up - 1
changedirectory <- dirname(changedirectory)
}
relativepaths <- R.utils::getRelativePath(
pathname = bibfiles,
relativeTo = basedirectory
)
if (length(bibfiles) == 0) {
return()
}
up = 0,
recursive = FALSE,
fileout = "00bibliography.Rmd") {
changedirectory <- basedirectory
bibfiles <- vector()
while (up >= 0) {
bibfiles <- append(
x = bibfiles,
after = length(bibfiles),
values = dir(
path = changedirectory,
recursive = recursive,
pattern = "\\.bib$",
full.names = TRUE
)
)
up <- up - 1
changedirectory <- dirname(changedirectory)
}
relativepaths <- R.utils::getRelativePath(
pathname = bibfiles,
relativeTo = basedirectory
)
if (length(bibfiles) == 0) {
return()
}

# Format for yaml
writeLines(
con = fileout,
text = utils::capture.output(
ymlthis::yml_citations(
.yml = ymlthis::yml_empty(),
bibliography = relativepaths
)
)
)
# Format for yaml
writeLines(
con = fileout,
text = utils::capture.output(
ymlthis::yml_citations(
.yml = ymlthis::yml_empty(),
bibliography = relativepaths
)
)
)
}
16 changes: 8 additions & 8 deletions R/write_summary.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#' Write Summary Files
#'
#' Write a file to the disk with the title
#' using the yaml form for inclusion in the stock assessment.
#' Write a file to the disk with the title using the yaml form for inclusion in
#' the stock assessment.
#'
#' @param type A vector of the type of summaries you want included in the
#' stock assessment document. Current options are listed in the default
#' call to \code{write_summary}.
#' @param type A vector of the type of summaries you want included in the stock
#' assessment document. Current options are listed in the default call to
#' `write_summary()`.
#' @template fileout
#'
#' @author Kelli F. Johnson

write_summary <- function(type = c("executive", "onepage"),
fileout = "01summaries.Rmd") {
type <- match.arg(type, several.ok = TRUE)
stop("This function is incomplete.")
fileout = "01summaries.Rmd") {
type <- match.arg(type, several.ok = TRUE)
stop("This function is incomplete.")
}
50 changes: 26 additions & 24 deletions R/write_title.R
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
#' Compile Title in YAML format
#'
#' Write a file to the disk with the title
#' using the yaml form for inclusion in the stock assessment.
#' Write a file to the disk with the title using the yaml form for inclusion in
#' the stock assessment.
#'
#' @details The Latin name as specified in \code{"latin"} argument
#' will be encased in underscores to facilitate it being italicized.
#' Using emph is not allowed because the tagging structure
#' for the creation of the 508 compliant pdf will not render the LaTex
#' code here. Nevertheless, the use of markdown inside the yaml seems
#' to work just fine.
#' @details
#' The Latin name as specified in \code{"latin"} argument will be encased in
#' underscores to facilitate it being italicized. Using emph is not allowed
#' because the tagging structure for the creation of the 508 compliant pdf will
#' not render the LaTex code here. Nevertheless, the use of markdown inside the
#' yaml seems to work just fine.
#'
#' @template species
#' @template latin
#' @template coast
#' @param year A numeric or character string specifying the year.
#' The default uses \code{Sys.Date()} to format the year into a four
#' digit character value.
#' @param year A numeric or character string specifying the year. The default
#' uses [Sys.Date()] to format the year into a four digit character value.
#' @template fileout
#'
#' @author Kelli F. Johnson
#' @family write

write_title <- function(species,
latin,
coast,
year = format(Sys.Date(), "%Y"),
fileout = "00title.Rmd") {
xfun::write_utf8(text = c(
"---\n",
yaml::as.yaml(list(title = paste0(
"Status of ", species, " (_", latin, "_)",
" along the ", coast, " coast in ", year
))),
"---"
), con = fileout, sep = "")
latin,
coast,
year = format(Sys.Date(), "%Y"),
fileout = "00title.Rmd") {
xfun::write_utf8(
text = c(
"---\n",
yaml::as.yaml(list(title = paste0(
"Status of ", species, " (_", latin, "_)",
" along the ", coast, " coast in ", year
))),
"---"
),
con = fileout,
sep = ""
)
}
18 changes: 9 additions & 9 deletions man/write_authors.Rd

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

Loading

0 comments on commit ebdbf0c

Please sign in to comment.