2015-06-08 30 views
2

我有五個圖像存儲如下(其中「currentDirectory所」是結果我從命令getwd得到()):我想寫插入多張圖片中的for循環到Sweave文檔

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文件。下面是我曾嘗試:

\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錯誤:未找到文件`paste(getwd(),「/」,outDir,「/」,plotName,「_」,i,sep =「」)。 第29行:缺少$插入。 第30行:未定義的控制序列。

任何幫助非常感謝!

編輯1:我考慮到了亞歷克斯A.的建議,並改變了部分以包含\ Sexpr {}表達式如下:

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

不過,現在我收到一個錯誤:

object 'i' not found 

我試着在改變條件的for循環也包括\ Sexpr {}比如:

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

但THI我得到了錯誤:

Unexpected 'in' 

任何幫助表示讚賞!

編輯2:

我試着考慮建議乾脆把for循環和圖像插入RCODE。所以,我試圖用JPEG庫及其readJPEG方法,如下圖所示:

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

不幸的是,這還導致了錯誤:

unexpected 'in' 

此外,當我單獨運行下面的代碼(未在for循環或.Rnw文件):

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

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

生成的圖像看起來比我所(位於currentDirectory所/結果/ thePlot_1.jpg)的.JPEG圖像

0不同
+0

嘗試在'\ Sexpr {}'中包裝'粘貼(...)'。 –

+0

@AlexA .:感謝您的幫助。它似乎確實解決了這個問題,儘管看起來for循環仍然不能像我所希望的那樣工作。你對這個新出現的錯誤有什麼建議嗎? – LanneR

+0

我不認爲使用'\ For'和'\ EndFor'來包含多個圖是最好的方法。 (我其實不認爲你可以這樣做)。我可能會做一個R塊,比如'<< plot,echo = FALSE,fig = TRUE,figs.only = TRUE,results = hide >>'( R代碼來顯示圖)'@'。這會自動嵌入它們。 –

回答

0

the Sweave Manual

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

要麼手動保存的曲線,並使用乳膠包括(所推薦的Sweave手冊)或切換到knitr插入。我會推薦後者。

+0

感謝您的建議。這看起來很明顯,但我不瞭解切換到Knitr。當我將編寫的代碼與Knitr的一個簡單示例進行比較時,它們看起來幾乎相同(https://github.com/yihui/knitr/blob/master/inst/examples/knitr-minimal.Rnw)。我真的很難看到如何儘可能無痛地開始解決這個問題。 – LanneR

+0

@LanneR在很多情況下,爲Sweave寫的.Rnw對於knitr來說工作得很好,但有時候你需要做一些改變。本文檔概述瞭如何轉換:http://yihui.name/knitr/demo/sweave/。唯一的區別是Sweave並不是真正處於積極的發展階段,並且存在侷限性(就像你遇到的那樣),而且knitr正處於積極的發展階段(並且沒有你遇到的限制)。 – Thomas

+0

謝謝。正如Yihui博客上發佈的那樣,我使用了Sweave2knitr命令。唯一的變化是\ SweaveOpts {concordance = TRUE}之後的部分(請參閱第三次編輯)。 \ FOR語句仍然出現「未定義的控制序列」錯誤。再次感謝您的建議。 – LanneR