simpler tips, tricks and tools - rob j. hyndman · goodhabits indentingandcommenting....

81
Simple . . tips, tricks & tools

Upload: others

Post on 08-Jul-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Simple.

......tips, tricks & tools

Page 2: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

A brief history of R & R1976: S language developed at Bell Laboratories.

1980: S first used outside Bell.1987: I first used an implementation of S (calledAce) distributed by CSIRO.1988: S-PLUS produced, and I start using it.1996: I heard Ross Ihaka give a talk about R at astatistics 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

Page 3: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

A brief history of R & R1976: S language developed at Bell Laboratories.1980: S first used outside Bell.

1987: I first used an implementation of S (calledAce) distributed by CSIRO.1988: S-PLUS produced, and I start using it.1996: I heard Ross Ihaka give a talk about R at astatistics 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

Page 4: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

A brief history of R & R1976: S language developed at Bell Laboratories.1980: S first used outside Bell.1987: I first used an implementation of S (calledAce) distributed by CSIRO.

1988: S-PLUS produced, and I start using it.1996: I heard Ross Ihaka give a talk about R at astatistics 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

Page 5: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

A brief history of R & R1976: S language developed at Bell Laboratories.1980: S first used outside Bell.1987: I first used an implementation of S (calledAce) distributed by CSIRO.1988: S-PLUS produced, and I start using it.

1996: I heard Ross Ihaka give a talk about R at astatistics 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

Page 6: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

A brief history of R & R1976: S language developed at Bell Laboratories.1980: S first used outside Bell.1987: I first used an implementation of S (calledAce) distributed by CSIRO.1988: S-PLUS produced, and I start using it.1996: I heard Ross Ihaka give a talk about R at astatistics 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

Page 7: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

A brief history of R & R1976: S language developed at Bell Laboratories.1980: S first used outside Bell.1987: I first used an implementation of S (calledAce) distributed by CSIRO.1988: S-PLUS produced, and I start using it.1996: I heard Ross Ihaka give a talk about R at astatistics 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

Page 8: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

A brief history of R & R1976: S language developed at Bell Laboratories.1980: S first used outside Bell.1987: I first used an implementation of S (calledAce) distributed by CSIRO.1988: S-PLUS produced, and I start using it.1996: I heard Ross Ihaka give a talk about R at astatistics 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

Page 9: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

A brief history of R & R1976: S language developed at Bell Laboratories.1980: S first used outside Bell.1987: I first used an implementation of S (calledAce) distributed by CSIRO.1988: S-PLUS produced, and I start using it.1996: I heard Ross Ihaka give a talk about R at astatistics 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

Page 10: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

A brief history of R & R1976: S language developed at Bell Laboratories.1980: S first used outside Bell.1987: I first used an implementation of S (calledAce) distributed by CSIRO.1988: S-PLUS produced, and I start using it.1996: I heard Ross Ihaka give a talk about R at astatistics 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

Page 11: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

A brief history of R & R1976: S language developed at Bell Laboratories.1980: S first used outside Bell.1987: I first used an implementation of S (calledAce) distributed by CSIRO.1988: S-PLUS produced, and I start using it.1996: I heard Ross Ihaka give a talk about R at astatistics 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

Page 12: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Outline

1 Getting help

2 Finding functions

3 Digging into functions

4 Writing functions

5 Debugging

6 Version control

7 My R workflow

3

Page 13: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Getting helpStackOverflow.comFor programmingquestions.

4

Page 14: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Getting helpStackOverflow.comFor programmingquestions.

CrossValidated.com

For statisticalquestions.

4

Page 15: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Getting helpStackOverflow.comFor programmingquestions.

CrossValidated.com

For statisticalquestions.

R-help mailing listsstat.ethz.ch/mailman/listinfo/r-helpOnly when all-elsefails.

4

Page 16: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Outline

1 Getting help

2 Finding functions

3 Digging into functions

4 Writing functions

5 Debugging

6 Version control

7 My R workflow

5

Page 17: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

How to find the right functionFunctions in installed packages.

