knitr::opts_chunk$set(
  message = FALSE, echo = TRUE, warning = FALSE, 
  comment = FALSE, collapse = TRUE, include = TRUE, 
  comment = "#>"
)
options(rmarkdown.html_vignette.check_title = FALSE)

Load Libraries

library(NMFSReports)
library(magrittr)
library(ggplot2)
library(png)
library(cowplot)
library(magick)

This file is essentially the example file included in the buildReport() function. The raw vignette script for this can be found here and the example script can be found here.

From run file:

cnt_chapt <- "000" # Keep everything in a proper order
cnt_chapt_content <- 0
cnt_equations <- 0
cnt_tables <- 0
cnt_figures <- 0
list_equations <- list()
list_tables <- list()
list_figures <- list()
dir_out_figures <- dir_out_tables <- NULL # I dont actually want to save them in this example
filename0<-"example"
indesign_flowin<-FALSE # not going into a final publication that requires indesign for final touches

Example of how to use this R Markdown document

# example data
dat <- data.frame(x = rnorm(n = 100), 
                  y = rnorm(n = 100), 
                  col = rep_len(x = c("a", "b"), length.out = 5))

Footnotes.list<-list("ExOfStandardFt" = "Wow, this project is so cool!")

types_of_vessels <- c("NOAA Vessel", "F/V Fishing Boat", "R/V University Vessel", "Private Charter")

Here is the report content:

Systematic writing of content

My example dataset has r xunits(nrow(dat)) rows in it and r xunits(ncol(dat)) columns in it.r paste0("^[", Footnotes.list$ExOfStandardFt,"]")

