我有一个名为"main.Rnw“的Rnw文件,其中包含以下内容
\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“中,我有:
% !Rnw root = Main.Rnw
\documentclass{article}
\usepackage{float}
\begin{document}
\SweaveOpts{concordance=TRUE}
\section{section}
Something something
\end{document}现在它给出了错误:
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} ),它给出错误:
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。
发布于 2017-03-30 19:15:00
代码不能工作的问题之一是因为您定义了两次文档,\begin{document}和\end{document}分别位于Main.Rnw和Child.Rnw中。
您的子文件\SweaveInput{Child.Rnw}应如下所示:
% !Rnw root = main.Rnw
\SweaveOpts{concordance=TRUE}
\section{section}
Something something尝试使用此文件并编译PDF。
我得到了3页的PDF文件
第一个是标题,第二个内容和第三个部分。

发布于 2017-03-30 19:21:21
我没有使用Sweave的经验,但下面应该会使用knitr。
如果它们应该是相同的,则不必为Child.Rnw指定新的文档规范,因为默认行为是继承父级的属性。
毛线衫到针织
#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:
\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:
\textbf{Testing Child.Rnw Processing:}
<<testChild>>=
data(mtcars)
plot(hclust(dist(mtcars)))
@PDF转换:
knit2pdf("Parent-knitr.Rnw")输出:

https://stackoverflow.com/questions/43114505
复制相似问题