Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Citing within an RMarkdown table

I am attempting to create a table which has citations built into the table. Here is a visual of what I am trying to achieve.

desired output

As far as I know you can only add footnotes in rowvars or colvars in kableExtra (love that package).

# Create a dataframe called df
Component <- c('N2','P3')
Latency <- c('150 to 200ms', '625 to 800ms')
Location <- c('FCz, Fz, Cz', 'Pz, Oz')
df <- data.frame(Component, Latency, Location)

Below is my attempt after reading through kableExtra's Git page

# Trying some code taken from the kableExtra guide
row.names(df) <- df$Component
df[1] <- NULL
dt_footnote <- df
names(dt_footnote)[1] <- paste0(names(dt_footnote)[2],
                                footnote_marker_symbol(1))

row.names(dt_footnote)[2] <- paste0(row.names(dt_footnote)[2], 
                                footnote_marker_alphabet(1))
kable(dt_footnote, align = "c", 
      # Remember this escape = F
      escape = F, "latex", longtable = T, booktabs = T, caption = "My Table Name") %>%
  kable_styling(full_width = F) %>%
  footnote(alphabet = "Jones, 2013",
           symbol = "Footnote Symbol 1; ",
           footnote_as_chunk = T)

Output

But this code only works on the headers. The ultimate goal would be if I could use a BibTex reference such as @JonesFunctionalMixedEffectModels2013 such that the final part of the code would look like

footnote(alphabet = @davidsonFunctionalMixedEffectModels2009,
           symbol = "Footnote Symbol 1; ", footnote_as_chunk = T)

Anyone have any ideas?

Thanks

like image 454
Patrick Avatar asked Aug 13 '26 17:08

Patrick


2 Answers

What I did at the end was to generate a temporary table with pander, then copy the references' number manually to my kable

pander(
  df,
  caption = "Temporal",
  style = "simple",
  justify = "left")
like image 97
Julio Avatar answered Aug 16 '26 19:08

Julio


It depends where you're writing (RStudio/ other IDE/ Overleaf) and what format you're using for the output (I assume pdf, but HTML is also an option).

I've been mainly using Quarto for my writing lately, so here's my attempt, but you should be able to adjust it to whatever format you need.

library(kableExtra)
library(knitr)

Component <- c('N2','P3')
Latency <- c('150 to 200ms', '625 to 800ms')
Location <- c('FCz, Fz[@wang_levetiracetam_2017], Cz[@roeske_modulation_2023]', 'Pz, Oz')
df <- data.frame(Component, Latency, Location)

table <- knitr::kable(df, format='simple')
kable_styling(table, full_width = F)

and this is what the output looks like for the whole PDF document:

like image 21
Julia Avatar answered Aug 16 '26 20:08

Julia