我通过使用虹膜data.frame简化了我的问题。问题是我无法在Rmd文件中的函数内打印data.frame。
这是我的“ test.Rmd”文件:
```{r results='asis',echo=FALSE}
fun_test<-function(dfiris){
out<-tryCatch(
{
cat(" \n### Inside fun_test. Printing dfiris -> FAIL \n")
cat(" \n")
kable(dfiris) # It does not work
},
error=function(cond){return(NA) }
)
}
```
```{r results='asis',echo=FALSE}
library(knitr)
df1<-head(iris)
cat(" \n## Start. Outisde fun_test. Printing df1 -> OK \n")
cat(" \n")
kable(df1)
fun_test(df1)
```
因此,我将其渲染到Word文档:
render("test.Rmd","word_document")
输出结果是:
为什么不打印data.frame“ dfiris”?我的“ fun_test”函数有问题吗?
您不返回结果,只是将其分配给out
。
尝试:
fun_test<-function(dfiris){
tryCatch(
{
cat(" \n### Inside fun_test. Printing dfiris -> FAIL \n")
cat(" \n")
kable(dfiris) # It does not work
},
error=function(cond){return(NA) }
)
}