我正在使用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,字符(1L),...,USE.NAMES = FALSE):值必须为长度1,但FUN(X [[1]])结果为3个调用:... paste - &gt; hook .inline.hook - &gt; format_sci - &gt; vapply unlockBinding错误(&#34; params&#34;,):没有绑定&#34; params&#34;电话: - &gt; do.call - &gt; unlockBinding执行暂停
(我希望我能够正确复制错误信息,似乎还没有从R Markdown窗口找到复制/粘贴)
解决这个问题的一种方法是将图形输出到png文件中而不是在脚本的前一部分中,然后链接到pngs ..但是还有其他/更优雅的方法吗?
使用下面的实际示例进行了更新,但使用了绘图,因为它产生了相同的效果(并且我可以更快地演示)
```{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}
答案 0 :(得分:4)
图形应该以块的形式呈现,而不是内联代码。这是一个示例.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
```