Created
November 6, 2018 10:53
-
-
Save k-hench/29beec756206f9dae2a05c6800f3b8f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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