Add footnotes within your tables in a smart way

add_table_footnotes(
  table,
  footnote = NULL,
  from_col = "",
  to_col = "",
  from_row = "",
  to_row = "",
  delim = ",,,"
)

Arguments

table

The data.frame you are adding footnotes to (and possibly from).

footnote

A string that you want to add as a footnote to the table. Optional.

from_col

What column number or name you want to pull footnotes from.

to_col

What column number or name you want add footnotes to.

from_row

What row number or name you want to pull footnotes from.

to_row

What row number or name you want add footnotes to.

delim

A deliminator string that seperates if you have multiple footnotes stored in a cell. The deliminator can be anything, as long as it does not conflict with anything that can be interpreted by regex. Default = "&&&"

Value

The table "tab" with the footnotes inserted into the table.

Examples

table<-data.frame(col = LETTERS[1:10],
                      x = rnorm(n = 10),
                      y = rnorm(n = 10),
                      footnotes = NA)
table$footnotes[3]<-"Example footnote in a table 1."
table$footnotes[4]<-"Example footnote in a table 2.&&&Example footnote in a table 3."
table[,c("x", "y")] <- NMFSReports::mod_number(table[,c("x", "y")],
                                                     divideby = 1, #'
                                                     comma_seperator = TRUE,
                                                     digits = 2)
# Here, add footnotes from the "footnotes" column to the content in the first column where necessary
table <- add_table_footnotes(tab = table,
                                   from_col = "footnotes", # either use the name of the column
                                   to_col = 1) # or the number of that column in that table
# Here, add a specific footnote to a specific place in the table
table <- add_table_footnotes(tab = table,
                                   footnote = "Example footnote in a table 4.",
                                   to_row = 2,
                                   to_col = 2)
table <- add_table_footnotes(tab = table,
                                   footnote = c("Example footnote in a table 5.",
                                                "Example footnote in a table 6."),
                                   to_row = 4,
                                   to_col = 2)
knitr::kable(table)
#> 
#> 
#> |col                                                                 |x                                      |y     |footnotes                                                       |
#> |:-------------------------------------------------------------------|:--------------------------------------|:-----|:---------------------------------------------------------------|
#> |A                                                                   |1.36                                   |-2.21 |NA                                                              |
#> |B                                                                   |0.10^[Example footnote in a table 4.]  |0.49  |NA                                                              |
#> |C^[Example footnote in a table 1.]                                  |0.34                                   |-1.10 |Example footnote in a table 1.                                  |
#> |D^[Example footnote in a table 2.&&&Example footnote in a table 3.] |-0.57^[Example footnote in a table 5.] |-0.49 |Example footnote in a table 2.&&&Example footnote in a table 3. |
#> |E                                                                   |0.57                                   |1.02  |NA                                                              |
#> |F                                                                   |0.98                                   |-0.37 |NA                                                              |
#> |G                                                                   |0.80                                   |1.11  |NA                                                              |
#> |H                                                                   |-0.14                                  |0.09  |NA                                                              |
#> |I                                                                   |0.87                                   |-0.15 |NA                                                              |
#> |J                                                                   |1.05                                   |0.71  |NA                                                              |