【问题标题】:R markdown child document: object not foundR markdown子文档:找不到对象
【发布时间】:2017-11-27 18:05:05
【问题描述】:

这似乎是一个非常基本的问题,但不知何故我无法在任何地方找到解决方案。我正在尝试将变量传递给 R markdown 子文档。

在父文档中,我有这个块:

```{r}
var1 = "test-var1!"
cat(knit_child("child.Rmd"), sep = "\n")
```

在子文档中,如果我使用ls(),我可以看到var1 在环境中。但是,如果我尝试使用 var1,我会收到一个 knit 错误:

Error in str(var1) : object 'var1' not found
Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> str

我尝试在 RStudio 和命令行中编织。

有没有办法在子文档中使用对象?

【问题讨论】:

标签: r knitr r-markdown


【解决方案1】:

我创建了一个带有两个默认文件的 MWE,格式为 RStudio,但我无法重现您的错误,尽管我猜输出不是您要查找的那个。

Parent.Rmd 文件:

---
title: "Parent"
author: "Clement Walter"
date: "27/11/2017"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r}
var = 1
cat(knitr::knit_child("Child.Rmd"), sep = "\n")
```

Child.Rmd 文件:

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r}
ls()
var + 1
```

输出:

【讨论】:

  • 感谢您的确认。经过进一步检查,事实证明这是一个愚蠢的错误。实际上,我在代码的前面调用了 child.Rmd,认为它是一个不同的孩子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-13
  • 2016-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多