SlideShare une entreprise Scribd logo
1  sur  10
Télécharger pour lire hors ligne
Mix source code and documentation together
                                 A
                 Write R code in LTEX using knitr, xtable and RStudio



                                        Julyan Arbel

                             CREST-INSEE, Universit´ Paris-Dauphine
                                                   e


                                     February 28, 2013




Julyan Arbel (CREST-INSEE)                R code in L EX
                                                    AT                  February 28, 2013   1 / 10
Goal




To have a single document which includes source code for easy update.
                                        A
    Use a single software: R interfaces LTEX, as it does with C++, JAGS,
    (Word, Excel?), etc.




   Julyan Arbel (CREST-INSEE)      R code in L EX
                                             AT                  February 28, 2013   2 / 10
First things



    We will use the knitr package which allows you to embed R code and figures
       A
    in LTEX documents (it is an evolution of Sweave). See the package
    homepage http://yihui.name/knitr/
                     A
    You need a valid LTEX 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 L EX
                                               AT                      February 28, 2013   3 / 10
Write a first document

    Open a new ”R Sweave” document in RStudio
    You can check that the toolbar now includes Format and Compile PDF
    buttons

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 R
           code inside the following lines (mind to write both on single lines, with no
           comment)
    <<>>=
    @
           code values in the text with
    Compile [Ctrl+Shift+I]

   Julyan Arbel (CREST-INSEE)             R code in L EX
                                                    AT                    February 28, 2013   4 / 10
What to put in code chunks




Any code that you want to evaluate / not to evaluate, or show / hide: you can
either show the code, or its result, or both.
    Functions
    Graphs
    Tables
    Global options
    Set working directory, etc.




   Julyan Arbel (CREST-INSEE)      R code in L EX
                                             AT                  February 28, 2013   5 / 10
Number of CRAN Packages




                                            100
                                                          200
                                                                     300
                                                                            400
                                                                                  500
                                                                                        600
                                                                                              800
                                                                                                           1200
                                                                                                                  1500
                                                                                                                         2000

                               2001−06−21                                                                                       1.3




  Julyan Arbel (CREST-INSEE)
                               2001−12−17                                                                                       1.4

                               2002−06−12                                                                                       1.5



                               2003−05−27                                                                                       1.7

                               2003−11−16                                                                                       1.8

                               2004−06−05                                                                                       1.9
                               2004−10−12                                                                                       2
                                                                                                                                                  Number of R packages (lien)




                               2005−06−18                                                                                       2.1




          AT
R code in L EX
                               2005−12−16                                                                                       2.2
                                                                                                                                      R Version




                               2006−05−31                                                                                       2.3

                               2006−12−12                                                                                       2.4
                               2007−04−12                                                                                       2.5

                               2007−11−16                                                                                       2.6
                               2008−03−18                                                                                       2.7

                               2008−10−18                                                                                       2.8


                               2009−09−17                                                                                       2.9
                                              110
                                              129
                                                    162
                                                            219
                                                                   273
                                                                           357
                                                                           406
                                                                                    548
                                                                                    647
                                                                                    739
                                                                                                    911
                                                                                                    1000
                                                                                                              1300
                                                                                                              1427
                                                                                                              1614
                                                                                                                         1952




  February 28, 2013
  6 / 10
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,
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","2
              "2006-12-12","2007-04-12","2007-11-16","2008-03-18","2
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=
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 L EX
                                          AT         February 28, 2013   7 / 10
To show graphs




     Use that kind of code chunk
<<echo=FALSE,out.width=’.7textwidth’,fig=TRUE,include=TRUE>>=
library(ggplot2)
qplot(speed, dist, data=cars)+geom_smooth()
@




    Julyan Arbel (CREST-INSEE)     R code in L EX
                                             AT      February 28, 2013   8 / 10
To show graphs: result



                                      100



                               dist




                                      50




                                       0




                                            5   10            15      20   25
                                                           speed




  Julyan Arbel (CREST-INSEE)                         R code in L EX
                                                               AT               February 28, 2013   9 / 10
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 L EX
                                             AT      February 28, 2013   10 / 10

Contenu connexe

Tendances

Russian insurance market growth perspectives and main directions of investmen...
Russian insurance market growth perspectives and main directions of investmen...Russian insurance market growth perspectives and main directions of investmen...
Russian insurance market growth perspectives and main directions of investmen...РОСГОССТРАХ
 
The Rising Global Offset Challenge - addressing the half trillion dollar ques...
The Rising Global Offset Challenge - addressing the half trillion dollar ques...The Rising Global Offset Challenge - addressing the half trillion dollar ques...
The Rising Global Offset Challenge - addressing the half trillion dollar ques...jbarney23
 
Recent developments in the canadian economy dec2011
Recent developments in the canadian economy   dec2011Recent developments in the canadian economy   dec2011
Recent developments in the canadian economy dec2011Sam Batarseh
 
Ecb draghi competitiveness and growth 20130315
Ecb draghi competitiveness and growth 20130315Ecb draghi competitiveness and growth 20130315
Ecb draghi competitiveness and growth 20130315Trond Johannessen
 
