vignettes/C_functions-to-help-write-report.Rmd
C_functions-to-help-write-report.Rmd
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.
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 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:
My example dataset has 100 rows in it and three columns in it.1
This sentence exemplifies how to systematically calculate a percent change: a 97% decrease2.
Here are several types of vessels: NOAA Vessel, F/V Fishing Boat, R/V University Vessel, and Private Charter.
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, child = system.file("rmd/_child_save_eq.Rmd", package = "NMFSReports")}
# ```
Equation 1. Pythagorean theorem.3
\[c^2 = b^2 + a^2\]
Alternative text: 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.
[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 `
Equation 2. Newton’s Universal Law of Gravitation.
\[F = G \frac {m_1 m_2}{d^2}\]
Alternative text: Newton’s Universal Law of Gravitation
Remember the pythagorean theorem from before? That was Equation #1 and it had a footnote that said “footnote about how cool the pythagorean theorem is.”.
There are a lot of ways to cite within your report and here is an example of two: (Fry 1957) and Arrhenius (1889). 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.
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, child = system.file("rmd/_child_save_fig.Rmd", package = "NMFSReports")}
# ```
#> NULL
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 `
#> NULL
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, child = system.file("rmd/_child_save_tab.Rmd", package = "NMFSReports")}
# ```
Table 1. -- This is a 3-column table of random letters (in the first column) and numbers (in the second and third columns). | ||
col |
x |
y |
A |
-0.39 |
0.08 |
B |
1.71 |
-1.66 |
C |
0.42 |
1.31 |
D |
0.52 |
1.04 |
E |
-2.20 |
1.59 |
F |
-3.00 |
1.35 |
G |
-1.02 |
-0.39 |
H |
-1.12 |
-0.28 |
I |
1.61 |
-0.66 |
J |
1.93 |
1.19 |
Here in table 1, we see that…
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 `
Wow, this project is so cool!↩︎
Here’s another, free-written footnote!↩︎
footnote about how cool the pythagorean theorem is.↩︎
Example footnote in a table 4.↩︎
Example footnote in a table 5.↩︎
Example footnote in a table 1.↩︎
Example footnote in a table 6.↩︎
Example footnote in a table 2.↩︎
Example footnote in a table 3.↩︎