......

help.search("neural"). Equivalently: ??neuralAlso 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

Page 18: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

How to find the right functionFunctions in installed packages.

......

help.search("neural"). Equivalently: ??neuralAlso 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

Page 19: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

How to find the right functionFunctions in installed packages.

......

help.search("neural"). Equivalently: ??neuralAlso 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

Page 20: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

How to find the right functionFunctions in installed packages.

......

help.search("neural"). Equivalently: ??neuralAlso 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

Page 21: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

How to find the right functionFunctions in installed packages.

......

help.search("neural"). Equivalently: ??neuralAlso built into RStudio help.

Functions in other CRAN packages.

......

library(sos)findFn("neural")RSiteSearch("neural")

findFn only searches functions.RSiteSearch searches more widely.

rseek.orgGoogle customized search on R-related sites.

6

Page 22: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

CRAN Task Views.

......cran.r-project.org/web/views/

Curated reviews ofpackages by subject

Use install.views()and update.views()in the ctv package.

7

Page 23: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

CRAN Task Views.

......cran.r-project.org/web/views/

Curated reviews ofpackages by subjectUse install.views()and update.views()in the ctv package.

7

Page 24: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Outline

1 Getting help

2 Finding functions

3 Digging into functions

4 Writing functions

5 Debugging

6 Version control

7 My R workflow

8

Page 25: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Digging into functions.Example: How does forecast for ets work?..

......

forecastforecast.etsforecast:::pegelsfcast.C

Typing the name of a function gives itsdefinition.Be aware of classes and methods.Type package:::function for hiddenfunctions.Download the tar.gz file from CRAN ifyou want to see any underlying C orFortran code.

9

Page 26: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Digging into functions.Example: How does forecast for ets work?..

......

forecastforecast.etsforecast:::pegelsfcast.C

Typing the name of a function gives itsdefinition.

Be aware of classes and methods.Type package:::function for hiddenfunctions.Download the tar.gz file from CRAN ifyou want to see any underlying C orFortran code.

9

Page 27: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Digging into functions.Example: How does forecast for ets work?..

......

forecastforecast.etsforecast:::pegelsfcast.C

Typing the name of a function gives itsdefinition.Be aware of classes and methods.

Type package:::function for hiddenfunctions.Download the tar.gz file from CRAN ifyou want to see any underlying C orFortran code.

9

Page 28: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Digging into functions.Example: How does forecast for ets work?..

......

forecastforecast.etsforecast:::pegelsfcast.C

Typing the name of a function gives itsdefinition.Be aware of classes and methods.Type package:::function for hiddenfunctions.

Download the tar.gz file from CRAN ifyou want to see any underlying C orFortran code.

9

Page 29: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Digging into functions.Example: How does forecast for ets work?..

......

forecastforecast.etsforecast:::pegelsfcast.C

Typing the name of a function gives itsdefinition.Be aware of classes and methods.Type package:::function for hiddenfunctions.Download the tar.gz file from CRAN ifyou want to see any underlying C orFortran code.

9

Page 30: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Outline

1 Getting help

2 Finding functions

3 Digging into functions

4 Writing functions

5 Debugging

6 Version control

7 My R workflow

10

Page 31: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Good habitsindenting and commenting.

Reindenting using RStudio.formatR package: I run tidy.dir() beforereading student code!Google R style guide:Hadley’s R style guide:

11

Page 32: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Good habitsindenting and commenting.Reindenting using RStudio.

formatR package: I run tidy.dir() beforereading student code!Google R style guide:Hadley’s R style guide:

11

Page 33: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Good habitsindenting and commenting.Reindenting using RStudio.formatR package: I run tidy.dir() beforereading student code!

Google R style guide:Hadley’s R style guide:

11

Page 34: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Good habitsindenting and commenting.Reindenting using RStudio.formatR package: I run tidy.dir() beforereading student code!Google R style guide:

Hadley’s R style guide:

11

Page 35: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Good habitsindenting and commenting.Reindenting using RStudio.formatR package: I run tidy.dir() beforereading student code!Google R style guide:Hadley’s R style guide:

