【发布时间】:2013-03-14 01:22:23
【问题描述】:
我想在编译 knitr rmarkdown 文档时将“pander”功能设置为替代“打印”功能。像这样(在 R 中运行的代码示例):
require(pander)
print <- function(...) pander(..., style = "rmarkdown") # makes sure that everyhing that everyprint will pass through pander
summary(cars)
这将导致:
> summary(cars)
----------------------------------
speed dist
------ ------------ --------------
**** Min. : 4.0 Min. : 2.00
**** 1st Qu.:12.0 1st Qu.: 26.00
**** Median :15.0 Median : 36.00
**** Mean :15.4 Mean : 42.98
**** 3rd Qu.:19.0 3rd Qu.: 56.00
**** Max. :25.0 Max. :120.00
----------------------------------
这样,我将获得所有表格的良好格式,而不是手动需要在整个文档中编写“pander”(想象我必须在文档中编写“summary(car) 20 次,更改“print”将节省我写 pander(summary(car)) )。
这可能吗? (或者有没有我不知道的更聪明的方法?)
谢谢。
更新:.rmd 文件的示例:
TEST
====
```{r}
require(pander)
print <- function(...) pander(..., style = "rmarkdown") # makes sure that everyhing that everyprint will pass through pander
summary(cars)
```
```{r, eval=FALSE}
library(knitr)
knit2html("test.rmd") # http://stackoverflow.com/questions/10646665/how-to-convert-r-markdown-to-html-i-e-what-does-knit-html-do-in-rstudio-0-9
# http://quantifyingmemory.blogspot.co.il/2013/02/reproducible-research-with-r-knitr.html
```
而输出 test.md 是:
TEST
====
```r
require(pander)
print <- function(...) pander(..., style = "rmarkdown") # makes sure that everyhing that everyprint will pass through pander
summary(cars)
```
```
## speed dist
## Min. : 4.0 Min. : 2
## 1st Qu.:12.0 1st Qu.: 26
## Median :15.0 Median : 36
## Mean :15.4 Mean : 43
## 3rd Qu.:19.0 3rd Qu.: 56
## Max. :25.0 Max. :120
```
```r
library(knitr)
knit2html("test.rmd") # http://stackoverflow.com/questions/10646665/how-to-convert-r-markdown-to-html-i-e-what-does-knit-html-do-in-rstudio-0-9
#
# http://quantifyingmemory.blogspot.co.il/2013/02/reproducible-research-with-r-knitr.html
```
【问题讨论】:
-
是的,但是没有用。生成的 .md 文件不接受更改。
-
我认为您需要扩展您的问题,特别是因为“需要在整个文档中编写 'pander'”有点含糊。
-
通用
print函数正在为您尝试打印的对象调用什么方法?如果我在新的 R 会话中键入methods( print ),则有 171 种打印方法可用。 -
Spacedman 和 Simon,我添加了一个示例。我希望这可以澄清。是的,我知道 print 有很多方法,但是对于有很多表格的文档,用“pander”替换它会对我“起作用”。 (同样,如果有人可以提供一个更聪明的选择会更好)
-
p.s:我不理解否决票,我认为这是一个合法且有用的问题。
标签: r knitr r-markdown