| 1 |
%% LyX 2.2.1 created this file. For more info, see http://www.lyx.org/. |
| 2 |
%% Do not edit unless you really know what you are doing. |
| 3 |
\documentclass{article} |
| 4 |
\usepackage[sc]{mathpazo} |
| 5 |
\usepackage[T1]{fontenc} |
| 6 |
\usepackage{geometry} |
| 7 |
\geometry{verbose,tmargin=2.5cm,bmargin=2.5cm,lmargin=2.5cm,rmargin=2.5cm} |
| 8 |
\usepackage{url} |
| 9 |
\usepackage[unicode=true,pdfusetitle, |
| 10 |
bookmarks=true,bookmarksnumbered=true,bookmarksopen=true,bookmarksopenlevel=2, |
| 11 |
breaklinks=false,pdfborder={0 0 1},backref=false,colorlinks=false] |
| 12 |
{hyperref} |
| 13 |
\hypersetup{ |
| 14 |
pdfstartview={XYZ null null 1}} |
| 15 |
\usepackage{breakurl} |
| 16 |
\usepackage{listings} |
| 17 |
\lstset{language=R} |
| 18 |
\begin{document} |
| 19 |
<<setup, include=FALSE, cache=FALSE>>= |
| 20 |
library(knitr) |
| 21 |
opts_chunk$set(fig.path = 'figure/listings-') |
| 22 |
options(formatR.arrow = TRUE) |
| 23 |
render_listings() |
| 24 |
@ |
| 25 |
|
| 26 |
\title{Using listings with knitr} |
| 27 |
|
| 28 |
\author{Yihui Xie} |
| 29 |
|
| 30 |
\maketitle |
| 31 |
To use the \textbf{listings} package with \textbf{knitr}, all you |
| 32 |
need to do is to call a function in your first setup chunk (that chunk |
| 33 |
should be hidden from the output with \texttt{include=FALSE} and should |
| 34 |
not be cached): |
| 35 |
|
| 36 |
% I just want to echo the 2nd line |
| 37 |
<<setup, echo=2>>= |
| 38 |
@ |
| 39 |
|
| 40 |
This function modifies the output hooks and header information so |
| 41 |
that the output is written in \textbf{listings} environments, which |
| 42 |
are kindly provided by Frank Harrell and can be found in \url{https://github.com/yihui/knitr/blob/master/inst/misc/Sweavel.sty}. |
| 43 |
Of course you have to install the \textbf{listings} package if your |
| 44 |
\LaTeX{} toolset does not include this package. |
| 45 |
|
| 46 |
Now we can see how the results look like with our new settings: |
| 47 |
|
| 48 |
<<boring-random>>= |
| 49 |
set.seed(1121) # for reproducibility |
| 50 |
options(width=85) |
| 51 |
x=rnorm(20) |
| 52 |
x |
| 53 |
mean(x) |
| 54 |
sqrt(-1) # this will give you a warning message |
| 55 |
@ |
| 56 |
|
| 57 |
Another chunk: |
| 58 |
|
| 59 |
<<rgl-hook-demo,eval=FALSE>>= |
| 60 |
hook_rgl = function(before, options, envir) { |
| 61 |
## after a chunk has been evaluated |
| 62 |
if (before || !require('rgl') || rgl.cur() == 0) return() # no active device |
| 63 |
name = paste(valid_prefix(options$fig.path), options$label, sep = '') |
| 64 |
par3d(windowRect = 100 + options$dpi * c(0, 0, options$width, options$height)) |
| 65 |
Sys.sleep(.05) # need time to respond to window size change |
| 66 |
|
| 67 |
fmt = opts_knit$get('out.format') |
| 68 |
if (fmt %in% c('html', 'markdown', 'gfm', 'jekyll')) options$dev = 'png' |
| 69 |
|
| 70 |
## support 3 formats: eps, pdf and png (default) |
| 71 |
switch(options$dev, |
| 72 |
postscript = rgl.postscript(paste(name, '.eps', sep = ''), fmt = 'eps'), |
| 73 |
pdf = rgl.postscript(paste(name, '.pdf', sep = ''), fmt = 'pdf'), |
| 74 |
rgl.snapshot(paste(name, '.png', sep = ''), fmt = 'png')) |
| 75 |
|
| 76 |
if (fmt == 'html') return(.plot.hook.html(c(name, 'png'), options)) |
| 77 |
if (fmt %in% c('markdown', 'gfm', 'jekyll')) |
| 78 |
return(.plot.hook.markdown(c(name, 'png'), options)) |
| 79 |
|
| 80 |
paste(ifelse(options$align == 'center', '\\centering{}', ''), '\\includegraphics[', |
| 81 |
sprintf('width=%s', options$out.width), ']{', name, '}\n', sep = '') |
| 82 |
} |
| 83 |
@ |
| 84 |
|
| 85 |
Well, we can do crazier things with \textbf{knitr}. Here we use the |
| 86 |
\texttt{fig.cap} option to write plots into the \texttt{figure} environment |
| 87 |
automatically, and the caption is generated from data dynamically |
| 88 |
(see Figure \ref{fig:boxplot-ex}): |
| 89 |
|
| 90 |
% set an option first |
| 91 |
<<eval-opts, echo=FALSE>>= |
| 92 |
opts_knit$set(eval.after = 'fig.cap') # evaluate fig.cap after the chunk |
| 93 |
|
| 94 |
<<boxplot-ex, fig.height=2, out.width='.8\\linewidth', fig.cap=sprintf('This is a boxplot; the median is %.2f.', median(x))>>= |
| 95 |
par(mar=c(4,4,.1,.1)) |
| 96 |
boxplot(x, horizontal=TRUE) |
| 97 |
@ |
| 98 |
|
| 99 |
As we know, \texttt{figure} is a float environment, so it has floated |
| 100 |
away from the R code to the top of this page. This should not be surprising |
| 101 |
to \LaTeX{} users. |
| 102 |
|
| 103 |
You should be able to compile the \TeX{} document and get a PDF file |
| 104 |
like this one: \url{https://github.com/yihui/knitr/releases/download/doc/knitr-listings.pdf}. |
| 105 |
For more information about out hooks, see \url{https://yihui.org/knitr/hooks/}. |
| 106 |
\end{document} |