This sentence exemplifies how to systematically calculate a percent change: r pchange(start = nrow(dat), end = ncol(dat))^[Here's another, free-written footnote!].

Here are several types of vessels: r NMFSReports::text_list(types_of_vessels).

Equations

Here is some equations you'll need to have in this report:

equation = "c^2 = b^2 + a^2"
nickname = "pythagorean"
header = "Pythagorean theorem"
footnote = "footnote about how cool the pythagorean theorem is."
alttext = "The Pythagoras theorem is a mathematical law that states that the sum of squares of the lengths of the two short sides of the right triangle is equal to the square of the length of the hypotenuse."

# save yo' stuff and do a lot of behind the scenes work
# alt: check out the "child = " in this next chunk header (which must stay empty)
# ```r

# ```

[Text blah blah]

equation = "F = G \\frac {m_1 m_2}{d^2}"
nickname = "Newton"
header = "Newton's Universal Law of Gravitation"

# save yo' stuff and do a lot of behind the scenes work
# alt: this does the same thing as calling "child = " in the chunk header
res <- knitr::knit_child(
  text = knitr::knit_expand(
    file = system.file("rmd/_child_save_eq.Rmd", package = "NMFSReports") ), 
  quiet = TRUE
)
# `r res `

r res

Remember the pythagorean theorem from before? That was Equation #r crossref(list_obj = list_equations, nickname = "pythagorean", sublist = "number") and it had a footnote that said "r crossref(list_obj = list_equations, nickname = "pythagorean", sublist = "footnote")".

Citations

There are a lot of ways to cite within your report and here is an example of two: [@RN686] and @RN621. Checkout https://github.com/cboettig/knitcitations for more info! For this method, you'll need a .bib file that will be used in the YAML of the RMarkdown document. If you are an EndNote, as I am, you can export .bib files using these guides.

Note that references will appear at the bottom of this document (and any document that you put references in) but when you combine this document with all of the other documents that you create, it will shift to the bottom of the report.

Example Figure (Plot)

header <- "Here is a figure!"
footnote<-c("A footnote for this figure!", "A second footnote for this figure!")
nickname <- "ex_plot"
alttext <- "This is a scatter plot of random data."

# Select data and make plot
figure <- dat %>%
  ggplot(aes(x = x, y = y, colour = as.factor(col))) + # create plot
  geom_point() 

# save yo' stuff and do a lot of behind the scenes work
# alt: check out the "child = " in this next chunk header (which must stay empty)
# ```r
# ```

Here in plot r crossref(list_obj = list_figures, nickname = "ex_plot", sublist = "number"), we see that...

Example Figure (Reference Image)

header <- "Here is a figure!"
footnote<-c("A footnote for this figure!")
nickname <- "noaalogo"
filename_desc <- "noaalogo"
alttext <- "The NOAA Meatball and text. "

# Select data and make plot
figure <- 
  cowplot::ggdraw() +
  cowplot::draw_image(readPNG(system.file("img/NOAA_Fisheries_logo_vertical.png", package = "NMFSReports")) )

# save yo' stuff and do a lot of behind the scenes work
# alt: this does the same thing as calling "child = " in the chunk header
res <- knitr::knit_child(
  text = knitr::knit_expand(
    file = system.file("rmd/_child_save_fig.Rmd", package = "NMFSReports")), 
  quiet = TRUE
)
# `r res `

r res

Here in figure r crossref(list_obj = list_figures, nickname = "noaalogo", sublist = "number"), we see the NOAA logo.

Example Table (Basic)

Here, we use the function theme_flextable_nmfstm which mimics many of the NMFS report tech memo motifs.

header <- "Here is a table!"
footnote<-"A footnote for this table!"
nickname <- "ex_table" # this is so you can refer to it later systematically
alttext <- "This is a 3-column table of random letters (in the first column) and numbers (in the second and third columns). "

# Select data and make plot

# Create data-saver version of table that will be used to reference things later saved as backup
table_raw<-data.frame(col = LETTERS[1:10], 
                      x = rnorm(n = 10), 
                      y = rnorm(n = 10)) 

# Create pretty version of table that will go into report
table_print <- table_raw
table_print[,c("x", "y")] <- 
  NMFSReports::mod_number(table_print[,c("x", "y")], 
                          divideby = 1, 
                          comma_seperator = TRUE, 
                          digits = 2)

table_print <- flextable::flextable(table_print) %>% 
    NMFSReports::theme_flextable_nmfstm(x = .)

# save yo' stuff and do a lot of behind the scenes work
# alt: check out the "child = " in this chunk header (which must stay empty)
# ```r
# ```

Here in table r crossref(list_obj = list_tables, nickname = "ex_table", sublist = "number"), we see that...

Example Table (With Footnotes)

header <- "Here is a table!"
nickname <- "ex_table_foot" # this is so you can refer to it later systematically

# Select data and make plot

# Create data-saver version of table that will be used to reference things later saved as backup
table_raw<-data.frame(col = LETTERS[1:10], 
                      x = rnorm(n = 10), 
                      y = rnorm(n = 10), 
                      footnotes = NA) 

table_raw$footnotes[3]<-"Example footnote in a table 1."
table_raw$footnotes[4]<-"Example footnote in a table 2.,,,Example footnote in a table 3."

footnote<-"A table with footnotes!"
footnote<-c(footnote, 
            ifelse(NMFSReports::is_something_in_this_matrix(
  x = table_raw, 
  search_for = "J"), 
  "There is a 'J' in this matrix!", 
  ""))

# Create pretty version of table that will go into report
table_print <- table_raw
table_print[,c("x", "y")] <- NMFSReports::mod_number(table_print[,c("x", "y")], 
                                                     divideby = 1, 
                                                     comma_seperator = TRUE, 
                                                     digits = 2)

# example of how to add footnotes from a column of footnotes
# here, we'll add footnotes from the "footnotes" column to the content in the first column, where necessary
table_print <- NMFSReports::add_table_footnotes(table = table_print, 
                                   from_col = "footnotes", # either use the name of the column or number
                                   to_col = 1, # or the number of that column in that table
                                   delim = ",,,") 
table_print$footnotes<-NULL # remove column from final table

# here, I'll add a specific footnote to a specific place in the table
table_print <- NMFSReports::add_table_footnotes(table = table_print, 
                                   footnote = "Example footnote in a table 4.", 
                                   to_row = 2, 
                                   to_col = 2)

# apply multiple footnotes to multiple (same number) spots
table_print <- NMFSReports::add_table_footnotes(table = table_print, 
                                   footnote = c("Example footnote in a table 5.", 
                                                "Example footnote in a table 6."), 
                                   to_row = 2:3,
                                   to_col = 2)


# Format table 
table_print <- table_print %>%
  NMFSReports::format_cells(rows = 0, # make column names
               cols = 1:ncol(table_print), # for all columns
               fonttype = "bold") #%>% # bold
  # knitr::kable(row.names = FALSE, booktabs = TRUE) #print table in text

# save yo' stuff and do a lot of behind the scenes work
# alt: this does the same thing as calling "child = " in the chunk header
res <- knitr::knit_child(
  text = knitr::knit_expand(
    file = system.file("rmd/_child_save_tab.Rmd", package = "NMFSReports")), 
  quiet = TRUE
)
# `r res `

r res

Here in table r crossref(list_obj = list_tables, nickname = "ex_table_foot", sublist = "number"), we see that...

References



EmilyMarkowitz-NOAA/NMFSReports documentation built on March 26, 2023, 1:08 a.m.