11

Page 36: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Outline

1 Getting help

2 Finding functions

3 Digging into functions

4 Writing functions

5 Debugging

6 Version control

7 My R workflow

12

Page 37: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

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 afuture release.

13

Page 38: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

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 afuture release.

13

Page 39: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

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 afuture release.

13

Page 40: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

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 afuture release.

13

Page 41: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

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 afuture release.

13

Page 42: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

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 afuture release.

13

Page 43: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

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 afuture release.

13

Page 44: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

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 afuture release.

13

Page 45: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Outline

1 Getting help

2 Finding functions

3 Digging into functions

4 Writing functions

5 Debugging

6 Version control

7 My R workflow

14

Page 46: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Version control.

......In any non-trivial project, it is important to keeptrack 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

Page 47: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Version control.

......In any non-trivial project, it is important to keeptrack 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

Page 48: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Version control.

......In any non-trivial project, it is important to keeptrack 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

Page 49: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Version control.

......In any non-trivial project, it is important to keeptrack 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

Page 50: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Version control.

......In any non-trivial project, it is important to keeptrack 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

Page 51: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Version control.

......In any non-trivial project, it is important to keeptrack 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

Page 52: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Version control.

......In any non-trivial project, it is important to keeptrack 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

Page 53: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Version control.

......In any non-trivial project, it is important to keeptrack 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

Page 54: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Outline

1 Getting help

2 Finding functions

3 Digging into functions

4 Writing functions

5 Debugging

6 Version control

7 My R workflow

16

Page 55: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

R projects

Basic idea

Every paper, book or consulting report is a“project”.

Every project has its own folder and Rworkspace.

Every project is entirely scripted. That is, allanalysis, graphs and tables must be able to begenerated by running one R script. The finalreport must be able to be generated byprocessing one LATEX file.

17

Page 56: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

R projects

Basic idea

Every paper, book or consulting report is a“project”.

Every project has its own folder and Rworkspace.

Every project is entirely scripted. That is, allanalysis, graphs and tables must be able to begenerated by running one R script. The finalreport must be able to be generated byprocessing one LATEX file.

17

Page 57: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

R projects

Basic idea

Every paper, book or consulting report is a“project”.

Every project has its own folder and Rworkspace.

Every project is entirely scripted. That is, allanalysis, graphs and tables must be able to begenerated by running one R script. The finalreport must be able to be generated byprocessing one LATEX file.

17

Page 58: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

R projectsfunctions.R contains all non-packagedfunctions used in the project.

main.R sources all other R files in the correctorder.Data files provided by the client (ordownloaded from a website) are never edited.All data manipulation is scripted in R (orsome other language).Tables generated via xtable or texregpackages.Graphics in pdf format.Report or paper in LATEX pulls in the tables andgraphics.

18

Page 59: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

R projectsfunctions.R contains all non-packagedfunctions used in the project.main.R sources all other R files in the correctorder.

Data files provided by the client (ordownloaded from a website) are never edited.All data manipulation is scripted in R (orsome other language).Tables generated via xtable or texregpackages.Graphics in pdf format.Report or paper in LATEX pulls in the tables andgraphics.

18

Page 60: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

R projectsfunctions.R contains all non-packagedfunctions used in the project.main.R sources all other R files in the correctorder.Data files provided by the client (ordownloaded from a website) are never edited.All data manipulation is scripted in R (orsome other language).

Tables generated via xtable or texregpackages.Graphics in pdf format.Report or paper in LATEX pulls in the tables andgraphics.

18

Page 61: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

R projectsfunctions.R contains all non-packagedfunctions used in the project.main.R sources all other R files in the correctorder.Data files provided by the client (ordownloaded from a website) are never edited.All data manipulation is scripted in R (orsome other language).Tables generated via xtable or texregpackages.

Graphics in pdf format.Report or paper in LATEX pulls in the tables andgraphics.

18

Page 62: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

