SlideShare ist ein Scribd-Unternehmen logo
1 von 81
Downloaden Sie, um offline zu lesen
Simple.
......
tips, tricks & tools
A brief history of R & R
1976: S language developed at Bell Laboratories.
2
A brief history of R & R
1976: S language developed at Bell Laboratories.
1980: S first used outside Bell.
2
A brief history of R & R
1976: S language developed at Bell Laboratories.
1980: S first used outside Bell.
1987: I first used an implementation of S (called
Ace) distributed by CSIRO.
2
A brief history of R & R
1976: S language developed at Bell Laboratories.
1980: S first used outside Bell.
1987: I first used an implementation of S (called
Ace) distributed by CSIRO.
1988: S-PLUS produced, and I start using it.
2
A brief history of R & R
1976: S language developed at Bell Laboratories.
1980: S first used outside Bell.
1987: I first used an implementation of S (called
Ace) distributed by CSIRO.
1988: S-PLUS produced, and I start using it.
1996: I heard Ross Ihaka give a talk about R at a
statistics conference.
2
A brief history of R & R
1976: S language developed at Bell Laboratories.
1980: S first used outside Bell.
1987: I first used an implementation of S (called
Ace) distributed by CSIRO.
1988: S-PLUS produced, and I start using it.
1996: I heard Ross Ihaka give a talk about R at a
statistics conference.
1997: CRAN began with 12 packages.
2
A brief history of R & R
1976: S language developed at Bell Laboratories.
1980: S first used outside Bell.
1987: I first used an implementation of S (called
Ace) distributed by CSIRO.
1988: S-PLUS produced, and I start using it.
1996: I heard Ross Ihaka give a talk about R at a
statistics conference.
1997: CRAN began with 12 packages.
2000: R 1.0.0 released.
2
A brief history of R & R
1976: S language developed at Bell Laboratories.
1980: S first used outside Bell.
1987: I first used an implementation of S (called
Ace) distributed by CSIRO.
1988: S-PLUS produced, and I start using it.
1996: I heard Ross Ihaka give a talk about R at a
statistics conference.
1997: CRAN began with 12 packages.
2000: R 1.0.0 released.
2001: I stopped using S-PLUS and switch to R.
2
A brief history of R & R
1976: S language developed at Bell Laboratories.
1980: S first used outside Bell.
1987: I first used an implementation of S (called
Ace) distributed by CSIRO.
1988: S-PLUS produced, and I start using it.
1996: I heard Ross Ihaka give a talk about R at a
statistics conference.
1997: CRAN began with 12 packages.
2000: R 1.0.0 released.
2001: I stopped using S-PLUS and switch to R.
2004: I contributed my first function to R
(quantile).
2
A brief history of R & R
1976: S language developed at Bell Laboratories.
1980: S first used outside Bell.
1987: I first used an implementation of S (called
Ace) distributed by CSIRO.
1988: S-PLUS produced, and I start using it.
1996: I heard Ross Ihaka give a talk about R at a
statistics conference.
1997: CRAN began with 12 packages.
2000: R 1.0.0 released.
2001: I stopped using S-PLUS and switch to R.
2004: I contributed my first function to R
(quantile).
2006: I contributed my first package to CRAN
(forecast).
2
Outline
1 Getting help
2 Finding functions
3 Digging into functions
4 Writing functions
5 Debugging
6 Version control
7 My R workflow
3
Getting help
StackOverflow.com
For programming
questions.
4
Getting help
StackOverflow.com
For programming
questions.
CrossValidated.com
For statistical
questions.
4
Getting help
StackOverflow.com
For programming
questions.
CrossValidated.com
For statistical
questions.
R-help mailing lists
stat.ethz.ch/
mailman/listinfo/
r-help
Only when all-else
fails.
4
Outline
1 Getting help
2 Finding functions
3 Digging into functions
4 Writing functions
5 Debugging
6 Version control
7 My R workflow
5
How to find the right function
Functions in installed packages
.
......
help.search("neural"). Equivalently: ??neural
Also built into RStudio help.
6
How to find the right function
Functions in installed packages
.
......
help.search("neural"). Equivalently: ??neural
Also built into RStudio help.
Functions in other CRAN packages
.
......
library(sos)
findFn("neural")
RSiteSearch("neural")
findFn only searches functions.
6
How to find the right function
Functions in installed packages
.
......
help.search("neural"). Equivalently: ??neural
Also built into RStudio help.
Functions in other CRAN packages
.
......
library(sos)
findFn("neural")
RSiteSearch("neural")
findFn only searches functions.
RSiteSearch searches more widely.
6
How to find the right function
Functions in installed packages
.
......
help.search("neural"). Equivalently: ??neural
Also built into RStudio help.
Functions in other CRAN packages
.
......
library(sos)
findFn("neural")
RSiteSearch("neural")
findFn only searches functions.
RSiteSearch searches more widely.
6
How to find the right function
Functions in installed packages
.
......
help.search("neural"). Equivalently: ??neural
Also built into RStudio help.
Functions in other CRAN packages
.
......
library(sos)
findFn("neural")
RSiteSearch("neural")
findFn only searches functions.
RSiteSearch searches more widely.
rseek.org
Google customized search on R-related sites.
6
CRAN Task Views
.
......
cran.r-project.org/
web/views/
Curated reviews of
packages by subject
7
CRAN Task Views
.
......
cran.r-project.org/
web/views/
Curated reviews of
packages by subject
Use install.views()
and update.views()
in the ctv package.
7
Outline
1 Getting help
2 Finding functions
3 Digging into functions
4 Writing functions
5 Debugging
6 Version control
7 My R workflow
8
Digging into functions
.
Example: How does forecast for ets work?
..
......
forecast
forecast.ets
forecast:::pegelsfcast.C
9
Digging into functions
.
Example: How does forecast for ets work?
..
......
forecast
forecast.ets
forecast:::pegelsfcast.C
Typing the name of a function gives its
definition.
9
Digging into functions
.
Example: How does forecast for ets work?
..
......
forecast
forecast.ets
forecast:::pegelsfcast.C
Typing the name of a function gives its
definition.
Be aware of classes and methods.
9
Digging into functions
.
Example: How does forecast for ets work?
..
......
forecast
forecast.ets
forecast:::pegelsfcast.C
Typing the name of a function gives its
definition.
Be aware of classes and methods.
Type package:::function for hidden
functions.
9
Digging into functions
.
Example: How does forecast for ets work?
..
......
forecast
forecast.ets
forecast:::pegelsfcast.C
Typing the name of a function gives its
definition.
Be aware of classes and methods.
Type package:::function for hidden
functions.
Download the tar.gz file from CRAN if
you want to see any underlying C or
Fortran code.
9
Outline
1 Getting help
2 Finding functions
3 Digging into functions
4 Writing functions
5 Debugging
6 Version control
7 My R workflow
10
Good habits
indenting and commenting.
11
Good habits
indenting and commenting.
Reindenting using RStudio.
11
Good habits
indenting and commenting.
Reindenting using RStudio.
formatR package: I run tidy.dir() before
reading student code!
11
Good habits
indenting and commenting.
Reindenting using RStudio.
formatR package: I run tidy.dir() before
reading student code!
Google R style guide:
11
Good habits
indenting and commenting.
Reindenting using RStudio.
formatR package: I run tidy.dir() before
reading student code!
Google R style guide:
Hadley’s R style guide:
11
Outline
1 Getting help
2 Finding functions
3 Digging into functions
4 Writing functions
5 Debugging
6 Version control
7 My R workflow
12
Simple debugging tools
.
When something goes wrong:
..
......
traceback()
13
Simple debugging tools
.
When something goes wrong:
..
......
traceback()
options(error=recover)
13
Simple debugging tools
.
When something goes wrong:
..
......
traceback()
options(error=recover)
debug()
13
Simple debugging tools
.
When something goes wrong:
..
......
traceback()
options(error=recover)
debug()
browser()
13
Simple debugging tools
.
When something goes wrong:
..
......
traceback()
options(error=recover)
debug()
browser()
trace()
13
Simple debugging tools
.
When something goes wrong:
..
......
traceback()
options(error=recover)
debug()
browser()
trace()
13
Simple debugging tools
.
When something goes wrong:
..
......
traceback()
options(error=recover)
debug()
browser()
trace()
.
More extensive debugging tools discussed at
..
......
www.stats.uwo.ca/faculty/murdoch/
software/debuggingR/
13
Simple debugging tools
.
When something goes wrong:
..
......
traceback()
options(error=recover)
debug()
browser()
trace()
.
More extensive debugging tools discussed at
..
......
www.stats.uwo.ca/faculty/murdoch/
software/debuggingR/
.
......
RStudio plans to have debugging tools in a
future release.
13
Outline
1 Getting help
2 Finding functions
3 Digging into functions
4 Writing functions
5 Debugging
6 Version control
7 My R workflow
14
Version control
.
......
In any non-trivial project, it is important to keep
track of versions of R code and documents.
15
Version control
.
......
In any non-trivial project, it is important to keep
track of versions of R code and documents.
Dropbox
Every version of your files for the last 30 days
(or longer if you buy Packrat).
15
Version control
.
......
In any non-trivial project, it is important to keep
track of versions of R code and documents.
Dropbox
Every version of your files for the last 30 days
(or longer if you buy Packrat).
But no built-in diff facilities or changelog.
15
Version control
.
......
In any non-trivial project, it is important to keep
track of versions of R code and documents.
Dropbox
Every version of your files for the last 30 days
(or longer if you buy Packrat).
But no built-in diff facilities or changelog.
15
Version control
.
......
In any non-trivial project, it is important to keep
track of versions of R code and documents.
Dropbox
Every version of your files for the last 30 days
(or longer if you buy Packrat).
But no built-in diff facilities or changelog.
Git
Free version control system.
15
Version control
.
......
In any non-trivial project, it is important to keep
track of versions of R code and documents.
Dropbox
Every version of your files for the last 30 days
(or longer if you buy Packrat).
But no built-in diff facilities or changelog.
Git
Free version control system.
Easy to fork or roll back.
15
Version control
.
......
In any non-trivial project, it is important to keep
track of versions of R code and documents.
Dropbox
Every version of your files for the last 30 days
(or longer if you buy Packrat).
But no built-in diff facilities or changelog.
Git
Free version control system.
Easy to fork or roll back.
Install git software on your local computer.
15
Version control
.
......
In any non-trivial project, it is important to keep
track of versions of R code and documents.
Dropbox
Every version of your files for the last 30 days
(or longer if you buy Packrat).
But no built-in diff facilities or changelog.
Git
Free version control system.
Easy to fork or roll back.
Install git software on your local computer.
Manage via RStudio.
15
Outline
1 Getting help
2 Finding functions
3 Digging into functions
4 Writing functions
5 Debugging
6 Version control
7 My R workflow
16
R projects
Basic idea
Every paper, book or consulting report is a
“project”.
17
R projects
Basic idea
Every paper, book or consulting report is a
“project”.
Every project has its own folder and R
workspace.
17
R projects
Basic idea
Every paper, book or consulting report is a
“project”.
Every project has its own folder and R
workspace.
Every project is entirely scripted. That is, all
analysis, graphs and tables must be able to be
generated by running one R script. The final
report must be able to be generated by
processing one LATEX file.
17
R projects
functions.R contains all non-packaged
functions used in the project.
18
R projects
functions.R contains all non-packaged
functions used in the project.
main.R sources all other R files in the correct
order.
18
R projects
functions.R contains all non-packaged
functions used in the project.
main.R sources all other R files in the correct
order.
Data files provided by the client (or
downloaded from a website) are never edited.
All data manipulation is scripted in R (or
some other language).
18
R projects
functions.R contains all non-packaged
functions used in the project.
main.R sources all other R files in the correct
order.
Data files provided by the client (or
downloaded from a website) are never edited.
All data manipulation is scripted in R (or
some other language).
Tables generated via xtable or texreg
packages.
18
R projects
functions.R contains all non-packaged
functions used in the project.
main.R sources all other R files in the correct
order.
Data files provided by the client (or
downloaded from a website) are never edited.
All data manipulation is scripted in R (or
some other language).
Tables generated via xtable or texreg
packages.
Graphics in pdf format.
18
R projects
functions.R contains all non-packaged
functions used in the project.
main.R sources all other R files in the correct
order.
Data files provided by the client (or
downloaded from a website) are never edited.
All data manipulation is scripted in R (or
some other language).
Tables generated via xtable or texreg
packages.
Graphics in pdf format.
Report or paper in LATEX pulls in the tables and
graphics.
18
R projects
.
Advantages over Sweave and knitR
..
......
¯ Keeps R and LATEX files separate.
19
R projects
.
Advantages over Sweave and knitR
..
......
¯ Keeps R and LATEX files separate.
¯ Multiple R files that can be run
separately.
19
R projects
.
Advantages over Sweave and knitR
..
......
¯ Keeps R and LATEX files separate.
¯ Multiple R files that can be run
separately.
¯ Easier to rebuild sections (e.g., only
some R files).
19
R projects
.
Advantages over Sweave and knitR
..
......
¯ Keeps R and LATEX files separate.
¯ Multiple R files that can be run
separately.
¯ Easier to rebuild sections (e.g., only
some R files).
¯ Easier to collaborate.
19
Figures without whitespace
R graphics have too much surrounding white
space for inclusion in reports.
20
Figures without whitespace
R graphics have too much surrounding white
space for inclusion in reports.
The following function fixes the problem.
20
Figures without whitespace
R graphics have too much surrounding white
space for inclusion in reports.
The following function fixes the problem.
20
Figures without whitespace
R graphics have too much surrounding white
space for inclusion in reports.
The following function fixes the problem.
.
......
savepdf <- function(file, width=16, height=10)
{
fname <- paste("figs/",file,".pdf",sep="")
pdf(fname, width=width/2.54, height=height/2.54,
pointsize=10)
par(mgp=c(2.2,0.45,0), tcl=-0.4, mar=c(3.3,3.6,1.1,1.1))
}
savepdf("fig1")
plot(x,y)
dev.off()
20
R with Makefiles
.
......
A Makefile provides instructions about how to
compile a project.
.
Makefile
..
......
# list R files
RFILES := $(wildcard *.R)
# pdf figures created by R
PDFFIGS := $(wildcard figs/*.pdf)
# Indicator files to show R file has run
OUT_FILES:= $(RFILES:.R=.Rdone)
all: $(OUT_FILES)
# RUN EVERY R FILE
%.Rdone: %.R functions.R
Rscript $< && touch $@
21
Makefiles for R & LATEX
.
Makefile
..
......
# Usually, only these lines need changing
TEXFILE= paper
RDIR= ./figs
FIGDIR= ./figs
# list R files
RFILES := $(wildcard $(RDIR)/*.R)
# pdf figures created by R
PDFFIGS := $(wildcard $(FIGDIR)/*.pdf)
# Indicator files to show R file has run
OUT_FILES:= $(RFILES:.R=.Rdone)
# Indicator files to show pdfcrop has run
CROP_FILES:= $(PDFFIGS:.pdf=.pdfcrop)
all: $(TEXFILE).pdf $(OUT_FILES) $(CROP_FILES)
22
Makefiles for R & LATEX
.
Makefile continued …
..
......
# May need to add something here if some R files
# depend on others.
# RUN EVERY R FILE
$(RDIR)/%.Rdone: $(RDIR)/%.R $(RDIR)/functions.R
Rscript $< && touch $@
# CROP EVERY PDF FIG FILE
$(FIGDIR)/%.pdfcrop: $(FIGDIR)/%.pdf
pdfcrop $< $< && touch $@
# Compile main tex file
$(TEXFILE).pdf: $(TEXFILE).tex $(OUT_FILES) $(CROP_FILES)
latexmk -pdf -quiet $(TEXFILE)
23
The magic of RStudio
An RStudio project
can have an
associated Makefile.
Then building and
cleaning can be done
from within RStudio..
...... rstudio.com
24
Keep up-to-date
RStudio blog:
25
Keep up-to-date
RStudio blog:
.
...... blog.rstudio.org
25
Keep up-to-date
RStudio blog:
.
...... blog.rstudio.org
I blog about R (and other things):
25
Keep up-to-date
RStudio blog:
.
...... blog.rstudio.org
I blog about R (and other things):
.
...... robjhyndman.com/researchtips
25
Keep up-to-date
RStudio blog:
.
...... blog.rstudio.org
I blog about R (and other things):
.
...... robjhyndman.com/researchtips
R-bloggers:
25
Keep up-to-date
RStudio blog:
.
...... blog.rstudio.org
I blog about R (and other things):
.
...... robjhyndman.com/researchtips
R-bloggers:
.
...... www.r-bloggers.com
25

Weitere ähnliche Inhalte

Andere mochten auch

Exploring the boundaries of predictability
Exploring the boundaries of predictabilityExploring the boundaries of predictability
Exploring the boundaries of predictabilityRob Hyndman
 
Visualization of big time series data
Visualization of big time series dataVisualization of big time series data
Visualization of big time series dataRob Hyndman
 
R tools for hierarchical time series
R tools for hierarchical time seriesR tools for hierarchical time series
R tools for hierarchical time seriesRob Hyndman
 
Coherent mortality forecasting using functional time series models
Coherent mortality forecasting using functional time series modelsCoherent mortality forecasting using functional time series models
Coherent mortality forecasting using functional time series modelsRob Hyndman
 
Exploring the feature space of large collections of time series
Exploring the feature space of large collections of time seriesExploring the feature space of large collections of time series
Exploring the feature space of large collections of time seriesRob Hyndman
 
MEFM: An R package for long-term probabilistic forecasting of electricity demand
MEFM: An R package for long-term probabilistic forecasting of electricity demandMEFM: An R package for long-term probabilistic forecasting of electricity demand
MEFM: An R package for long-term probabilistic forecasting of electricity demandRob Hyndman
 
Academia sinica jan-2015
Academia sinica jan-2015Academia sinica jan-2015
Academia sinica jan-2015Rob Hyndman
 
Forecasting without forecasters
Forecasting without forecastersForecasting without forecasters
Forecasting without forecastersRob Hyndman
 

Andere mochten auch (10)

Exploring the boundaries of predictability
Exploring the boundaries of predictabilityExploring the boundaries of predictability
Exploring the boundaries of predictability
 
Visualization of big time series data
Visualization of big time series dataVisualization of big time series data
Visualization of big time series data
 
Econ304 1 intro
Econ304 1 introEcon304 1 intro
Econ304 1 intro
 
R tools for hierarchical time series
R tools for hierarchical time seriesR tools for hierarchical time series
R tools for hierarchical time series
 
Coherent mortality forecasting using functional time series models
Coherent mortality forecasting using functional time series modelsCoherent mortality forecasting using functional time series models
Coherent mortality forecasting using functional time series models
 
Exploring the feature space of large collections of time series
Exploring the feature space of large collections of time seriesExploring the feature space of large collections of time series
Exploring the feature space of large collections of time series
 
MEFM: An R package for long-term probabilistic forecasting of electricity demand
MEFM: An R package for long-term probabilistic forecasting of electricity demandMEFM: An R package for long-term probabilistic forecasting of electricity demand
MEFM: An R package for long-term probabilistic forecasting of electricity demand
 
Academia sinica jan-2015
Academia sinica jan-2015Academia sinica jan-2015
Academia sinica jan-2015
 
Econ304 2 - Index Number
Econ304 2 - Index NumberEcon304 2 - Index Number
Econ304 2 - Index Number
 
Forecasting without forecasters
Forecasting without forecastersForecasting without forecasters
Forecasting without forecasters
 

Ähnlich wie SimpleR: tips, tricks & tools

Ähnlich wie SimpleR: tips, tricks & tools (20)

Poly_introduction_R.pdf
Poly_introduction_R.pdfPoly_introduction_R.pdf
Poly_introduction_R.pdf
 
A little-book-of-r-for-time-series
A little-book-of-r-for-time-seriesA little-book-of-r-for-time-series
A little-book-of-r-for-time-series
 
Time series
Time seriesTime series
Time series
 
Easy R
Easy REasy R
Easy R
 
Setup R and R Studio
Setup R and R StudioSetup R and R Studio
Setup R and R Studio
 
Introduction to Rstudio
Introduction to RstudioIntroduction to Rstudio
Introduction to Rstudio
 
r,rstats,r language,r packages
r,rstats,r language,r packagesr,rstats,r language,r packages
r,rstats,r language,r packages
 
Tableau integration with R
Tableau integration with RTableau integration with R
Tableau integration with R
 
Fun never stops. introduction to haskell programming language
Fun never stops. introduction to haskell programming languageFun never stops. introduction to haskell programming language
Fun never stops. introduction to haskell programming language
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
 
Introduction to r
Introduction to rIntroduction to r
Introduction to r
 
STAT-522 (Data Analysis Using R) by SOUMIQUE AHAMED.pdf
STAT-522 (Data Analysis Using R) by SOUMIQUE AHAMED.pdfSTAT-522 (Data Analysis Using R) by SOUMIQUE AHAMED.pdf
STAT-522 (Data Analysis Using R) by SOUMIQUE AHAMED.pdf
 
Getting started with R
Getting started with RGetting started with R
Getting started with R
 
R Basics and Best Practices
R Basics and Best PracticesR Basics and Best Practices
R Basics and Best Practices
 
Lecture1_R.pdf
Lecture1_R.pdfLecture1_R.pdf
Lecture1_R.pdf
 
Report
ReportReport
Report
 
Introduction to BDD (Behavior-Driven Development)
Introduction to BDD (Behavior-Driven Development)Introduction to BDD (Behavior-Driven Development)
Introduction to BDD (Behavior-Driven Development)
 
Better graphics in R
Better graphics in RBetter graphics in R
Better graphics in R
 

Mehr von Rob Hyndman

Probabilistic forecasting of long-term peak electricity demand
Probabilistic forecasting of long-term peak electricity demandProbabilistic forecasting of long-term peak electricity demand
Probabilistic forecasting of long-term peak electricity demandRob Hyndman
 
Automatic time series forecasting
Automatic time series forecastingAutomatic time series forecasting
Automatic time series forecastingRob Hyndman
 
Forecasting Hierarchical Time Series
Forecasting Hierarchical Time SeriesForecasting Hierarchical Time Series
Forecasting Hierarchical Time SeriesRob Hyndman
 
Forecasting using R
Forecasting using RForecasting using R
Forecasting using RRob Hyndman
 
Demographic forecasting
Demographic forecastingDemographic forecasting
Demographic forecastingRob Hyndman
 
Forecasting electricity demand distributions using a semiparametric additive ...
Forecasting electricity demand distributions using a semiparametric additive ...Forecasting electricity demand distributions using a semiparametric additive ...
Forecasting electricity demand distributions using a semiparametric additive ...Rob Hyndman
 
FPP 1. Getting started
FPP 1. Getting startedFPP 1. Getting started
FPP 1. Getting startedRob Hyndman
 

Mehr von Rob Hyndman (8)

Probabilistic forecasting of long-term peak electricity demand
Probabilistic forecasting of long-term peak electricity demandProbabilistic forecasting of long-term peak electricity demand
Probabilistic forecasting of long-term peak electricity demand
 
Automatic time series forecasting
Automatic time series forecastingAutomatic time series forecasting
Automatic time series forecasting
 
Forecasting Hierarchical Time Series
Forecasting Hierarchical Time SeriesForecasting Hierarchical Time Series
Forecasting Hierarchical Time Series
 
Forecasting using R
Forecasting using RForecasting using R
Forecasting using R
 
Ysc2013
Ysc2013Ysc2013
Ysc2013
 
Demographic forecasting
Demographic forecastingDemographic forecasting
Demographic forecasting
 
Forecasting electricity demand distributions using a semiparametric additive ...
Forecasting electricity demand distributions using a semiparametric additive ...Forecasting electricity demand distributions using a semiparametric additive ...
Forecasting electricity demand distributions using a semiparametric additive ...
 
FPP 1. Getting started
FPP 1. Getting startedFPP 1. Getting started
FPP 1. Getting started
 

Kürzlich hochgeladen

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Principled Technologies
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024SynarionITSolutions
 

Kürzlich hochgeladen (20)

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 

SimpleR: tips, tricks & tools

  • 2. A brief history of R & R 1976: S language developed at Bell Laboratories. 2
  • 3. A brief history of R & R 1976: S language developed at Bell Laboratories. 1980: S first used outside Bell. 2
  • 4. A brief history of R & R 1976: S language developed at Bell Laboratories. 1980: S first used outside Bell. 1987: I first used an implementation of S (called Ace) distributed by CSIRO. 2
  • 5. A brief history of R & R 1976: S language developed at Bell Laboratories. 1980: S first used outside Bell. 1987: I first used an implementation of S (called Ace) distributed by CSIRO. 1988: S-PLUS produced, and I start using it. 2
  • 6. A brief history of R & R 1976: S language developed at Bell Laboratories. 1980: S first used outside Bell. 1987: I first used an implementation of S (called Ace) distributed by CSIRO. 1988: S-PLUS produced, and I start using it. 1996: I heard Ross Ihaka give a talk about R at a statistics conference. 2
  • 7. A brief history of R & R 1976: S language developed at Bell Laboratories. 1980: S first used outside Bell. 1987: I first used an implementation of S (called Ace) distributed by CSIRO. 1988: S-PLUS produced, and I start using it. 1996: I heard Ross Ihaka give a talk about R at a statistics conference. 1997: CRAN began with 12 packages. 2
  • 8. A brief history of R & R 1976: S language developed at Bell Laboratories. 1980: S first used outside Bell. 1987: I first used an implementation of S (called Ace) distributed by CSIRO. 1988: S-PLUS produced, and I start using it. 1996: I heard Ross Ihaka give a talk about R at a statistics conference. 1997: CRAN began with 12 packages. 2000: R 1.0.0 released. 2
  • 9. A brief history of R & R 1976: S language developed at Bell Laboratories. 1980: S first used outside Bell. 1987: I first used an implementation of S (called Ace) distributed by CSIRO. 1988: S-PLUS produced, and I start using it. 1996: I heard Ross Ihaka give a talk about R at a statistics conference. 1997: CRAN began with 12 packages. 2000: R 1.0.0 released. 2001: I stopped using S-PLUS and switch to R. 2
  • 10. A brief history of R & R 1976: S language developed at Bell Laboratories. 1980: S first used outside Bell. 1987: I first used an implementation of S (called Ace) distributed by CSIRO. 1988: S-PLUS produced, and I start using it. 1996: I heard Ross Ihaka give a talk about R at a statistics conference. 1997: CRAN began with 12 packages. 2000: R 1.0.0 released. 2001: I stopped using S-PLUS and switch to R. 2004: I contributed my first function to R (quantile). 2
  • 11. A brief history of R & R 1976: S language developed at Bell Laboratories. 1980: S first used outside Bell. 1987: I first used an implementation of S (called Ace) distributed by CSIRO. 1988: S-PLUS produced, and I start using it. 1996: I heard Ross Ihaka give a talk about R at a statistics conference. 1997: CRAN began with 12 packages. 2000: R 1.0.0 released. 2001: I stopped using S-PLUS and switch to R. 2004: I contributed my first function to R (quantile). 2006: I contributed my first package to CRAN (forecast). 2
  • 12. Outline 1 Getting help 2 Finding functions 3 Digging into functions 4 Writing functions 5 Debugging 6 Version control 7 My R workflow 3
  • 15. Getting help StackOverflow.com For programming questions. CrossValidated.com For statistical questions. R-help mailing lists stat.ethz.ch/ mailman/listinfo/ r-help Only when all-else fails. 4
  • 16. Outline 1 Getting help 2 Finding functions 3 Digging into functions 4 Writing functions 5 Debugging 6 Version control 7 My R workflow 5
  • 17. How to find the right function Functions in installed packages . ...... help.search("neural"). Equivalently: ??neural Also built into RStudio help. 6
  • 18. How to find the right function Functions in installed packages . ...... help.search("neural"). Equivalently: ??neural Also built into RStudio help. Functions in other CRAN packages . ...... library(sos) findFn("neural") RSiteSearch("neural") findFn only searches functions. 6
  • 19. How to find the right function Functions in installed packages . ...... help.search("neural"). Equivalently: ??neural Also built into RStudio help. Functions in other CRAN packages . ...... library(sos) findFn("neural") RSiteSearch("neural") findFn only searches functions. RSiteSearch searches more widely. 6
  • 20. How to find the right function Functions in installed packages . ...... help.search("neural"). Equivalently: ??neural Also built into RStudio help. Functions in other CRAN packages . ...... library(sos) findFn("neural") RSiteSearch("neural") findFn only searches functions. RSiteSearch searches more widely. 6
  • 21. How to find the right function Functions in installed packages . ...... help.search("neural"). Equivalently: ??neural Also built into RStudio help. Functions in other CRAN packages . ...... library(sos) findFn("neural") RSiteSearch("neural") findFn only searches functions. RSiteSearch searches more widely. rseek.org Google customized search on R-related sites. 6
  • 23. CRAN Task Views . ...... cran.r-project.org/ web/views/ Curated reviews of packages by subject Use install.views() and update.views() in the ctv package. 7
  • 24. Outline 1 Getting help 2 Finding functions 3 Digging into functions 4 Writing functions 5 Debugging 6 Version control 7 My R workflow 8
  • 25. Digging into functions . Example: How does forecast for ets work? .. ...... forecast forecast.ets forecast:::pegelsfcast.C 9
  • 26. Digging into functions . Example: How does forecast for ets work? .. ...... forecast forecast.ets forecast:::pegelsfcast.C Typing the name of a function gives its definition. 9
  • 27. Digging into functions . Example: How does forecast for ets work? .. ...... forecast forecast.ets forecast:::pegelsfcast.C Typing the name of a function gives its definition. Be aware of classes and methods. 9
  • 28. Digging into functions . Example: How does forecast for ets work? .. ...... forecast forecast.ets forecast:::pegelsfcast.C Typing the name of a function gives its definition. Be aware of classes and methods. Type package:::function for hidden functions. 9
  • 29. Digging into functions . Example: How does forecast for ets work? .. ...... forecast forecast.ets forecast:::pegelsfcast.C Typing the name of a function gives its definition. Be aware of classes and methods. Type package:::function for hidden functions. Download the tar.gz file from CRAN if you want to see any underlying C or Fortran code. 9
  • 30. Outline 1 Getting help 2 Finding functions 3 Digging into functions 4 Writing functions 5 Debugging 6 Version control 7 My R workflow 10
  • 31. Good habits indenting and commenting. 11
  • 32. Good habits indenting and commenting. Reindenting using RStudio. 11
  • 33. Good habits indenting and commenting. Reindenting using RStudio. formatR package: I run tidy.dir() before reading student code! 11
  • 34. Good habits indenting and commenting. Reindenting using RStudio. formatR package: I run tidy.dir() before reading student code! Google R style guide: 11
  • 35. Good habits indenting and commenting. Reindenting using RStudio. formatR package: I run tidy.dir() before reading student code! Google R style guide: Hadley’s R style guide: 11
  • 36. Outline 1 Getting help 2 Finding functions 3 Digging into functions 4 Writing functions 5 Debugging 6 Version control 7 My R workflow 12
  • 37. Simple debugging tools . When something goes wrong: .. ...... traceback() 13
  • 38. Simple debugging tools . When something goes wrong: .. ...... traceback() options(error=recover) 13
  • 39. Simple debugging tools . When something goes wrong: .. ...... traceback() options(error=recover) debug() 13
  • 40. Simple debugging tools . When something goes wrong: .. ...... traceback() options(error=recover) debug() browser() 13
  • 41. Simple debugging tools . When something goes wrong: .. ...... traceback() options(error=recover) debug() browser() trace() 13
  • 42. Simple debugging tools . When something goes wrong: .. ...... traceback() options(error=recover) debug() browser() trace() 13
  • 43. Simple debugging tools . When something goes wrong: .. ...... traceback() options(error=recover) debug() browser() trace() . More extensive debugging tools discussed at .. ...... www.stats.uwo.ca/faculty/murdoch/ software/debuggingR/ 13
  • 44. Simple debugging tools . When something goes wrong: .. ...... traceback() options(error=recover) debug() browser() trace() . More extensive debugging tools discussed at .. ...... www.stats.uwo.ca/faculty/murdoch/ software/debuggingR/ . ...... RStudio plans to have debugging tools in a future release. 13
  • 45. Outline 1 Getting help 2 Finding functions 3 Digging into functions 4 Writing functions 5 Debugging 6 Version control 7 My R workflow 14
  • 46. Version control . ...... In any non-trivial project, it is important to keep track of versions of R code and documents. 15
  • 47. Version control . ...... In any non-trivial project, it is important to keep track of versions of R code and documents. Dropbox Every version of your files for the last 30 days (or longer if you buy Packrat). 15
  • 48. Version control . ...... In any non-trivial project, it is important to keep track of versions of R code and documents. Dropbox Every version of your files for the last 30 days (or longer if you buy Packrat). But no built-in diff facilities or changelog. 15
  • 49. Version control . ...... In any non-trivial project, it is important to keep track of versions of R code and documents. Dropbox Every version of your files for the last 30 days (or longer if you buy Packrat). But no built-in diff facilities or changelog. 15
  • 50. Version control . ...... In any non-trivial project, it is important to keep track of versions of R code and documents. Dropbox Every version of your files for the last 30 days (or longer if you buy Packrat). But no built-in diff facilities or changelog. Git Free version control system. 15
  • 51. Version control . ...... In any non-trivial project, it is important to keep track of versions of R code and documents. Dropbox Every version of your files for the last 30 days (or longer if you buy Packrat). But no built-in diff facilities or changelog. Git Free version control system. Easy to fork or roll back. 15
  • 52. Version control . ...... In any non-trivial project, it is important to keep track of versions of R code and documents. Dropbox Every version of your files for the last 30 days (or longer if you buy Packrat). But no built-in diff facilities or changelog. Git Free version control system. Easy to fork or roll back. Install git software on your local computer. 15
  • 53. Version control . ...... In any non-trivial project, it is important to keep track of versions of R code and documents. Dropbox Every version of your files for the last 30 days (or longer if you buy Packrat). But no built-in diff facilities or changelog. Git Free version control system. Easy to fork or roll back. Install git software on your local computer. Manage via RStudio. 15
  • 54. Outline 1 Getting help 2 Finding functions 3 Digging into functions 4 Writing functions 5 Debugging 6 Version control 7 My R workflow 16
  • 55. R projects Basic idea Every paper, book or consulting report is a “project”. 17
  • 56. R projects Basic idea Every paper, book or consulting report is a “project”. Every project has its own folder and R workspace. 17
  • 57. R projects Basic idea Every paper, book or consulting report is a “project”. Every project has its own folder and R workspace. Every project is entirely scripted. That is, all analysis, graphs and tables must be able to be generated by running one R script. The final report must be able to be generated by processing one LATEX file. 17
  • 58. R projects functions.R contains all non-packaged functions used in the project. 18
  • 59. R projects functions.R contains all non-packaged functions used in the project. main.R sources all other R files in the correct order. 18
  • 60. R projects functions.R contains all non-packaged functions used in the project. main.R sources all other R files in the correct order. Data files provided by the client (or downloaded from a website) are never edited. All data manipulation is scripted in R (or some other language). 18
  • 61. R projects functions.R contains all non-packaged functions used in the project. main.R sources all other R files in the correct order. Data files provided by the client (or downloaded from a website) are never edited. All data manipulation is scripted in R (or some other language). Tables generated via xtable or texreg packages. 18
  • 62. R projects functions.R contains all non-packaged functions used in the project. main.R sources all other R files in the correct order. Data files provided by the client (or downloaded from a website) are never edited. All data manipulation is scripted in R (or some other language). Tables generated via xtable or texreg packages. Graphics in pdf format. 18
  • 63. R projects functions.R contains all non-packaged functions used in the project. main.R sources all other R files in the correct order. Data files provided by the client (or downloaded from a website) are never edited. All data manipulation is scripted in R (or some other language). Tables generated via xtable or texreg packages. Graphics in pdf format. Report or paper in LATEX pulls in the tables and graphics. 18
  • 64. R projects . Advantages over Sweave and knitR .. ...... ¯ Keeps R and LATEX files separate. 19
  • 65. R projects . Advantages over Sweave and knitR .. ...... ¯ Keeps R and LATEX files separate. ¯ Multiple R files that can be run separately. 19
  • 66. R projects . Advantages over Sweave and knitR .. ...... ¯ Keeps R and LATEX files separate. ¯ Multiple R files that can be run separately. ¯ Easier to rebuild sections (e.g., only some R files). 19
  • 67. R projects . Advantages over Sweave and knitR .. ...... ¯ Keeps R and LATEX files separate. ¯ Multiple R files that can be run separately. ¯ Easier to rebuild sections (e.g., only some R files). ¯ Easier to collaborate. 19
  • 68. Figures without whitespace R graphics have too much surrounding white space for inclusion in reports. 20
  • 69. Figures without whitespace R graphics have too much surrounding white space for inclusion in reports. The following function fixes the problem. 20
  • 70. Figures without whitespace R graphics have too much surrounding white space for inclusion in reports. The following function fixes the problem. 20
  • 71. Figures without whitespace R graphics have too much surrounding white space for inclusion in reports. The following function fixes the problem. . ...... savepdf <- function(file, width=16, height=10) { fname <- paste("figs/",file,".pdf",sep="") pdf(fname, width=width/2.54, height=height/2.54, pointsize=10) par(mgp=c(2.2,0.45,0), tcl=-0.4, mar=c(3.3,3.6,1.1,1.1)) } savepdf("fig1") plot(x,y) dev.off() 20
  • 72. R with Makefiles . ...... A Makefile provides instructions about how to compile a project. . Makefile .. ...... # list R files RFILES := $(wildcard *.R) # pdf figures created by R PDFFIGS := $(wildcard figs/*.pdf) # Indicator files to show R file has run OUT_FILES:= $(RFILES:.R=.Rdone) all: $(OUT_FILES) # RUN EVERY R FILE %.Rdone: %.R functions.R Rscript $< && touch $@ 21
  • 73. Makefiles for R & LATEX . Makefile .. ...... # Usually, only these lines need changing TEXFILE= paper RDIR= ./figs FIGDIR= ./figs # list R files RFILES := $(wildcard $(RDIR)/*.R) # pdf figures created by R PDFFIGS := $(wildcard $(FIGDIR)/*.pdf) # Indicator files to show R file has run OUT_FILES:= $(RFILES:.R=.Rdone) # Indicator files to show pdfcrop has run CROP_FILES:= $(PDFFIGS:.pdf=.pdfcrop) all: $(TEXFILE).pdf $(OUT_FILES) $(CROP_FILES) 22
  • 74. Makefiles for R & LATEX . Makefile continued … .. ...... # May need to add something here if some R files # depend on others. # RUN EVERY R FILE $(RDIR)/%.Rdone: $(RDIR)/%.R $(RDIR)/functions.R Rscript $< && touch $@ # CROP EVERY PDF FIG FILE $(FIGDIR)/%.pdfcrop: $(FIGDIR)/%.pdf pdfcrop $< $< && touch $@ # Compile main tex file $(TEXFILE).pdf: $(TEXFILE).tex $(OUT_FILES) $(CROP_FILES) latexmk -pdf -quiet $(TEXFILE) 23
  • 75. The magic of RStudio An RStudio project can have an associated Makefile. Then building and cleaning can be done from within RStudio.. ...... rstudio.com 24
  • 78. Keep up-to-date RStudio blog: . ...... blog.rstudio.org I blog about R (and other things): 25
  • 79. Keep up-to-date RStudio blog: . ...... blog.rstudio.org I blog about R (and other things): . ...... robjhyndman.com/researchtips 25
  • 80. Keep up-to-date RStudio blog: . ...... blog.rstudio.org I blog about R (and other things): . ...... robjhyndman.com/researchtips R-bloggers: 25
  • 81. Keep up-to-date RStudio blog: . ...... blog.rstudio.org I blog about R (and other things): . ...... robjhyndman.com/researchtips R-bloggers: . ...... www.r-bloggers.com 25