Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit fd8e122

Browse files
Merge pull request #4 from EmilyMarkowitz-NOAA/dev
Dev merge to main
2 parents f1e7a71 + 8a06aea commit fd8e122

File tree

78 files changed

+793
-365
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+793
-365
lines changed

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515

1616
^doc$
1717
^docs$
18+
^documentation$

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ vignettes/*.zip
1515

1616
/doc/
1717
docs
18+
/documentation/

DESCRIPTION

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: NMFSReports
22
Title: Easily write NOAA reports and Tech Memos in R Markdown!
3-
Version: 0.0.1.1
3+
Version: 0.0.1.2
44
Authors@R:
55
person(given = "Emily",
66
family = "Markowitz",
@@ -27,7 +27,8 @@ Imports:
2727
knitcitations,
2828
XML,
2929
googledrive,
30-
flextable
30+
flextable,
31+
utils
3132
Suggests:
3233
testthat (>= 3.0.0),
3334
rmarkdown,

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export(numbers0)
1515
export(numbers2words)
1616
export(numbers2words_th)
1717
export(pchange)
18+
export(range_text)
1819
export(save_equations)
1920
export(save_figures)
2021
export(save_tables)

R/NMFSReports.R

Lines changed: 186 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ buildReport<-function(
6464
# Now... Load those folders with stuff you care about
6565

6666
################## RMD scripts
67+
68+
# Add figtab
69+
file.copy(from = system.file("rmd", "0_figtab.Rmd", package="NMFSReports"),
70+
to = "./code/0_figtab.Rmd",
71+
overwrite = T)
72+
73+
# add other files
6774
a <- list.files(path = system.file("rmd", package="NMFSReports"), pattern = "0_")
6875
b <- c("example", sections)
6976
if (!(is.null(styles_reference_pptx))) {
@@ -221,6 +228,7 @@ buildReport<-function(
221228

222229
# INSERT_SECTIONS
223230
b <- list.files(path = "./code/", pattern = ".Rmd") # find the files that are already there
231+
b <- b[b != "0_figtab.Rmd"]
224232
bb <- strsplit(x = b, split = "_")
225233
sections_no <- unlist(lapply(bb, `[[`, 1))
226234
bb <- strsplit(x = b, split = "[0-9]+_")
@@ -519,22 +527,31 @@ tolower2<-function(str0,
519527
#' This function alows you to take a string of words and combine them into a sentance list. For example, 'apples', 'oranges', 'pears' would become 'apples, oranges, and pears'. This function uses oxford commas.
520528
#' @param x Character strings you want in your string.
521529
#' @param oxford T/F: would you like to use an oxford comma? Default = TRUE
522-
#' @param sep string. default = "," but ";" might be what you need!
530+
#' @param sep string. default = ", " but "; " or " " might be what you need!
531+
#' @param sep_last string. default = " and " but " & " or " , " might be what you need!
523532
#' @keywords strings
524533
#' @export
525-
#' @examples text_list(c(1,2,"hello",4,"world",6))
526-
text_list<-function(x, oxford = TRUE, sep = ",") {
534+
#' @examples
535+
#' text_list(c(1,2,"hello",4,"world",6))
536+
#' text_list(c(1,"world"))
537+
#' text_list(c(1,2,"hello",4,"world",6), oxford = FALSE)
538+
#' paste0("here is a list of things: ",
539+
#' text_list(paste0("list", 1:5), sep = " ", sep_last = ""))
540+
text_list<-function(x = "",
541+
oxford = TRUE,
542+
sep = ", ",
543+
sep_last = "and ") {
527544
x<-x[which(x!="")]
528545
# x<-x[which(!is.null(x))]
529546
x<-x[which(!is.na(x))]
530547
# x<-x[order(x)]
531548
if (length(x)==2) {
532-
str1<-paste(x, collapse = " and ")
549+
str1<-paste(x, collapse = paste0(" ", sep_last))
533550
} else if (length(x)>2) {
534-
str1<-paste(x[1:(length(x)-1)], collapse = paste0(sep, " "))
551+
str1<-paste(x[1:(length(x)-1)], collapse = paste0(sep))
535552
str1<-paste0(str1,
536-
ifelse(oxford == TRUE, sep, ""),
537-
" and ", x[length(x)])
553+
ifelse(oxford == TRUE, sep, " "),
554+
sep_last, x[length(x)])
538555
} else {
539556
str1<-x
540557
}
@@ -1199,6 +1216,69 @@ format_cells <- function(dat, rows, cols, fonttype) {
11991216
return(dat)
12001217
}
12011218

1219+
#' Find a range of numbers for text
1220+
#'
1221+
#' This function outputs the range of values (broken or continuous) as you would want to display it in text.
1222+
#'
1223+
#' @param x A numeric vector of any length. Any duplicates will be removed.
1224+
#' @param dash A string that will go between consecutive values in the string output.
1225+
#' @param oxford Default = TRUE. Will only be used if the vector x provided is not continuous. Inherited from NMFSReports::text_list().
1226+
#' @param sep Default = ", ". Will only be used if the vector x provided is not continuous. Inherited from NMFSReports::text_list().
1227+
#' @param sep_last Default = "and ". Will only be used if the vector x provided is not continuous. Inherited from NMFSReports::text_list().
1228+
#'
1229+
#' @return A string with the range of those values as might be included in a sentence ("1-3, 5, and 7-8").
1230+
#' @export
1231+
#'
1232+
#' @examples
1233+
#' # a typical example
1234+
#' x <- c(2003:2005, 2007, 2010:2012)
1235+
#' range_text(x)
1236+
#' # example has duplicate values out of order and specifies for a different dash and no oxford comma
1237+
#' x <- c(1,2,11,3,4,7,NA,8,3)
1238+
#' range_text(x, dash = "--", oxford = FALSE)
1239+
range_text <- function(x,
1240+
dash = "-",
1241+
oxford = TRUE,
1242+
sep = ", ",
1243+
sep_last = "and ") {
1244+
x <- x[!(is.na(x))]
1245+
x <- x[!duplicated(x)]
1246+
x <- sort(x)
1247+
y <- min(x):max(x)
1248+
z <- setdiff(y, x)
1249+
if (length(z)>0) { # if x is not continuous
1250+
# https://stat.ethz.ch/pipermail/r-help/2010-April/237031.html
1251+
vec <- y
1252+
vec[(vec %in% z)] <- NA
1253+
1254+
# remove consecutive NAs
1255+
foo <- function( x ){
1256+
idx <- 1 + cumsum( is.na( x ) )
1257+
not.na <- ! is.na( x )
1258+
split( x[not.na], idx[not.na] )
1259+
}
1260+
ls <- foo(vec)
1261+
1262+
str <- c()
1263+
for (i in 1:length(ls)) {
1264+
a <- ls[i][[1]]
1265+
if (length(a) == 1){
1266+
str <- c(str, paste0(a))
1267+
} else {
1268+
str <- c(str, paste0(min(a),dash,max(a)))
1269+
}
1270+
}
1271+
str <- NMFSReports::text_list(x = str,
1272+
oxford = oxford,
1273+
sep = sep,
1274+
sep_last = sep_last)
1275+
} else {
1276+
str <- paste0(min(x),dash,max(x))
1277+
}
1278+
return(str)
1279+
}
1280+
1281+
12021282
######## FILE ORGANIZATION #########
12031283

12041284
#' Make numbers the same length preceeded by 0s
@@ -1322,7 +1402,7 @@ save_figures<-function(figure,
13221402

13231403
# Title
13241404
header<-trimws(header)
1325-
header<-paste0(type, " ",cnt,". ",
1405+
header<-paste0(type, " [",cnt,"](){#",nickname,"}. ",
13261406
ifelse(substr(x = header,
13271407
start = nchar(header),
13281408
stop = nchar(header)) %in%
@@ -1333,7 +1413,8 @@ save_figures<-function(figure,
13331413
header,
13341414
paste0(header, paste(paste0("^[", footnotes, "]"),
13351415
collapse = " ^,^ ")))
1336-
filename00<-paste0(filename0, cnt_chapt_content, "_fig_",cnt,
1416+
filename00<-paste0(#filename0,
1417+
cnt_chapt_content, "_fig_",cnt,
13371418
ifelse(filename_desc!="", paste0("_", filename_desc), ""))
13381419

13391420
# Save
@@ -1360,7 +1441,7 @@ save_figures<-function(figure,
13601441
file = paste0(path, filename00,
13611442
".csv"),
13621443
sep = ",",
1363-
row.names=FALSE, col.names = F, append = F)
1444+
row.names=FALSE, col.names = TRUE, append = F)
13641445
# }
13651446
} else {
13661447
raw <- ""
@@ -1463,7 +1544,7 @@ save_tables<-function(table_raw = NULL,
14631544
# Title
14641545
header<-trimws(header)
14651546
# header<-stringr::str_to_sentence(header)
1466-
header<-paste0(type, " ",cnt,". ",
1547+
header<-paste0(type, " [",cnt,"](){#",nickname,"}. ",
14671548
ifelse(substr(x = header,
14681549
start = nchar(header),
14691550
stop = nchar(header)) %in%
@@ -1474,7 +1555,8 @@ save_tables<-function(table_raw = NULL,
14741555
header,
14751556
paste0(header, paste(paste0("^[", footnotes, "]"),
14761557
collapse = " ^,^ ")))
1477-
filename00<-paste0(filename0, cnt_chapt_content, "_tab_",cnt,
1558+
filename00<-paste0(#filename0,
1559+
cnt_chapt_content, "_tab_",cnt,
14781560
ifelse(filename_desc!="", paste0("_", filename_desc), ""))
14791561
# Save
14801562
if (!is.null(path)){
@@ -1488,7 +1570,7 @@ save_tables<-function(table_raw = NULL,
14881570
file = paste0(path, filename00,
14891571
"_raw.", output_type[i]),
14901572
sep = ",",
1491-
row.names=FALSE, col.names = F, append = F)
1573+
row.names=FALSE, col.names = TRUE, append = F)
14921574
}
14931575
} else {
14941576
table_raw <- ""
@@ -1588,7 +1670,7 @@ save_equations<-function(equation,
15881670
# Title
15891671
header<-trimws(header)
15901672
# header<-stringr::str_to_sentence(header)
1591-
header<-paste0(type, " ",cnt,". ",
1673+
header<-paste0(type, " [",cnt,"](){#",nickname,"}. ",
15921674
ifelse(substr(x = header,
15931675
start = nchar(header),
15941676
stop = nchar(header)) %in%
@@ -1613,49 +1695,124 @@ save_equations<-function(equation,
16131695
return(list_equations )
16141696
}
16151697

1616-
#' Reference a figure, table, or equation (or other list elements!)
1698+
#' Reference a figure, table, or equation with an anchored tag
16171699
#'
16181700
#' @param list_obj A list object created by list_figures or list_tables.
16191701
#' @param nickname A unique string that is used to identify the plot or table in list_figures or list_tables, respectively.
16201702
#' @param sublist A string of the sublist in list_figures or list_tables you want the contents returned from.
1703+
#' @param exact T/F. If TRUE, 'nickname' must match the name of the list item exactly. If FALSE, crossref will return all entries with that string fragment. Default = TRUE.
1704+
#' @param text T/F. If TRUE, will output results prepared for a text output. If FALSE, will output each element. Default = TRUE.
16211705
#' @return The item in the list.
16221706
#' @export
16231707
#' @examples
16241708
#' list_figures <- c()
1625-
#' pp <- plot(x = 1, y = 1)
1709+
#' table_raw <- data.frame(x = 1, y = 1)
1710+
#' pp <- plot(x = table_raw$x, y = table_raw$y)
16261711
#' list_figures <- NMFSReports::save_figures(
16271712
#' figure = pp,
16281713
#' list_figures = list_figures,
16291714
#' header = "blah blah blah",
1630-
#' nickname = "example_figure", # a unique name you can refer back to
1715+
#' nickname = "example_1", # a unique name you can refer back to
16311716
#' cnt_chapt_content = "003",
16321717
#' cnt = "012")
16331718
#' list_figures <- NMFSReports::save_figures(
16341719
#' figure = pp,
16351720
#' list_figures = list_figures,
16361721
#' header = "blah blah blah",
1637-
#' nickname = "example_figure_other", # a unique name you can refer back to
1722+
#' nickname = "example2", # a unique name you can refer back to
16381723
#' cnt_chapt_content = "003",
16391724
#' cnt = "013")
16401725
#' list_figures
1641-
#' refnum <- NMFSReports::crossref(
1726+
#' refnum <- crossref(
16421727
#' list_obj = list_figures,
1643-
#' nickname = "example_figure",
1728+
#' nickname = "example_1",
16441729
#' sublist = "number")
16451730
#' refnum
16461731
#' print(paste0("Please refer to figure ", refnum,
16471732
#' " to see this figure, not the other figure."))
1733+
#' # example using a partial phrase with `exact = FALSE`
1734+
#' crossref(
1735+
#' list_obj = list_figures,
1736+
#' nickname = "example_",
1737+
#' sublist = "number",
1738+
#' exact = FALSE)
1739+
#' # using a wildard with `exact = FALSE`
1740+
#' crossref(
1741+
#' list_obj = list_figures,
1742+
#' nickname = "example*1",
1743+
#' sublist = "number",
1744+
#' exact = FALSE)
1745+
#' crossref(
1746+
#' list_obj = list_figures,
1747+
#' nickname = "example*",
1748+
#' sublist = "number",
1749+
#' exact = FALSE,
1750+
#' text = FALSE)
1751+
#' refnum <- crossref(
1752+
#' list_obj = list_figures,
1753+
#' nickname = "example*",
1754+
#' sublist = "number",
1755+
#' exact = FALSE,
1756+
#' text = TRUE)
1757+
#' refnum
1758+
#' print(paste0("Please refer to figure ", refnum,
1759+
#' " to see this figure, not the other figure."))
16481760
crossref <- function(list_obj,
16491761
nickname,
1650-
sublist = "number"){
1651-
ref <- list_obj[which(lapply(list_obj, `[[`, "nickname") %in% nickname)][[1]][sublist]
1762+
sublist = "number",
1763+
exact = TRUE,
1764+
text = TRUE){
1765+
nickname0<-nickname
1766+
if (!exact) {
1767+
nickname <- c()
1768+
for (i in 1:length(nickname0)){
1769+
if (grepl(nickname0[i], pattern = "*", fixed = TRUE)) { # if the name uses a wildcard
1770+
nickname <- c(nickname,
1771+
names(list_obj)[grepl(pattern = utils::glob2rx(nickname0[i]),
1772+
x = names(list_obj))])
1773+
} else { # if there is no wildcard character
1774+
nickname <- c(nickname,
1775+
names(list_obj)[grepl(pattern = (nickname0[i]),
1776+
x = names(list_obj))])
1777+
}
1778+
}
1779+
}
1780+
# ref <- list_obj[which(lapply(list_obj, `[[`, "nickname") %in% nickname)][[1]][sublist]
1781+
ref <- lapply(list_obj[names(list_obj) %in% nickname], `[[`, sublist)
1782+
16521783
if (sublist == "number") {
1653-
ref<-as.character(ref)
1784+
# ref<-as.character(ref)
1785+
ref<-paste0("[", ref, "](#", nickname, ")")
1786+
if (text) {
1787+
# setdiff()
1788+
if (length(ref)>5) {
1789+
ref <- paste0(ref[1], " to ", ref[length(ref)])
1790+
} else {
1791+
ref <- NMFSReports::text_list(ref)
1792+
}
1793+
}
1794+
} else if (sublist == "res") {
1795+
if(text) {
1796+
if (exact) {
1797+
# TOLEDO ref was missing here?
1798+
ref <- sapply(list_obj[grepl(x = names(list_obj), pattern = nickname0)],"[[", sublist)
1799+
} else {
1800+
ref <- paste(ref, sep = "", collapse = "
1801+
1802+
")
1803+
}
1804+
}
1805+
} else if (sublist == "raw") {
1806+
ref <- ref#sapply(list_obj[grepl(x = names(list_obj), pattern = nickname0)],"[[", sublist)
1807+
if (length(ref)==1) {
1808+
ref <- ref[[1]]
1809+
}
1810+
16541811
}
16551812
return(ref)
16561813
}
16571814

1658-
ref_listobject<-crossref
1815+
# ref_listobject<-crossref
16591816

16601817

16611818
# Adapted from flextable::theme_vanilla()
@@ -1676,7 +1833,8 @@ ref_listobject<-crossref
16761833
#' @param font String. Default = "Times New Roman". Instead, you may want "Arial".
16771834
#' @param body_size Numeric. default = 11.
16781835
#' @param header_size Numeric. default = 11.
1679-
#' @param spacing table spacing. default = .5
1836+
#' @param spacing table spacing. default = 1
1837+
#' @param pad padding around each element. default = 0.1
16801838
#' @family functions related to themes
16811839
#' @examples
16821840
#' ft <- flextable::flextable(head(airquality))
@@ -1691,7 +1849,8 @@ theme_flextable_nmfstm <- function(x,
16911849
body_size = 11,
16921850
header_size = 11,
16931851
font = "Times New Roman",
1694-
spacing = .9) {
1852+
spacing = 1,
1853+
pad = 0.1) {
16951854

16961855
if (!inherits(x, "flextable")) {
16971856
stop("theme_flextable_nmfstm supports only flextable objects.")
@@ -1719,6 +1878,7 @@ theme_flextable_nmfstm <- function(x,
17191878
x <- flextable::bold(x = x, bold = TRUE, part = "header")
17201879
x <- flextable::align_text_col(x = x, align = "left", header = TRUE)
17211880
x <- flextable::align_nottext_col(x = x, align = "right", header = TRUE)
1881+
x <- flextable::padding(x = x, padding = pad, part = "all") # remove all line spacing in a flextable
17221882
x <- flextable::font(x = x, fontname = font, part = "all")
17231883
x <- flextable::fontsize(x = x, size = body_size, part = "body")
17241884
x <- flextable::fontsize(x = x, size = header_size, part = "header")

0 commit comments

Comments
 (0)