Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including R help in knitr output

Tags:

r

knitr

Is it possible to include R documentation in knitr output? When using stock datasets, it would be nice to just include the builtin documentation without having to copy and paste it in. The problem appears to be that ? works by side effect and so there is no "result" in a meaningful sense. For example,

```{r}
?mtcars
```

has no output that is trapped by knitr.

Using help(...,help_type) instead of ? doesn't help either. I've tried:

```{r, results='markup'}
help(mtcars, help_type="text")
```

and

```{r, results='asis'}
help(mtcars, type="html")
```

with the same result. (In the latter case, knitr did trap the output ## starting httpd help server ... done, which is basically just a message about the side effect.)

In other words, is there a way to extract R help in plain text or HTML?

like image 318
Livius Avatar asked Jun 10 '14 16:06

Livius


People also ask

How do you show output in R Markdown in knitting?

Rendering Output There are two ways to render an R Markdown document into its final output format. If you are using RStudio, then the “Knit” button (Ctrl+Shift+K) will render the document and display a preview of it.

How do I add output in R Markdown?

If you prefer to use the console by default for all your R Markdown documents (restoring the behavior in previous versions of RStudio), you can make Chunk Output in Console the default: Tools -> Options -> R Markdown -> Show output inline for all R Markdown documents .

How not include output in R Markdown?

You use results="hide" to hide the results/output (but here the code would still be displayed). You use include=FALSE to have the chunk evaluated, but neither the code nor its output displayed.

How does R knitr work?

When you run render , R Markdown feeds the . Rmd file to knitr, which executes all of the code chunks and creates a new markdown (. md) document which includes the code and its output. The markdown file generated by knitr is then processed by pandoc which is responsible for creating the finished format.


2 Answers

To answer your specific question, "Is there a way to extract R help in plain text or HTML?", the answer would be to use a combination of Rd2HTML or Rd2txt from the "tools" package, with a little bit of help from .getHelpFile from "utils".

For HTML:

tools:::Rd2HTML(utils:::.getHelpFile(help(mtcars)))

For txt:

tools:::Rd2txt(utils:::.getHelpFile(help(mtcars)))

By the sounds of it, though, you should be able to use the function I've linked to in the comment above. For instance, to include the text from the "Description" section of the "mtcars" help page, you would use something along the lines of:

```{r, echo=FALSE, results='asis'}
cat(helpExtract(mtcars, section = "Desc", type = "m_text"))
```
like image 181
A5C1D2H2I1M1N2O1R2T1 Avatar answered Sep 19 '22 09:09

A5C1D2H2I1M1N2O1R2T1


I think you can get what you want by hacking the pager option as follows:

pfun <- function(files, header, title, delete.file) {
    all.str <- do.call("c",lapply(files,readLines))
    cat(all.str,sep="\n")
}
orig_pager <- options(pager=pfun)
help("mtcars")
options(orig_pager)

(you can return the character vector from the function instead of cat()ing it if you prefer).

like image 40
Ben Bolker Avatar answered Sep 18 '22 09:09

Ben Bolker