Skip to content

Instantly share code, notes, and snippets.

@k-hench
Created November 6, 2018 10:53
Show Gist options
  • Save k-hench/29beec756206f9dae2a05c6800f3b8f5 to your computer and use it in GitHub Desktop.
Save k-hench/29beec756206f9dae2a05c6800f3b8f5 to your computer and use it in GitHub Desktop.
library(tidyverse)
export_2_latex <- function(table, name){
table %>%
mutate(`\\\\\\hline` = '\\\\') %>%
write_delim(.,path = '.tmp.tex',delim = '&')
# clean last column
n <- names(table) %>% length()
# open latex table
write_lines(str_c('\\begin{tabular}{',
str_c(rep(' c',n),collapse = ''),
' }'), name)
# add table body
read_lines('.tmp.tex') %>%
str_replace(.,'&\\\\',' \\\\') %>%
str_replace(.,'&',' & ') %>%
write_lines(name, append = TRUE)
# close latex table
write_lines('\\end{tabular}', name, append = TRUE)
# remove temporary file
file.remove('.tmp.tex')
message(str_c('Exportet latex table to "',name,'".'))
}
# test
tibble(x = letters[1:10], y = LETTERS[1:10]) %>%
export_2_latex(.,'~/Desktop/test.tex')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment