I want to output graphs to a single tex file using {tikzDevice}
, I wrote following loop trying to make it happen:
library{tikzDevice}
graphList <- list(...) # ... are the graphs I have made using ggplot2
for (i in 1:length(graphList)) {
cat("\\begin{figure}\n", file = "GraphList.tex", append=TRUE)
sink("GraphList.tex", append=TRUE)
tikz(console = TRUE)
graphList[[i]]
dev.off()
sink()
cat(paste0("\\caption{", names(graphList)[[i]],"}",sep=" "),
file = "GraphList.tex", append=TRUE)
cat("\\end{figure}\n", file = "GraphList.tex", append=TRUE)
}
Sometimes it works but sometimes does not by just writing what I put in the cat
part into tex
file without graphs.
I am pretty newbie to this, could anyone help me out of here? Many Thanks!
Updated:
The following code works (Suppose I only have 2 graphs in list for example):
sink("Output/graph/GraphList.tex", append=TRUE)
tikz(console = TRUE)
cat("\\begin{figure}\n")
graphList[[1]]
cat(paste0("\\caption{", names(graphList)[[1]],"}",sep=" "))
cat("\n\\end{figure}\n")
cat("\n\\begin{figure}\n")
graphList[[2]]
cat(paste0("\\caption{", names(graphList)[[2]],"}",sep=" "))
cat("\n\\end{figure}\n")
sink()
So my wild guess is that R
does not write loop part into a file, is that correct?