comment=NA # to remove all hashes
或者
comment='%' # to use a different character
这里提供更多关于knitr的帮助: http://yihui.name/knitr/options
如果您正在使用R Markdown,您的代码块可能如下所示:
```{r comment=NA}
summary(cars)
```
```{r include=FALSE}
knitr::opts_chunk$set(comment = NA)
```
opts_chunk$set(comment = NA)
。 - Ramnathresults='asis'
,并且不需要使用 comment
。 - Molxlibrary(knitr)
。 - cbrnr如果您的输出仅为HTML,则可以充分利用PRE或CODE HTML标记。
```{r my_pre_example,echo=FALSE,include=TRUE,results='asis'}
knitr::opts_chunk$set(comment = NA)
cat('<pre>')
print(t.test(mtcars$mpg,mtcars$wt))
cat('</pre>')
```
Welch双样本t检验
数据:mtcars$mpg和mtcars$wt t值=15.633,自由度=32.633,p值<0.00000000000000022 备择假设:平均值的真实差异不等于0 95%置信区间: 14.67644 19.07031 样本估计值: x的均值 y的均值 20.09062 3.21725
如果您的输出是PDF,则可能需要一些替换函数。这里是我正在使用的:
```r
tidyPrint <- function(data) {
content <- paste0(data,collapse = "\n\n")
content <- str_replace_all(content,"\\t"," ")
content <- str_replace_all(content,"\\ ","\\\\ ")
content <- str_replace_all(content,"\\$","\\\\$")
content <- str_replace_all(content,"\\*","\\\\*")
content <- str_replace_all(content,":",": ")
return(content)
}
```
代码也需要稍作修改:
```{r my_pre_example,echo=FALSE,include=TRUE,results='asis'}
knitr::opts_chunk$set(comment = NA)
resultTTest <- capture.output(t.test(mtcars$mpg,mtcars$wt))
cat(tidyPrint(resultTTest))
```
如果您确实需要页面在PDF和HTML两种情况下工作,那么tidyPrint在最后一步应该有所不同。
注:tidyPrint是指一种用于将HTML转换为PDF的工具。
```r
tidyPrint <- function(data) {
content <- paste0(data,collapse = "\n\n")
content <- str_replace_all(content,"\\t"," ")
content <- str_replace_all(content,"\\ ","\\\\ ")
content <- str_replace_all(content,"\\$","\\\\$")
content <- str_replace_all(content,"\\*","\\\\*")
content <- str_replace_all(content,":",": ")
return(paste("<code>",content,"</code>\n"))
}
```
PDF 结果与之前相同,HTML 结果接近之前的版本,但是带有一些额外的边框。
虽然不完美,但可能已经足够了。
command + shift + c
或在PC上使用control + shift + c
来删除井号,如果需要的话。 - rawr