Including R help in knitr output

r markdown hide output
r markdown examples
r markdown inline code
r markdown show code but not output
r markdown code chunk width
r markdown pdf
r markdown code blocks
r setup, include=false

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?

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"))
```

R Markdown Quick Tour, When you process the R Markdown document with knitr, each of the code chunks and/or output will be inserted (unless you suppress one or both with chunk options, In addition to the chunk options, there are also package options, set with  The R package knitr is a general-purpose literate programming engine, with lightweight API's designed to give users full control of the output without heavy coding work. It combines many features into one package with slight tweaks motivated from my everyday use of Sweave. See the package homepage for details and examples.

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).

Knitr with R Markdown, Creating documents with R Markdown starts with an . all of the R code chunks and creates a new markdown (.md) document which includes the R code and its output. You can install the R Markdown package from CRAN as follows: See the package website and manuals in the references to know more about knitr, including the full documentation of chunk options and demos, etc. Value The compiled document is written into the output file, and the path of the output file is returned.

Use printr, e.g.

library(printr)
help(mtcars)
detach('package:printr', unload = TRUE)

Input and Output, The RStudio IDE knit button renders a file to the first format listed in its output field​. The following output formats are available to use with R Markdown. R package vignette (HTML); github_document - GitHub Flavored Markdown document. Those who use knitr::include_graphics() frequently in their R Markdown files may discover some inconsistencies (from the user point of view) if the same Rmd is used for multiple output formats, especially when PDF (LaTeX) is involved.

knit function, When include = FALSE , this whole code chunk is excluded in the output, but R plots in code chunks are first recorded via a graphical device in knitr, and is a comprehensive guide to the knitr package, but unfortunately biased towards  prefix = getOption("knitr.bib.prefix", "R-")) Arguments x Package names. Packages which are not installed are ignored. file The (‘.bib’) file to write. By default, or if NULL, output is written to the R console. tweak Whether to fix some known problems in the citations, especially non-standard format of author names.

Output Formats - R Markdown, Each output format is often accompanied with several format options. All these options are documented on the R package help pages. For example, you can  11.7 Hide code, text output, messages, or plots. By default, knitr displays all possible output from a code chunk, including the source code, text output, messages, warnings, and plots. You can hide them individually using the corresponding chunk options.

2.6 R code chunks and inline R code, knitr is an R package that integrates computing and reporting. By incorporating code Secondly, content tracking tools like git and github work wonderfully with text files. The important details to learn are methods of controlling the output. Knitr Knitr is a engine having in mind dynamic report generation with R, a statistics-oriented programming language. This article explains how to add R code to your LaTeX document to generate a dynamic output. In a standard LaTeX distribution you must have R set up in your operating system and run some special commands to compile it.

Comments
  • Does the function I've hacked together here help you? stackoverflow.com/a/20165267/1270695
  • On my plan; just have not found time for it yet github.com/yihui/printr/issues/3
  • Rd2HTML is actually the better variant for me because it best preserves all of the formatting (and the code is similar for anybody looking at the Rmd file). To avoid having to include the R help stylesheet, you need to set stylesheet="".
  • @Livius Partially inspired by this post, I have made it possible to automatically display the help page or some specific sections in knitr documents now. Please see github.com/yihui/printr and yihui.name/printr Thanks!