Insert multiple images for a cycle into a KnitR 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}

<<include=FALSE>>=
library(knitr)
opts_chunk$set(
concordance=TRUE
)
@

\author{myName}
\title{myTitle}

\maketitle

<<echo=FALSE>>==
library(ggplot2)
library(GGally)
library(stringr)
library(dplyr)
library(matrixStats)
library(gridExtra)
library(reshape2)
@

      

...

\section*{mySection}

\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}

      

I am getting the error:

"Object 'i' not found"

      

for the line \ Sexpr {nPlots = i}.

I posted a similar question ( Embed multiple images in for-loop in Sweave Document ) and it was suggested to open it here after including several suggestions.

+3


source to share





All Articles