In knitr, no output from pander in for loop
Asked Answered
G

1

9

Using knitr in RStudio, pander does not produce any (or correct) html output when in a for loop. Here is a minimal case, as an Rmd input file.

---
title: "Untitled"
output: html_document
---

Testing why pander doesn't work in for loop

```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.width=7, fig.height=5, echo=FALSE, warning=FALSE,
  message=FALSE)

```

```{r}
library(pander)

r <- 1:10
print("pander at top level")   
pander(summary(r))    # works

print("pander in for loop")
for (i in 1:2) pander(summary(r))    #does not work (nothing in output)
for (i in 1:2) print(pander(summary(r)))    #does not work  (code in output)
for (i in 1:2) print(summary(r))    # works

```

The result is the same for other (more interesting) "summary" objects, e.g. a summary of an lm fit. The same behavior was observed for pander 0.5.2 from CRAN, and also 0.5.3 loaded from github.

RStudio v 0.99.467.

> sessionInfo()
R version 3.2.0 (2015-04-16)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.4 (Yosemite)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] pander_0.5.3

loaded via a namespace (and not attached):
 [1] minqa_1.2.4     MASS_7.3-40     Matrix_1.2-0    htmltools_0.2.6 tools_3.2.0    
 [6] yaml_2.1.13     Rcpp_0.11.6     rmarkdown_0.7   splines_3.2.0   nlme_3.1-120   
[11] grid_3.2.0      digest_0.6.8    nloptr_1.0.4    lme4_1.1-7      lattice_0.20-31
Gilchrist answered 25/7, 2015 at 12:25 Comment(0)
V
10

@daroczig answered in the comments :

  1. change knitr chunk option results to asis, either at the global level knitr::opts_chunk$set(results="asis") or at the chunk level ```{r,results="asis"}
  2. disable panderOption knitr.auto.asis : panderOptions('knitr.auto.asis', FALSE)

Cf. this issue

---
title: "Untitled"
output: html_document
---

Testing **when** pander doesn't work in for loop

```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.width=7, fig.height=5, echo=TRUE, warning=FALSE,
  message=FALSE)

```

```{r,results="asis"}
library(pander)
panderOptions('knitr.auto.asis', FALSE)

r <- 1:10

for (i in 1:2) pander(summary(r))

```
Valencia answered 25/7, 2015 at 12:25 Comment(5)
Neither print or cat is needed: pander already prints to stdout (using cat internally). Related question and comment: #31491917Chrissie
@Chrissie could you turn that to an answer so we can point to it? I'll delete my answerValencia
daroczig, thanks for the pointer to the related issue. But I'm confused, because in my minimal example, just pander(..) without cat or print did not produce any visible output.Gilchrist
@EliotSmith actually, it does if you add results="asis" and specify panderOptions('knitr.auto.asis', FALSE) between library(pander) and pander()Valencia
pander prints to stdout, AS LONG as the document is not knitted. For more information see github.com/Rapporter/pander/issues/142Melda

© 2022 - 2024 — McMap. All rights reserved.