更换“印刷品".针织块评估中的功能 [英] Replacing the "print" function in knitr chunk evaluation

查看:73
本文介绍了更换“印刷品".针织块评估中的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在编译knitr rmarkdown文档时将"pander"功能设置为替代的"print"功能.像这样(在R中运行的代码示例):

I would like to set the "pander" function as an alternative "print" function for when compiling knitr rmarkdown documents. Like this (Example of code to run in R):

require(pander)
print <- function(...) pander(..., style = "rmarkdown") # makes sure that everyhing that everyprint will pass through pander
summary(cars)

这将导致:

> summary(cars)

----------------------------------
&nbsp;    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 
----------------------------------

这样,我将使所有表格格式正确,而无需手动在整个文档中编写平移"(想象我不得不在文档中编写摘要(汽车)" 20次,更改打印"将使我免于编写pander(summary(car))).

This way, I will get all of the tables well formatted, instead of manually needing to write "pander" all across the document (imagine I had to write "summary(car) 20 times in the document, changing "print" will save me writing pander(summary(car)) ).

可以吗? (或者我不知道有没有更聪明的方法?)

谢谢.

更新:.rmd文件的示例:

Update: example for an .rmd file:

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为:

While the output test.md is:

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
```

推荐答案

您需要有选择地否决要使用pander打印的对象类的打印方法.执行methods(pander)找出可用的内容.某些方法未导出,因此您必须使用:::进行访问.这是一个简单的例子.

You need to selectively overrule the print method for the object class you want to print with pander. Do methods(pander) to figure out what is available. Some methods are not exported, so you will have to use ::: to access them. Here is a simple example.

TEST
====

```{r cache = F, comment = NA}
print.lm <- pander:::pander.lm
lm(mpg ~ wt, data = mtcars)
```

输出

TEST
====


```r
print.lm <- pander:::pander.lm
lm(mpg ~ wt, data = mtcars)
```

```

--------------------------------------------------------------
           &nbsp;  Estimate   Std. Error   t value   Pr(>|t|) 
----------------- ---------- ------------ --------- ----------
  **(Intercept)**   37.29       1.878       19.86   8.242e-19 

           **wt**   -5.344      0.5591     -9.559   1.294e-10 
--------------------------------------------------------------

Table: Fitting linear model: mpg ~ wt
```

这篇关于更换“印刷品".针织块评估中的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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