Pretty-printing R package help in Rmarkdown.

I am trying to pretty print the help output available for some R packages, particularly when they have comments with roxygen markup.

I have tried using the R package printr, but I'm unable to get it to reproduce the nice output it shows. Instead, when I render the Rmarkdown file to PDF, it generates the source code verbatim prefixed by "##" comment markers.

Here is an MRE:

---
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r example}
library(printr)
library(stats)
?confint
```

This outputs something that fails to pretty print the help. If I replace pdf_document with html_document, I get the expected pretty printed output.

Any ideas? I saw this posted as an issue on its github page, Help pages rendered as code block in knit to pdf · Issue #36 · yihui/printr · GitHub, but the author only responds with:

"That is a natural idea. I actually tried it in the very beginning but it was tricky. The main problem is how to properly insert the LaTeX "packages" like Rd.sty into the final .tex output document, since the LaTeX output of a help page requires extra LaTeX packages."

This was back in 2018, so apparently it was very tricky. Are there any other packages that might help with this?

Running your example gives me this

And I think that is the expected output. Do you get something different ?

BTW I edited your post to format correctly the Rmd code. See

FAQ: How to Format R Markdown Source

Hi, thank you for responding.

I get the same output as you. I wanted to reproduce the output that html_document provides.
Here's the MWE:

---
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r example}
library(printr)
library(stats)
?confint
```

Knitting this MWE produces the following output. (Note that I removed most of the output it produces for brevity.)

library(printr)
library(stats)
?confint
confint R Documentation

Confidence Intervals for Model Parameters

Description

Computes confidence intervals for one or more parameters in a fitted model. There is a default and a method for objects inheriting from class “lm”.

Usage

confint(object, parm, level = 0.95, ...)

Arguments

object

a fitted model object.

parm

a specification of which parameters are to be given confidence intervals, either a vector of numbers or a vector of names. If missing, all parameters are considered.

level

the confidence level required.

additional argument(s) for methods.

Details

confint is a generic function. The default method assumes normality, and needs suitable coef and vcov methods to be available. The default method can be called directly for comparison with other methods.

This would be a feature request for printr package. Currently I think only the output is processed this way for HTML outputs, there is no specific transformation of the Rd help page parsing to produce valid LaTeX.

There is already a similar discussion there you could add too:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.