10
Mix source code and documentation together Write R code in L A T E X using knitr, xtable and RStudio Julyan Arbel CREST-INSEE, Universit´ e Paris-Dauphine February 28, 2013 Julyan Arbel (CREST-INSEE) R code in LAT E X February 28, 2013 1 / 10

R in latex

Embed Size (px)

Citation preview

Page 1: R in latex

Mix source code and documentation togetherWrite R code in LATEX using knitr, xtable and RStudio

Julyan Arbel

CREST-INSEE, Universite Paris-Dauphine

February 28, 2013

Julyan Arbel (CREST-INSEE) R code in LATEX February 28, 2013 1 / 10

Page 2: R in latex

Goal

To have a single document which includes source code for easy update.

Use a single software: R interfaces LATEX, as it does with C++, JAGS,(Word, Excel?), etc.

Julyan Arbel (CREST-INSEE) R code in LATEX February 28, 2013 2 / 10

Page 3: R in latex

First things

We will use the knitr package which allows you to embed R code and figuresin LATEX documents (it is an evolution of Sweave). See the packagehomepage http://yihui.name/knitr/

You need a valid LATEX distribution

We will use knitr in RStudio, because it’s well integrated in it

install.packages("knitr")

library("knitr")

Julyan Arbel (CREST-INSEE) R code in LATEX February 28, 2013 3 / 10

Page 4: R in latex

Write a first document

Open a new ”R Sweave” document in RStudio

You can check that the toolbar now includes Format and Compile PDFbuttons

\documentclass{article}

\begin{document}

\end{document}

Write your text, and insert

code chunks [Ctrl+Alt+I] for graphs or tables. A code chunk consists in Rcode inside the following lines (mind to write both on single lines, with nocomment)

<<>>=@

code values in the text with

Compile [Ctrl+Shift+I]

Julyan Arbel (CREST-INSEE) R code in LATEX February 28, 2013 4 / 10

Page 5: R in latex

What to put in code chunks

Any code that you want to evaluate / not to evaluate, or show / hide: you caneither show the code, or its result, or both.

Functions

Graphs

Tables

Global options

Set working directory, etc.

Julyan Arbel (CREST-INSEE) R code in LATEX February 28, 2013 5 / 10

Page 6: R in latex

Number of R packages (lien)

2001

−06

−21

2001

−12

−17

2002

−06

−12

2003

−05

−27

2003

−11

−16

2004

−06

−05

2004

−10

−12

2005

−06

−18

2005

−12

−16

2006

−05

−31

2006

−12

−12

2007

−04

−12

2007

−11

−16

2008

−03

−18

2008

−10

−18

2009

−09

−17

100100

200

300

400

500

600

800

1200

1500

2000

Num

ber

of C

RA

N P

acka

ges

1.3

1.4

1.5

1.7

1.8

1.9

2 2.1

2.2

2.3

2.4

2.5

2.6

2.7

2.8

2.9

R Version

110129

162

219

273

357406

548

647739

9111000

130014271614

1952

Julyan Arbel (CREST-INSEE) R code in LATEX February 28, 2013 6 / 10

Page 7: R in latex

Number of R packages: now the code

rv <- seq(1.3, 2.9, .1)

pckg.num <- c(110,129,162,219,273,357,406,548,647,739,911,1000,1300,1427,1614,1952)

rv.dates <- c("2001-6-21", "2001-12-17","2002-06-12","2003-05-27",

"2003-11-16","2004-06-05","2004-10-12","2005-06-18","2005-12-16", "2006-05-31",

"2006-12-12","2007-04-12","2007-11-16","2008-03-18","2008-10-18","2009-09-17")

pckg.fit <- lm(pckg.num~rv)

par(mar=c(7, 5, 5, 3), las=2)

plot(as.POSIXct(rv.dates), pckg.num, xlab="",ylab="",col="red", log="y", pch=19, axes=F)

axis.POSIXct(1, 1:16, rv.dates, format="%Y-%m-%d")

axis(2, at=c(100,200,300,400,500,600,800,100,1200,1500,2000))

mtext("Number of CRAN Packages", side=2, line=3, las=3)

axis.POSIXct(3, rv.dates, rv.dates, labels=as.character(rv))

mtext("R Version", side=3, line=3, las=1)

axis(4, pckg.num)

abline(v=as.POSIXct(rv.dates), col="lightgray", lty="dashed")

abline(h=pckg.num, col="lightgray", lty="dashed")

abline(lm(log10(pckg.num)~as.POSIXct(rv.dates)), col="red")

Julyan Arbel (CREST-INSEE) R code in LATEX February 28, 2013 7 / 10

Page 8: R in latex

To show graphs

Use that kind of code chunk

<<echo=FALSE,out.width=’.7\\textwidth’,fig=TRUE,include=TRUE>>=

library(ggplot2)

qplot(speed, dist, data=cars)+geom_smooth()

@

Julyan Arbel (CREST-INSEE) R code in LATEX February 28, 2013 8 / 10

Page 9: R in latex

To show graphs: result

0

50

100

5 10 15 20 25speed

dist

Julyan Arbel (CREST-INSEE) R code in LATEX February 28, 2013 9 / 10

Page 10: R in latex

To show data tables

Use that kind of code chunk

<<echo=FALSE,results=asis>>=

load(file="data.Rdata")

row.names(data) = c(...)

library(xtable)

tab=xtable(data)

digits(tab)=1

print(tab,floating=FALSE,include.rownames=TRUE,type="latex")

@

Julyan Arbel (CREST-INSEE) R code in LATEX February 28, 2013 10 / 10