我有一个R代码,它根据我的数据生成几个图和表。我想用Rmarkdown编写一份报告,其中我只想包括情节和表,而不需要重写R代码。一种方法是使用'read_chunk‘函数,但它不符合我的目的。下面是我所需要的一个简单的例子。
假设我有下面的R脚本‘Exampe.r’
x <- 1:4
y <- sin(x)
####----Table
table <- cbind(x,y)
####----Plot
plot_1 <- plot(x,y) 现在,在我的R标记文件中,我需要以下内容:
```{r echo=FALSE, cache=FALSE}Knitr::read_chunk(‘Exprese.r’)
The following table shows the results:
```{r Table, echo=FALSE}One can depict the result in a plot as well:
```{r Plot, echo=FALSE}在上面的示例中,我将无法插入表或图,因为两者都需要在表和绘图命令运行之前定义输入'x‘和'y’。有没有办法实现这一点,而不硬编码'x‘和'y’两次?
发布于 2018-10-23 01:30:02
这里不是一个R脚本,而是我的工作流程,它可能对您有用:(对不起,我觉得它应该是一个评论,但它会变得有点冗长)
result_181023.rda文件。在像data/这样的文件夹中result_181023.rda文件加载到report.rmd中,绘制您的数字&打印您的表,并按照您喜欢的方式润色您的报表。举个例子:
```{r data echo=FALSE, cache=FALSE}名为result.list的列表
有一个表result.list$df
和一个ggplot对象: result.list$gg1
load("data/result_181023.rda")
The following table shows the results:
```{r Table, echo=FALSE}针织品::kable(result.list$df)
One can depict the result in a plot as well:
```{r Plot, echo=FALSE}result.list$gg1
https://stackoverflow.com/questions/52939722
复制相似问题