R projectsfunctions.R contains all non-packagedfunctions used in the project.main.R sources all other R files in the correctorder.Data files provided by the client (ordownloaded from a website) are never edited.All data manipulation is scripted in R (orsome other language).Tables generated via xtable or texregpackages.Graphics in pdf format.

Report or paper in LATEX pulls in the tables andgraphics.

18

Page 63: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

R projectsfunctions.R contains all non-packagedfunctions used in the project.main.R sources all other R files in the correctorder.Data files provided by the client (ordownloaded from a website) are never edited.All data manipulation is scripted in R (orsome other language).Tables generated via xtable or texregpackages.Graphics in pdf format.Report or paper in LATEX pulls in the tables andgraphics.

18

Page 64: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

R projects

.Advantages over Sweave and knitR..

......

å Keeps R and LATEX files separate.

å Multiple R files that can be runseparately.

å Easier to rebuild sections (e.g., onlysome R files).

å Easier to collaborate.

19

Page 65: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

R projects

.Advantages over Sweave and knitR..

......

å Keeps R and LATEX files separate.

å Multiple R files that can be runseparately.

å Easier to rebuild sections (e.g., onlysome R files).

å Easier to collaborate.

19

Page 66: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

R projects

.Advantages over Sweave and knitR..

......

å Keeps R and LATEX files separate.

å Multiple R files that can be runseparately.

å Easier to rebuild sections (e.g., onlysome R files).

å Easier to collaborate.

19

Page 67: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

R projects

.Advantages over Sweave and knitR..

......

å Keeps R and LATEX files separate.

å Multiple R files that can be runseparately.

å Easier to rebuild sections (e.g., onlysome R files).

å Easier to collaborate.

19

Page 68: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Figures without whitespaceR graphics have too much surrounding whitespace 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

Page 69: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Figures without whitespaceR graphics have too much surrounding whitespace 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

Page 70: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Figures without whitespaceR graphics have too much surrounding whitespace 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

Page 71: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Figures without whitespaceR graphics have too much surrounding whitespace 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

Page 72: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

R with Makefiles.

......

A Makefile provides instructions about how tocompile a project..Makefile..

......

# list R filesRFILES := $(wildcard *.R)# pdf figures created by RPDFFIGS := $(wildcard figs/*.pdf)# Indicator files to show R file has runOUT_FILES:= $(RFILES:.R=.Rdone)

all: $(OUT_FILES)

# RUN EVERY R FILE%.Rdone: %.R functions.RRscript $< && touch $@

21

Page 73: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

Makefiles for R & LATEX.Makefile..

......

# Usually, only these lines need changingTEXFILE= paperRDIR= ./figsFIGDIR= ./figs

# list R filesRFILES := $(wildcard $(RDIR)/*.R)# pdf figures created by RPDFFIGS := $(wildcard $(FIGDIR)/*.pdf)# Indicator files to show R file has runOUT_FILES:= $(RFILES:.R=.Rdone)# Indicator files to show pdfcrop has runCROP_FILES:= $(PDFFIGS:.pdf=.pdfcrop)

all: $(TEXFILE).pdf $(OUT_FILES) $(CROP_FILES)

22

Page 74: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

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.RRscript $< && touch $@

# CROP EVERY PDF FIG FILE$(FIGDIR)/%.pdfcrop: $(FIGDIR)/%.pdfpdfcrop $< $< && touch $@

# Compile main tex file$(TEXFILE).pdf: $(TEXFILE).tex $(OUT_FILES) $(CROP_FILES)latexmk -pdf -quiet $(TEXFILE)

23

Page 75: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

The magic of RStudioAn RStudio projectcan have anassociated Makefile.Then building andcleaning can be donefrom within RStudio..

...... rstudio.com

24

Page 76: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

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

Page 77: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

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

Page 78: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

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

Page 79: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

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

Page 80: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

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

Page 81: SimpleR tips, tricks and tools - Rob J. Hyndman · Goodhabits indentingandcommenting. ReindentingusingRStudio. formatRpackage:Iruntidy.dir()before readingstudentcode! GoogleRstyleguide:

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