Insert multiple images into for-loop in Sweave Document
I have five images stored as follows (where "currentDirectory" is the output I get from the getwd () command):
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 a .tex file which I can then run pdflatex to have a .pdf file containing these five images. Below 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 am getting multiple errors:
Line 25: Undefined control sequence. Line 29: Missing $ value. Line 29: LaTeX error: File `paste (getwd ()," / ", outDir," / ", plotName," _ ", i, sep =" ") 'not found. Line 29: Missing $ value. Line 30: The escape sequence is Undefined.
Any help is greatly appreciated!
EDIT 1: I took Alex A.'s advice into account and changed the section to include the \ 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 am now getting the error:
object 'i' not found
I tried changing the condition in the for loop to include \ Sexpr {} as well, as in:
\FOR{\Sexpr{i in 1:5}}
But this is throwing me an error:
Unexpected 'in'
Any help is appreciated!
EDIT 2:
I tried to heed the advice to just enable for-loop and image insertion in Rcode. So I tried using jpeg library and my readJPEG method as 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 results in an error:
Unexpected 'in'
Also, when I run just one below code (not in 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 I have (located in currentDirectory / results / thePlot_1.jpg)
source to share
From The Sweave Guide :
A.7 Creating multiple shapes from one piece of shape does not work
Either save the graphs manually, insert them using LaTeX (as recommended by Sweave), or switch to knitr. I would recommend the latter.
source to share