2

I have five images stored as follows (where "currentDirectory" is the result I get from the command getwd()):

currentDirectory/results/thePlot_1.jpg
currentDirectory/results/thePlot_2.jpg
currentDirectory/results/thePlot_3.jpg
currentDirectory/results/thePlot_4.jpg
currentDirectory/results/thePlot_5.jpg

I am trying to write a .Rnw script in Rstudio that will create the .tex file that I can then run pdflatex on to have a .pdf file containing these five images. Below is what I have tried:

\documentclass{article}
\usepackage{float, hyperref}
\usepackage[margin=1in]{geometry}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{caption}
\usepackage{algorithm}
\usepackage{algorithmic}

\begin{document}
\SweaveOpts{concordance=TRUE}

\author{myName}
\title{myTitle}

\maketitle

<<options, echo=FALSE>>=
library(knitr)
  opts_chunk$set(cache=TRUE)
@

\section*{mySection}

\FOR{i in 1:5}
nPlots=i
plotName = "thePlot"
outDir = "results"
\includegraphics{paste(getwd(), "/", outDir , "/", plotName, "_", i, sep="")}
\ENDFOR

\end{document}

For which I receive several errors:

Line 25: Undefined control sequence. Line 29: Missing $ inserted. Line 29: LaTeX Error: File `paste(getwd(), "/", outDir , "/", plotName, "_", i, sep="")' not found. Line 29: Missing $ inserted. Line 30: Undefined control sequence.

Any help greatly appreciated!

EDIT 1: I took into account Alex A.'s advice, and changed the section to include \Sexpr{} expressions as follows:

\FOR{i in 1:5}
\Sexpr{nPlots=i}
\Sexpr{plotName = "thePlot"}
\Sexpr{outDir = "results"}
\includegraphics{\Sexpr{paste(getwd(), "/", outDir , "/", plotName, "_", i, sep="")}}
\ENDFOR

\end{document}

However, I now receive an error:

object 'i' not found

I tried changing the condition in the for loop to also include \Sexpr{} as in:

\FOR{\Sexpr{i in 1:5}}

But this gets me the error:

Unexpected 'in'

Any help is appreciated!

EDIT 2:

I tried taking into account advice to simply put the for-loop and image-insertion into Rcode. So, I tried to use the jpeg library and its readJPEG method, as shown below:

<<echo=FALSE>>==
library(jpeg)
@

<<plots, echo = FALSE, fig = TRUE, figs.only = TRUE, results = hide>>=
for (i in 1:5){
  nPlots=i
  plotName = "thePlot"
  outDir = "results"

  img <- readJPEG(paste(getwd(), "/", outDir , "/", plotName, "_", i, ".jpg", sep=""))
  plot(img)
}
@

\end{document}

Unfortunately, this still leads to the error:

unexpected 'in'

Also, when I run the below code alone (not in the for-loop or .Rnw file):

  nPlots=1
  plotName = "thePlot"
  outDir = "results"

  img <- readJPEG(paste(getwd(), "/", outDir , "/", plotName, "_", i, ".jpg", sep=""))
  plot(img)

The image that generates looks different than the .jpeg image that I have (located in currentDirectory/results/thePlot_1.jpg)

Thomas
  • 43,637
  • 12
  • 109
  • 140
  • Try wrapping `paste(...)` in `\Sexpr{}`. – Alex A. Jun 08 '15 at 16:43
  • @AlexA.: Thank you for your help. It did seem to solve the problem, although it appears that the for-loop still does not quite work as I would hope. Do you have any suggestions for this new error that arises? –  Jun 08 '15 at 17:55
  • I don't think using `\For` and `\EndFor` to include multiple plots is the best way to do that. (I actually don't think you _can_ do it that way.) I'd probably do an R chunk like `<>` (R code to show plots) `@`. That will automatically embed them. – Alex A. Jun 08 '15 at 18:04
  • @AlexA. Thanks again. I am trying it now from your suggested approach, but am running across some strange situations. Am I missing a much simpler way to read in the image into the R code? Thank you. –  Jun 08 '15 at 19:57
  • Hm. That's odd... Unfortunately this isn't my area of expertise so hopefully someone else will chime in soon to enlighten us. – Alex A. Jun 08 '15 at 20:10

3 Answers3

0

From the Sweave Manual:

A.7 Creating several figures from one figure chunk does not work

Either save the plots manually and insert them using LaTeX includes (as recommended by the Sweave manual) or switch to knitr. I would recommend the latter.

Thomas
  • 43,637
  • 12
  • 109
  • 140
  • Thanks for the advice. It may seem obvious, but I do not understand about switching to Knitr. When I compare the code I wrote to a minimal example of Knitr, they seem pretty much the same (https://github.com/yihui/knitr/blob/master/inst/examples/knitr-minimal.Rnw). I am just really having a hard time seeing how to even start fixing this in as painless of a fashion as possible. –  Jun 16 '15 at 20:28
  • @LanneR In many cases, an .Rnw written for Sweave will work just fine for knitr, but sometimes you need to make some changes. This document outlines how to transition: http://yihui.name/knitr/demo/sweave/. The only difference is that Sweave is not really under active development and has limitations (like the one you're encountering) and knitr is under active development (and does not have the limitation you're encountering). – Thomas Jun 16 '15 at 20:44
  • Thank you. I have used that command Sweave2knitr, as posted on Yihui's blog. The only change was a section after \SweaveOpts{concordance=TRUE} (see third edit). I still get an error with the \FOR statement as "Undefined control sequence.". Thanks again for suggestions. –  Jun 17 '15 at 17:36
  • @LanneR I'd suggest you post those issues as a new question and revert the edits you made here. Your original question is resolved by my answer and you're asking a new question, so standard procedure is to open a new thread. – Thomas Jun 17 '15 at 18:06
  • Okay, I did just that. –  Jun 17 '15 at 20:26
  • I did end up trying to switch to knitr, which you recommended. However, I am still having an issue with syntax. If you have any knowledge and/or recommendations about it, I reposted the issue here (http://stackoverflow.com/questions/30901733/insert-multiple-images-in-for-loop-into-knitr-document). Thank you. –  Jul 01 '15 at 20:03
0

for anyone who might experiment with latex and knitr image loops, I do it for plots eg. like this:

<<echo=F, cache=T,cache.rebuild=T, results='asis' >>=

pictures <- c(pathtoimage1, pathtoimage2, pathtoimage3)   

plots <- ""
for(pic in pictures){
plots <- c(plots,paste("\\includegraphics[width=0.5\\linewidth]{",pic,"}",sep=""))
}
cat(plots)

@

I believe results='asis' does the trick. But I am not an expert. This creates for me hundreds of plots in one chunk easily in the loop.

Cheers

kn1g
  • 358
  • 3
  • 16
0

The secret is to use paste inside a cat function. Example:

<<echo=F, cache=T,cache.rebuild=T, results='asis'>>=

for(i in c(1:100) {

    cat(paste("ok\\\\\\")

}

@