Skip to content

Instantly share code, notes, and snippets.

@mczyzj
Created October 3, 2018 06:12
Show Gist options
  • Save mczyzj/09d04567dd71110c4888a8b27f4681ef to your computer and use it in GitHub Desktop.
Save mczyzj/09d04567dd71110c4888a8b27f4681ef to your computer and use it in GitHub Desktop.
Citations in RMarkdown tables
# This snippets allows quick change from markdown citation syntax (i.e. @smith2018) to LaTeX syntax.
# It is particularly useful when you need to use both html and LaTeX output and don't want to use
# two separate tables for each output. In this case you can use simple conditional that changes RMarkdown
# syntax to LaTeX when needed.
df <- data.frame(book = c('Book one', 'Book two'), reference = c('@smith2005', '@foo2006')
dfR <- df$reference
dfR %>%
gsub('@', '', .) %>%
strsplit('; ') %>%
lapply(formatingCite) %>%
lapply(paste, collapse = '; ') %>%
unlist %>%
data.frame(references = .) %>%
bind_cols(df, .) %>%
select(-'reference')
dfR$references[dfR$references == '\\citeauthor{}, \\hyperlink{ref-}{\\citeyear{}}'] <- NA
@mczyzj
Copy link
Author

mczyzj commented Oct 3, 2018

For real life example please check templates in pestrPRA package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment