gpt4 book ai didi

r - 将 ggplot 嵌入 knitr 文档失败

转载 作者:行者123 更新时间:2023-12-01 23:54:30 25 4
gpt4 key购买 nike

我正在使用 R-Studio/Knit 创建文档,并希望在文档部分使用图形和表格等之前通过创建图形和表格等来组织我的工作,例如

mygraph1 <- ggplot(<whatever is needed>)

```
In this document I want the graphs shown after this line

`r mygraph1`

and before this
```{r, echo=FALSE}

但我得到的是一条错误消息

vapply(x, format_sci_one, character(1L),..., USE.NAMES=FALSE) 中的错误:值的长度必须为 1,但 FUN(X[[1]]) 结果为 3 次调用: ... 粘贴 -> Hook .inline.hook -> format_sci -> vapplyunlockBinding("params",) 中的错误:“params”调用没有绑定(bind):-> do.call ->unlockBinding 执行已停止

(我希望我能够正确复制错误消息,似乎尚未从 R Markdown 窗口中找到复制/粘贴)

解决这个问题的方法是将图形输出到 png 文件中,而不是在脚本的前一部分中,然后链接到 png。但是还有其他/更优雅的方法来做到这一点吗?

使用下面的真实示例进行了更新,但使用了绘图,因为它产生了相同的效果(并且我可以更快地演示)

```{r setup, include=FALSE}

library(knitr)
n <- 5
df <- data.frame(x=seq(1,n),y=rnorm(n))
df_kable <- kable(df)
df_plot <- plot(df)

```

## My chapter

Tabular data works fine like this embedded in text

`r df_kable`

and it would be so cool if plot etc would work the same way as well..

`r ds_plot`

But looks it does not. So sad..

```{r echo=FALSE}

最佳答案

图形应该以 block 的形式呈现,而不是内联代码。下面是一个示例 .Rmd 文件和生成的 .html 输出。

---
title: Embedding ggplot into knitr document fails
---

We will generate a graphic in the following chunk.
```{r}
library(ggplot2)

mtcars_ggplot <-
ggplot(mtcars) +
aes(x = wt, y = mpg) +
geom_point() +
stat_smooth()
```

In-line code is great for things like summary statistics. The mean miles per gallon for the `mtcars` data set is `r mean(mtcars$mpg)`.

In-line code is not good for graphics, as you know.

Instead, use a chunk. This will also allow you to control how the graphic is
rendered in your final document.

```{r show_figure, fig.width = 3, fig.height = 3}
mtcars_ggplot
```

enter image description here

关于r - 将 ggplot 嵌入 knitr 文档失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43674528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com