从 Rmd 文件读取块到 Rnw 文件

Read chunk from Rmd file to Rnw file

我想从 RNW 文件中读取 RMD 文件中的块,这两个文件位于不同的文件夹中。我无法让它工作。似乎无法从 RMD 文件中读取块。 read_chunk() 函数似乎只能从 .R 文件中读取。但就我而言,我也无法让它发挥作用。

这是我的三个文件(在不同的文件夹中),最后是我的 RNW 文件的输出。知道为什么这不起作用吗?

"..\Folder_R\trial_r.R"

## @knitr r_chunk_1
14 + 17
cat("SUCCESS THIS IS R CHUNK 1!!!")

## @knitr r_chunk_2
plot(cars)

"..\Folder_html\trial_html.RMD"

---
title: "Untitled"
output: html_document
---

```{r html_chunk_1}
## @knitr html_chunk_1
cat("SUCCESS THIS IS HTML CHUNK 1!!!")
```

```{r html_chunk_2, echo=FALSE}
## @knitr html_chunk_2
plot(cars)
```

"..\Folder_latex\trial_latex.RNW"

\documentclass{article}

\begin{document}
\SweaveOpts{concordance=TRUE}

<<echo=FALSE>>=
library(knitr)

if (file.exists(file.path("..","Folder_html", "trial_html.Rmd")))
  cat("`trial_html` File Exists!!\n\n")

read_chunk(file.path("..","Folder_html", "trial_html.Rmd"))
@


<<latex_chunk_1, echo=FALSE>>=
cat("This is LATEX Chunk!!")
@

<<latex_chunk_2, ref.label='html_chunk_1', echo=FALSE>>=
@

<<latex_chunk_3, echo=FALSE>>=
<<html_chunk_1>>
@

<<html_chunk_1, echo=FALSE>>=
@

<<latex_chunk_4, echo=FALSE>>=
if (file.exists(file.path("..","Folder_R", "trial_r.R")))
  cat("`trial_r` File Exists!!\n\n")
read_chunk(file.path("..","Folder_R", "trial_r.R"))
@

<<latex_chunk_5, echo=FALSE>>=
<<r_chunk_1>>
@

<<r_chunk_2>>=
@

\end{document}

因此,我在 PDF 文件中看到的唯一内容是:

`trial_html` File Exists!!
This is LATEX Chunk!!
`trial_r` File Exists!!

我检查了以下来源 THIS, THIS is somewhat what I want but did not help to solve my problem, and THIS 很有帮助,但我无法让它工作。

我的编译警告信息是:

You can now run (pdf)latex on 'trial_latex.tex'
Warning messages:
1: In utils::Sweave("trial_latex.Rnw", encoding = "ISO8859-1") :
  reference to unknown chunk 'html_chunk_1'
2: In utils::Sweave("trial_latex.Rnw", encoding = "ISO8859-1") :
  reference to unknown chunk 'r_chunk_1'
Running pdflatex.exe on trial_latex.tex...completed

我认为你是 using Sweave instead of knitr。如果您使用的是 RStudio,请务必更改选项: