我无法弄清楚如何让R识别Pander包

时间:2013-10-09 21:53:25

标签: r knitr pandoc pander

我想要一个R - > Docx工作流程。我使用了here给出的教程。设置R系统的命令(我在教程中使用的命令)是:

install.packages('pander')
library(knitr)
knit2html("example.rmd")
# installing/loading the package:
if(!require(installr)) { install.packages("installr"); require(installr)} #load / install+load installr 
# Installing pandoc
install.pandoc()
FILE <- "example"
system(paste0("pandoc -o ", FILE, ".docx ", FILE, ".md"))

网站(example.rmd)的示例文件是:

Doc header 1
============
```{r set_knitr_chunk_options}
opts_chunk$set(echo=FALSE,message=FALSE,results = "asis") # important for making sure the output will be well formatted.
```

```{r load_pander_methods}
require(pander)
replace.print.methods <- function(PKG_name = "pander") {
   PKG_methods <- as.character(methods(PKG_name))
   print_methods <- gsub(PKG_name, "print", PKG_methods)
   for(i in seq_along(PKG_methods)) {
      f <- eval(parse(text=paste(PKG_name,":::", PKG_methods[i], sep = ""))) # the new function to use for print
      assign(print_methods[i], f, ".GlobalEnv")
   }   
}
replace.print.methods()
## The following might work with some tweaks:
## print <- function (x, ...) UseMethod("pander")
```
Some text explaining the analysis we are doing
```{r}
summary(cars)# a summary table
fit <- lm(dist~speed, data = cars)
fit
plot(cars) # a plot
```

这会创建一个doc文件,如下所示(最后还有一个图表):

Doc header 1
opts_chunk$set(echo = FALSE, message = FALSE, results = "asis")  # important for making sure the output will be well formatted.
## Warning: there is no package called 'pander'
## Error: no function 'pander' is visible
Some text explaining the analysis we are doing 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
Call: lm(formula = dist ~ speed, data = cars)
Coefficients: (Intercept) speed
 -17.58 3.93

![generated graph image][1]

现在如何删除生成的doc文件中的错误?如果可能,我想解决错误。

1 个答案:

答案 0 :(得分:3)

pander不是knitr

您需要安装pander包。 (即install.packages('pander'))就像安装knitr一样。