我正在尝试使用R markdown。我在外部文件中获得了图形的 R 代码,我想使用 source
或read_chunk
在 R markdown 文件中绘制图形以读取我的 R 代码。我无法自己找到解决方案。
这是我的要点,为方便起见,复制如下:
克。R
ggplot(data = df, aes(x = x, y = y)) + geom_point()
降价文档:
```{r data, echo=FALSE}
library("ggplot2")
library("knitr")
df <- data.frame(x = rnorm(100), u = rnorm(100))
df$y <- 1 + df$x + df$u
```
```{r test, echo=FALSE, results='asis'}
read_chunk('gr.R')
```
```{r test2, echo=FALSE, results='asis'}
source('gr.R')
````
您需要显式打印绘图:
克。R
plt <- ggplot(data = df, aes(x = x, y = y)) + geom_point()
降价
```{r test2, echo=FALSE, results='asis'}
source('gr.R')
print(plt)
````