[R] How to use Latex code in R loop?
Duncan Mackay
mackay at northnet.com.au
Mon Apr 2 01:20:04 CEST 2012
Hi
There are a number of packages that produce latex from R, Sweave,
brew, knitr - ones that come to mind. It happens that i use Sweave.
The package you finally use may depend on your preferences
The graphic output in R may also depend on what latex you use and
other requirements - eg if you want to use it in a publication or share.
Below is rather complex code from what I have done in the past in a
loop and altered to give you an idea.
It was done this way for a purpose as there may be better and more
efficient ways to do it
Basically :
1 The data is imported in a Sweave chunk
2 A tex file is created to add the figures in latex
3 A loop is created to print the graphs to go into latex and the
latex code is added to the tex file for each graphic
4 the tex file is added to the sweave document as \input
# import file
data <- read.table(x)
# Create Latex file for figures and \input
ftex <-
"D:/Cic/Sweave/Sheep/AWTA/11/SheepWool-AWTA-Data11PRa.tex"
if (file.exists(ftex) ) file.remove(ftex)
file.create(ftex)
# open to append
ff <- file(ftex, "a+")
# Store macro text
fchars <- ""
# Plot data ? pdf
# numeric columns (not dates which are the first)
for (j in c(0:2) ) {
pdf(file = filename etc)
print(graph eg xyplot)
dev.off()
# Comment
if (j == 0 ){
fchars <-
paste(fchars,
"\\newpage\n\n",
"%******************************\n",
"% splom ofdaa2[,c(34:36)]|paste(FarmID,YearOfTesting)",
"ex label{CWW.7.2.1}\n",
sep = "")
} ## j == 1 **
# R macro
fchars <-
paste(fchars, "\n",
paste("%", yvar, "\n",
"\\begin{landscape}\n"),
paste("\\begin{figure}\n\\centering\n"),
paste("\\includegraphics[height=0.88\\textheight,%\n",
" trim=0.5in 0.5in 0.5in 0.5in,%\n",
" keepaspectratio=true]%\n"),
paste(" {", filename, "}\n", sep = ""),
paste("\\pdfbookmark[3]{Pairs of ",
c("Hogget ","Ewe ","Wether ")[(j+1)], yvar, "}{",
"CWW.7.3.", j,
"}\\label{", "CWW.7.3.", j, "}\n", sep = ""),
paste("\\caption{",
"Pairs plot of ", c("Hogget ","Ewe ","Wether ")[(j+1)],
yvar, " against ",
"\\textsf{YearOfTesting, FarmID}:\ ",
basenom, "}\n", sep = ""),
paste("\\end{figure}\n\n",
"\\end{landscape}\n",
"\\clearpage\n\n", sep = ""),
sep = "")
} ## for (j in seq_along(ysufbwo) )
# write % filename and close
writeLines(fchars, ff)
close(ff)
REgards
Duncan
Duncan Mackay
Department of Agronomy and Soil Science
University of New England
ARMIDALE NSW 2351
Email home: mackay at northnet.com.au
At 00:48 2/04/2012, you wrote:
>Hi,
>
>I am newbie in Latex and R. I am working on one report in which i need to
>read file and display content of file by formatting (adding color boxes and
>colorful text for each record). For this i need to use latex code in R loop.
>How can i use Latex code in R loop. Any example will help me a lot.
>
>Thanks
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/How-to-use-Latex-code-in-R-loop-tp4523544p4523544.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list