Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

function pander when nested in another function in a knit chunk does not cat output. #142

Closed
mathematicalcoffee opened this issue Dec 11, 2014 · 4 comments

Comments

@mathematicalcoffee
Copy link

Reproduced this with pander 0.5.1 (on CRAN) and then again with 0.5.2 (using install_github), and knitr 1.8 (from CRAN).

I have a function which uses pander to print a model.

This works as expected:

```{r, results='asis'}
library(pander)
m.lm = lm(Sepal.Width ~ Sepal.Length, iris)
pander(m.lm)
```

This doesn't (it produces just the paragraph "Next paragraph", omitting the pander(x) output)

```{r, results='asis'}
myprint = function (x) {
    cat(pander(x))
    pandoc.p("Next paragraph")
}
myprint(m.lm)
```

To fix it you have to surround the pander call in cat:

```{r, results='asis'}
myprint.cat = function (x) {
    pander(x)
    pandoc.p("Next paragraph")
}
myprint.cat(m.lm)
```

Then you get both the pander(x) output and the next paragraph.

Bug

According to ?pander I shouldn't have to surround the pander call in cat.

Note

  • If the pander(x) call is the last statement of the function, then the result is printed. Or if you store output = pander(x) and return(output). That is, if the output of pander(x) happens to be the value returned by myprint, it will be printed. However, since I'm printing additional output after the pander call, I cannot ensure that the pander call comes last (or it prints out of order).
  • If I call myprint(x) from the console I'll get the expected output. It's only once I call myprint from a knit chunk. And the pander(x) call has to be inside another function such as myprint; if I simply put the body of the function in a knit chunk it all works.
@daroczig
Copy link
Member

To quote a classic: this is a feature, not a bug :)

But besides joking, pander indeed prints to stdout by default, except for the case when it's called while being "knitted" due to a feature request: http://blog.rapporter.net/2014/09/pander-tables-inside-of-knitr.html (related commit: 9923b2a)

This added functionality simply identifies if knitr is running in the backgorund, and if so, it capture.output and return the resulting string as an knit_asis object, so that you do not need to specify the results='asis' option in your knitr chunk.

To disable this stuff, please add panderOptions('knitr.auto.asis', FALSE) to your .Rprofile or in your Rmd file, and pander will not try to be smarter than it used to be.

Not sure if this resolves the issue (BTW thank you very much for the detailed report), and I would be more than happy to hear any suggestions on how to overcome the described problem automatically.

@daroczig
Copy link
Member

@mathematicalcoffee do you agree that the above described "disable knitr.auto.asis option" resolves your issue?

@mathematicalcoffee
Copy link
Author

Yes, it does. Thanks for the explanation, I wondered if it was something like that. I'm perfectly happy to set knitr.auto.asis to F (I hadn't noticed that feature, pretty cool). Thanks!

@daroczig
Copy link
Member

Sounds great, thanks for the prompt and kind feedback. I'm closing this issue now, but please feel free to reopen/submit a new one if you come up with a better solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants