首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用多个Rnw文件?

如何使用多个Rnw文件?
EN

Stack Overflow用户
提问于 2017-03-30 17:59:32
回答 2查看 567关注 0票数 0

我有一个名为"main.Rnw“的Rnw文件,其中包含以下内容

代码语言:javascript
复制
\documentclass{article}
\usepackage{float} 
\title{Something}
\author{John Smith}
\setkeys{Gin}{width=1.15\textwidth}
\begin{document}
\SweaveOpts{concordance=TRUE}
\thispagestyle{empty}
\maketitle
\newpage
\thispagestyle{empty}
\tableofcontents
\newpage
\SweaveInput{reports/Child.Rnw}
\end{document}

在文件"Child.Rnw“中,我有:

代码语言:javascript
复制
% !Rnw root = Main.Rnw
 \documentclass{article}
 \usepackage{float} 
\begin{document}
\SweaveOpts{concordance=TRUE}
\section{section}
 Something something
\end{document}

现在它给出了错误:

代码语言:javascript
复制
  Error in SweaveReadFile(c(ifile, file), syntax, encoding = encoding) : 
  no Sweave file with name ‘./reports/Child.Rnw’ found
  Calls: <Anonymous> -> SweaveReadFile -> SweaveReadFile
  Execution halted

如果我更改代码并从命令\SweaveInput{reports/Child.Rnw} (因此。代码是\SweaveInput{Child.Rnw} ),它给出错误:

代码语言:javascript
复制
Writing to file Main.tex
Processing code chunks with options ...
 Error in seq.default(from = which + 1L, length.out = length(linesout) -  : 
 'from' must be of length 1
Calls: <Anonymous> -> <Anonymous> -> seq -> seq.default
In addition: Warning messages:
1: In 1L:which : numerical expression has 2 elements: only the first used
2: In seq.default(from = which + 1L, length.out = length(linesout) -  :
first element used of 'length.out' argument
Execution halted

我做错了什么?我使用R Studio。

EN

回答 2

Stack Overflow用户

发布于 2017-03-30 19:15:00

代码不能工作的问题之一是因为您定义了两次文档,\begin{document}\end{document}分别位于Main.RnwChild.Rnw中。

您的子文件\SweaveInput{Child.Rnw}应如下所示:

代码语言:javascript
复制
% !Rnw root = main.Rnw
\SweaveOpts{concordance=TRUE}
\section{section}
 Something something

尝试使用此文件并编译PDF。

我得到了3页的PDF文件

第一个是标题,第二个内容和第三个部分。

票数 1
EN

Stack Overflow用户

发布于 2017-03-30 19:21:21

我没有使用Sweave的经验,但下面应该会使用knitr

如果它们应该是相同的,则不必为Child.Rnw指定新的文档规范,因为默认行为是继承父级的属性。

毛线衫到针织

代码语言:javascript
复制
#Convert Sweave specific options to knitr compatible version
#this will generate a new file named Parent-knitr.Rnw
library("knitr")
Sweave2knitr("Parent.Rnw")

文件路径:

为了测试,让我们假设以下假设的目录结构

D:/project/Parent.Rnw

D:/project/reports/Child.Rnw

如果Child.Rnw文件存在于与Parent.Rnw位置相同的位置,则应执行以下操作

\Sexpr{knit_child('Child.RnW')}

如果存在于不同位置,则需要指定Child.Rnw的完整路径,并且绝对路径和相对路径都应该有效

绝对路径,例如“D:/project/report/Child.Rnw”

相对路径,例如"../reports/Child.Rnw“

.表示当前目录,..表示从工作目录上移一级,默认情况下,工作目录设置为Parent.Rnw的目录。有关更多文档,请参阅?knit_child

Parent.Rnw:

代码语言:javascript
复制
\documentclass{article}
\usepackage{float} 
\title{Testing Parent Child Processing with knitr}
\author{John Smith}
\setkeys{Gin}{width=1.15\textwidth}
\begin{document}

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

\thispagestyle{empty}
\maketitle
%\newpage
\thispagestyle{empty}
%\tableofcontents
%\newpage
%\SweaveInput{reports/Child.Rnw}



\Sexpr{knit_child('./reports/Child.RnW')}
\end{document}

Child.Rnw:

代码语言:javascript
复制
\textbf{Testing Child.Rnw Processing:}

<<testChild>>=

data(mtcars)
plot(hclust(dist(mtcars)))


@

PDF转换:

代码语言:javascript
复制
knit2pdf("Parent-knitr.Rnw")

输出:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43114505

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档