2007* Airline Marketing Embraer Day 2007
2007* Airline Marketing Embraer Day 20072007* Airline Marketing Embraer Day 2007
2007* Airline Marketing Embraer Day 2007Embraer RI
 

Tendances (6)

Russian insurance market growth perspectives and main directions of investmen...
Russian insurance market growth perspectives and main directions of investmen...Russian insurance market growth perspectives and main directions of investmen...
Russian insurance market growth perspectives and main directions of investmen...
 
The Rising Global Offset Challenge - addressing the half trillion dollar ques...
The Rising Global Offset Challenge - addressing the half trillion dollar ques...The Rising Global Offset Challenge - addressing the half trillion dollar ques...
The Rising Global Offset Challenge - addressing the half trillion dollar ques...
 
Recent developments in the canadian economy dec2011
Recent developments in the canadian economy   dec2011Recent developments in the canadian economy   dec2011
Recent developments in the canadian economy dec2011
 
Ecb draghi competitiveness and growth 20130315
Ecb draghi competitiveness and growth 20130315Ecb draghi competitiveness and growth 20130315
Ecb draghi competitiveness and growth 20130315
 
Road, Safety, and Health - Is There a Disconnect?
Road, Safety, and Health - Is There a Disconnect?Road, Safety, and Health - Is There a Disconnect?
Road, Safety, and Health - Is There a Disconnect?
 
2007* Airline Marketing Embraer Day 2007
2007* Airline Marketing Embraer Day 20072007* Airline Marketing Embraer Day 2007
2007* Airline Marketing Embraer Day 2007
 

Plus de Julyan Arbel

Bayesian neural networks increasingly sparsify their units with depth
Bayesian neural networks increasingly sparsify their units with depthBayesian neural networks increasingly sparsify their units with depth
Bayesian neural networks increasingly sparsify their units with depthJulyan Arbel
 
Species sampling models in Bayesian Nonparametrics
Species sampling models in Bayesian NonparametricsSpecies sampling models in Bayesian Nonparametrics
Species sampling models in Bayesian NonparametricsJulyan Arbel
 
Dependent processes in Bayesian Nonparametrics
Dependent processes in Bayesian NonparametricsDependent processes in Bayesian Nonparametrics
Dependent processes in Bayesian NonparametricsJulyan Arbel
 
Asymptotics for discrete random measures
Asymptotics for discrete random measuresAsymptotics for discrete random measures
Asymptotics for discrete random measuresJulyan Arbel
 
Bayesian Nonparametrics, Applications to biology, ecology, and marketing
Bayesian Nonparametrics, Applications to biology, ecology, and marketingBayesian Nonparametrics, Applications to biology, ecology, and marketing
Bayesian Nonparametrics, Applications to biology, ecology, and marketingJulyan Arbel
 
A Gentle Introduction to Bayesian Nonparametrics
A Gentle Introduction to Bayesian NonparametricsA Gentle Introduction to Bayesian Nonparametrics
A Gentle Introduction to Bayesian NonparametricsJulyan Arbel
 
A Gentle Introduction to Bayesian Nonparametrics
A Gentle Introduction to Bayesian NonparametricsA Gentle Introduction to Bayesian Nonparametrics
A Gentle Introduction to Bayesian NonparametricsJulyan Arbel
 
Lindley smith 1972
Lindley smith 1972Lindley smith 1972
Lindley smith 1972Julyan Arbel
 
Diaconis Ylvisaker 1985
Diaconis Ylvisaker 1985Diaconis Ylvisaker 1985
Diaconis Ylvisaker 1985Julyan Arbel
 
Jefferys Berger 1992
Jefferys Berger 1992Jefferys Berger 1992
Jefferys Berger 1992Julyan Arbel
 
Poster DDP (BNP 2011 Veracruz)
Poster DDP (BNP 2011 Veracruz)Poster DDP (BNP 2011 Veracruz)
Poster DDP (BNP 2011 Veracruz)Julyan Arbel
 

Plus de Julyan Arbel (20)

UCD_talk_nov_2020
UCD_talk_nov_2020UCD_talk_nov_2020
UCD_talk_nov_2020
 
Bayesian neural networks increasingly sparsify their units with depth
Bayesian neural networks increasingly sparsify their units with depthBayesian neural networks increasingly sparsify their units with depth
Bayesian neural networks increasingly sparsify their units with depth
 
Species sampling models in Bayesian Nonparametrics
Species sampling models in Bayesian NonparametricsSpecies sampling models in Bayesian Nonparametrics
Species sampling models in Bayesian Nonparametrics
 
Dependent processes in Bayesian Nonparametrics
Dependent processes in Bayesian NonparametricsDependent processes in Bayesian Nonparametrics
Dependent processes in Bayesian Nonparametrics
 
Asymptotics for discrete random measures
Asymptotics for discrete random measuresAsymptotics for discrete random measures
Asymptotics for discrete random measures
 
