[R] How to use Latex code in R loop?

Henrik Bengtsson hb at biostat.ucsf.edu
Mon Apr 2 02:32:07 CEST 2012


On Sun, Apr 1, 2012 at 4:20 PM, Duncan Mackay <mackay at northnet.com.au> wrote:
> 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)

FYI (and to advertise the R.rsp package), to avoid all that
error-prone escape/cat/paste/file stuff, you can turn it all "inside
out" by instead using the below file
'SheepWool-AWTA-Data11PRa.tex.rsp' and run
R.rsp::rsp("SheepWool-AWTA-Data11PRa.tex.rsp") to generate the wanted
SheepWool-AWTA-Data11PRa.tex and *.pdf image files.  You could even
use RSP directly on an Sweave file, e.g. 'report.Rnw.rsp' and generate
the PDF via R.rsp::rsp("report.Rnw.rsp") - it will process RSP and the
Sweave vignette automagically.  See vignette 'Dynamic LaTeX reports
with RSP' of R.rsp package for more details:

  http://cran.r-project.org/web/packages/R.rsp/


- - FILE START - -

%% Filename: SheepWool-AWTA-Data11PRa.tex.rsp
%% Usage: R.rsp::rsp("SheepWool-AWTA-Data11PRa.tex.rsp")
%% Outputs: SheepWool-AWTA-Data11PRa.tex and *.pdf files.

<%
# Change default output path for figures to current directory
options("devEval/args/path"=".")
%>

<%
# import file
data <- read.table(x)
%>

<% for (j in 0:2) { %>
<% if (j == 0) { %>
\newpage
%******************************
% splom ofdaa2[,c(34:36)]|paste(FarmID,YearOfTesting) ex label{CWW.7.2.1}
<% } # if (j == 0) %>
% <%=yvar%>
\begin{landscape}
 \begin{figure}
  \centering
  \includegraphics[height=0.88\textheight,%
                   trim=0.5in 0.5in 0.5in 0.5in,%
                   keepaspectratio=true]%
     {<%=toPDF("MyFigure", tags=j, aspectRatio=0.8, scale=1.3, {
      # Your R plot code here
      # print(graph eg xyplot)
      })%>}
  \pdfbookmark[3]{Pairs of <%=c("Hogget ","Ewe ","Wether
")[(j+1)]%><%=yvar%>}{CWW.7.3.<%=j%>}
  \label{CWW.7.3.<%=j%>}
  \caption{Pairs plot of <%=c("Hogget ","Ewe ","Wether
")[(j+1)]%><%=yvar%> against \textsf{YearOfTesting, FarmID}:
<%=basenom%>}
 \end{figure}
\end{landscape}
\clearpage
<% } # for (j in 0:2) %>

- - FILE END - -

/Henrik

>
> 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.
>
>
> ______________________________________________
> 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