将for循环中的多个图像插入Sweave Document [英] Insert multiple images in for-loop into Sweave Document

查看:281
本文介绍了将for循环中的多个图像插入Sweave Document的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有五个图像存储如下(其中currentDirectory是我从命令getwd()获得的结果):

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

我试图在Rstudio中编写一个.Rnw脚本来创建.tex文件然后我可以运行pdflatex以获得包含这五个图像的.pdf文件。以下是我的尝试:

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}

我收到了几个错误:

第25行:未定义的控制序列。
第29行:缺少$插入。
第29行:LaTeX错误:文件`粘贴(getwd(),/,outDir,/,plotName,_,i,sep =)'未找到。
第29行:缺少$插入。
第30行:未定义的控制序列。

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.

任何帮助都非常感谢!

编辑1:我考虑到Alex A.的建议,并将该部分更改为包括\ Sexpr {}表达式,如下所示:

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

我尝试将for循环中的条件更改为还包括\ Sexpr {},如下所示:

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

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

但这会得到我错误:

Unexpected 'in'

感谢任何帮助!

编辑2:

我尝试考虑将for-loop和图像插入简单地放入Rcode的建议。所以,我尝试使用jpeg库及其readJPEG方法,如下所示:

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'

另外,当我单独运行以下代码时(不在for循环或.Rnw文件中):

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)

图像生成的外观与我拥有的.jpeg图像不同(位于currentDirectory / results / thePlot_1.jpg)

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

推荐答案

来自 Sweave手册


A.7从一个图块创建多个数字不起作用

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

手动保存图并使用LaTeX include(按照Sweave手册的建议)插入图表或切换到knitr。我会推荐后者。

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.

这篇关于将for循环中的多个图像插入Sweave Document的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