options(width = 80)
The printr (read "printer" or "print R") package is a companion package to
knitr. Its main purpose is to extend the S3
knit_print() in knitr, which is the default value of the
render, as explained in the vignette
To enable the printing methods defined in this package, just
in a code chunk (in the beginning) of your knitr document. Then some objects
will be printed differently with what you would have seen in a normal R console.
help(foo)) can be rendered as HTML, LaTeX, or plain text, and you can also specify which section(s) of the help page to include in the output
vignette()are rendered as tables
library(help = 'foo')is rendered as plain text
To disable the printing methods in this package, you can call
detach('package:printr', unload = TRUE) if you attached the package via
library(printr) before, or
unloadNamespace('printr') if you loaded it via
This package aims to be portable in the sense that it should work in most
document formats, including
*.Rnw (R + LaTeX),
*.Rmd (R Markdown), and
*.Rhtml (R + HTML) files, etc.
First we take a look at a quick example of printing some R objects in the R console:
# R uses plain text representation for data frames/matrices/... head(mtcars) head(iris)
Then we attach the printr package in this R session, and see how things change later:
Matrices and data frames are printed as tables using the
kable() function in
options(digits = 4) set.seed(123) x = matrix(rnorm(40), 5) x # with colunm names dimnames(x) = list(NULL, head(LETTERS, ncol(x))) x # further customization via kable(), e.g. digits and captions knitr::kable(x, digits = 2, caption = 'A table produced by printr.') head(mtcars) head(iris, 10)
For contingency tables, 1-d tables are printed as a 1-row matrix, 2-d tables are printed an $n \times m$ matrix, and tables of higher dimensions are printed as data frames with frequencies.
x1 = sample(letters[1:3], 1000, TRUE) x2 = sample(letters[1:3], 1000, TRUE) x3 = sample(letters[1:3], 1000, TRUE) table(x1) table(x1, x2) table(x1, x2, x3)
Here are some examples demonstrating the results of
help.search(), or you can
?? to search for a string.
??sunflower help.search('contourplot') help.search('foo', package = 'base') help.search('foooooooo', package = 'utils')
In a normal R session, the results will be displayed as an HTML page by default, but normally these functions are meant to be called in an interactive R session, and knitr documents are often compiled in non-interactive R sessions, so we changed the printing behavior of these results, and readers will get the basic idea of these functions when reading the knitr output. If they want to run these functions by themselves, they can do it in an interactive R session.
When you want to read the help page of a certain R object, you normally use
help(), which will launch a separate help page from the R session, and
require human interaction. Again, we may not desire human interactions in
knitr documents, so the help pages are printed as static documents here.
When help pages are really long, we can use the chunk option
printr.help.sections to select
a few sections to display, e.g. we only show the sections
usage of the
We can print the lists of vignettes and datasets in packages using
vignette(package = 'rpart') vignette(package = c('rpart', 'knitr')) data(package = 'lattice') data(package = c('rpart', 'lattice')) data(package = 'knitr') # no datasets here browseVignettes(package = 'knitr')
A description of a package can be printed via
library(help = 'foo'):
library(help = 'printr')
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.