Bayesian Nonparametrics, Applications to biology, ecology, and marketing
Bayesian Nonparametrics, Applications to biology, ecology, and marketingBayesian Nonparametrics, Applications to biology, ecology, and marketing
Bayesian Nonparametrics, Applications to biology, ecology, and marketing
 
A Gentle Introduction to Bayesian Nonparametrics
A Gentle Introduction to Bayesian NonparametricsA Gentle Introduction to Bayesian Nonparametrics
A Gentle Introduction to Bayesian Nonparametrics
 
A Gentle Introduction to Bayesian Nonparametrics
A Gentle Introduction to Bayesian NonparametricsA Gentle Introduction to Bayesian Nonparametrics
A Gentle Introduction to Bayesian Nonparametrics
 
Lindley smith 1972
Lindley smith 1972Lindley smith 1972
Lindley smith 1972
 
Berger 2000
Berger 2000Berger 2000
Berger 2000
 
Seneta 1993
Seneta 1993Seneta 1993
Seneta 1993
 
Lehmann 1990
Lehmann 1990Lehmann 1990
Lehmann 1990
 
Diaconis Ylvisaker 1985
Diaconis Ylvisaker 1985Diaconis Ylvisaker 1985
Diaconis Ylvisaker 1985
 
Hastings 1970
Hastings 1970Hastings 1970
Hastings 1970
 
Jefferys Berger 1992
Jefferys Berger 1992Jefferys Berger 1992
Jefferys Berger 1992
 
Bayesian Classics
Bayesian ClassicsBayesian Classics
Bayesian Classics
 
Bayesian Classics
Bayesian ClassicsBayesian Classics
Bayesian Classics
 
Arbel oviedo
Arbel oviedoArbel oviedo
Arbel oviedo
 
Poster DDP (BNP 2011 Veracruz)
Poster DDP (BNP 2011 Veracruz)Poster DDP (BNP 2011 Veracruz)
Poster DDP (BNP 2011 Veracruz)
 
Causesof effects
Causesof effectsCausesof effects
Causesof effects
 

R in latex

  • 1. Mix source code and documentation together A Write R code in LTEX using knitr, xtable and RStudio Julyan Arbel CREST-INSEE, Universit´ Paris-Dauphine e February 28, 2013 Julyan Arbel (CREST-INSEE) R code in L EX AT February 28, 2013 1 / 10
  • 2. Goal To have a single document which includes source code for easy update. A Use a single software: R interfaces LTEX, as it does with C++, JAGS, (Word, Excel?), etc. Julyan Arbel (CREST-INSEE) R code in L EX AT February 28, 2013 2 / 10
  • 3. First things We will use the knitr package which allows you to embed R code and figures A in LTEX documents (it is an evolution of Sweave). See the package homepage http://yihui.name/knitr/ A You need a valid LTEX 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 L EX AT February 28, 2013 3 / 10
  • 4. Write a first document Open a new ”R Sweave” document in RStudio You can check that the toolbar now includes Format and Compile PDF buttons 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 R code inside the following lines (mind to write both on single lines, with no comment) <<>>= @ code values in the text with Compile [Ctrl+Shift+I] Julyan Arbel (CREST-INSEE) R code in L EX AT February 28, 2013 4 / 10
  • 5. What to put in code chunks Any code that you want to evaluate / not to evaluate, or show / hide: you can either show the code, or its result, or both. Functions Graphs Tables Global options Set working directory, etc. Julyan Arbel (CREST-INSEE) R code in L EX AT February 28, 2013 5 / 10
  • 6. Number of CRAN Packages 100 200 300 400 500 600 800 1200 1500 2000 2001−06−21 1.3 Julyan Arbel (CREST-INSEE) 2001−12−17 1.4 2002−06−12 1.5 2003−05−27 1.7 2003−11−16 1.8 2004−06−05 1.9 2004−10−12 2 Number of R packages (lien) 2005−06−18 2.1 AT R code in L EX 2005−12−16 2.2 R Version 2006−05−31 2.3 2006−12−12 2.4 2007−04−12 2.5 2007−11−16 2.6 2008−03−18 2.7 2008−10−18 2.8 2009−09−17 2.9 110 129 162 219 273 357 406 548 647 739 911 1000 1300 1427 1614 1952 February 28, 2013 6 / 10
  • 7. 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, 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","2 "2006-12-12","2007-04-12","2007-11-16","2008-03-18","2 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= 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 L EX AT February 28, 2013 7 / 10
  • 8. To show graphs Use that kind of code chunk <<echo=FALSE,out.width=’.7textwidth’,fig=TRUE,include=TRUE>>= library(ggplot2) qplot(speed, dist, data=cars)+geom_smooth() @ Julyan Arbel (CREST-INSEE) R code in L EX AT February 28, 2013 8 / 10
  • 9. To show graphs: result 100 dist 50 0 5 10 15 20 25 speed Julyan Arbel (CREST-INSEE) R code in L EX AT February 28, 2013 9 / 10
  • 10. 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 L EX AT February 28, 2013 10 / 10