继续从rmarkDown中以前的块的输出

发布于 2025-02-07 13:56:45 字数 684 浏览 1 评论 0原文

我想使用/继续从rmarkDown中以前的块中的输出进行以下操作:

```{r one}
x <- 1
print(x)
```

```{r two}
x <- x + 10
print(x)
```

```{r three, use.output.from = "one"}
x <- x * 2
print(x)
```

在这里,最后一个块应与标签“一个”在块中创建的X一起使用。换句话说,块“三”应该打印“ 2”,而不是“ 22”。

我试图设置

```{r three, dependson = "one"}
print(x)
```

它不起作用,因为它打印了“ 22”而不是“ 2”。也

```{r three,ref.label = "one"}
x <- x * 2
print(x)
```

不起作用并打印“ 1”,而不是“ 2”(并且它再次完整地执行了整个块“一个”)。

是否有一个块选项可以使我可以从“一个”而不是从“两个”中输出的x来调用x?也许是通过在“一个”中缓存X,然后称该特定的缓存?否则可能还有另一种方法。理想情况下,它可以在LearnR中使用“练习。设置”(我不知道它是如何工作的,但似乎并没有再次执行“一个”)。

I would like to use/continue with the output from a previous chunk in rmarkdown as follows:

```{r one}
x <- 1
print(x)
```

```{r two}
x <- x + 10
print(x)
```

```{r three, use.output.from = "one"}
x <- x * 2
print(x)
```

Here, the last chunk should work with x as created in the chunk with label "one". In other words, chunk "three" should print "2" instead of "22".

I have tried to set

```{r three, dependson = "one"}
print(x)
```

which doesn't work as it prints "22" instead of "2". Also

```{r three,ref.label = "one"}
x <- x * 2
print(x)
```

does not work and prints "1" instead of "2" (and it performs the entire chunk "one" again in full).

Is there a chunk option that allows me to call x as outputted from "one" rather than from "two" in chunk "three"? Perhaps by caching x in "one" and then calling that specific cache? Or there may be another approach. Ideally, it would work something like "exercise.setup" in learnr (I don't know how that works, but it doesn't appear to execute "one" again).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑忘罢 2025-02-14 13:56:45

您可以使用local来声明新的本地范围,以免更改全局变量。

```{r one}
x <- 1
print(x)
```

```{r two}
local({
x <- x + 10
print(x)
})
```

```{r three, use.output.from = "one"}
x <- x * 2
print(x)
```

You can use local to declare a new local scope so the global variables will not be changed.

```{r one}
x <- 1
print(x)
```

```{r two}
local({
x <- x + 10
print(x)
})
```

```{r three, use.output.from = "one"}
x <- x * 2
print(x)
```
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文