R,knitr和源函数:如何为html报告保留源文件注释 [英] R, knitr, and source function: How to preserve source file comments for html report

查看:116
本文介绍了R,knitr和源函数:如何为html报告保留源文件注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R控制台:当我调用source("file_of_functions.R",echo=TRUE)时,所有源文件表达式(包括注释)都将打印到控制台.

R console: When I call source("file_of_functions.R",echo=TRUE), all source file expressions, including comments, print to console.

编织HTML :当我将source("file_of_functions.R",echo=TRUE)放在一个大块中并编织成html时,除了注释外,输出的内容相同.

Knit HTML: When I put source("file_of_functions.R",echo=TRUE) within a chunk and knit to html, the same output prints except for comments.

为使代码和报告更加清晰,我希望源文件的注释包含在html报告中.

For clarity of my code and report, I would like the comments of the source file to be included in the html report.

有什么建议吗?

基本示例:将以下内容另存为f.R:

Basic example: Save the following as f.R:

# function to add a number to itself
f <- function(x) x+x
f(2)

在控制台中,呼叫source("f.R",echo=TRUE)打印:

In console, the call source("f.R",echo=TRUE) prints:

#function to add a number to itself
> f <- function(x) x+x
> f(2)
> [1] 4

编织到html时,调用

When knitting to html, the call

```{r}
source("f.R",echo=TRUE)
```

产生相同的输出,但没有注释.

yields the same output but without the comment.

推荐答案

test.Rmd

---
output: html_document
---

```{r}
options(prompt = '> ')
```

```{r}
source('./test.r', echo = TRUE)
```

```{r}
source('./test.r', echo = TRUE, keep.source = TRUE)
```

这篇关于R,knitr和源函数:如何为html报告保留源文件注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