r - Include code from an external R script, run in, display both code and output
问题描述
Is it possible to include code from an external R script in an .Rmd and simultaneously run the code, display the code, and display its results in the output .HTML file? For example, if I have
x <- 1
y <- 3
z <- x + y
z
in external.R
. In the output document I want to see the code above along with the result of z
, i.e. 4. Essentially, I want the equivalent of what would happen if I copy/pasted what's above in an R chunk. So I want
```{r}
some.library::some.function("external.R")
```
to be the equivalent of
```{r}
x <- 1
y <- 3
z <- x + y
z
```
In the output HTML file.
I've tried things like knitr::read_chunk('external.R)
and source('external.R
)`, but these don't display the code. Am I missing something simple?
EDIT
I found that source('external.R', echo = TRUE)
will produce what I ask, but each line of the output's displayed code/results is prepended by ##
. Any way to make it look like it would if the code was simply copy/pasted in a chunk in the .Rmd?
解决方案
推荐阅读
- python - 如何重塑具有不同尺寸的 3d 数组?
- python - pandas 默认从字典列表中读取键作为值
- graphviz - 如何在包含粗体文本的 graphviz 中调整表节点的大小?
- azure - .NET Azure Functions 中的依赖注入 - Azure Cosmos DB 客户端
- python - diff() 不包括每组的第一行
- c - 读取未知维度的矩阵
- java - 覆盖 Vaadin 中的现有 Css
- sql - 删除重复项并保留一份副本
- mapbox-gl - 如何更新deckgl-mapbox MapboxLayer的可见道具
- go - 如何为不同的时区设置多个 cron