Contents:
Why should you care about scripting style?
Recommended components in a R script file
Start a file with a program header
Format code chunks
End a file explicitly
1. R scripting styles
SPEAKER: LUN-HSIEN CHANG
AFFILIATION: GENETIC EPIDEMIOLOGY, QIMR BERGHOFER MEDICAL
RESEARCH INSTITUTE
MEETING: R USER GROUP 20181003, HERSTON CAMPUS, UQ, BRISBANE
6. • Follow an established coding standard, naming
conventions
• Respect advise from senior programmers
7. Recommended components in a R
script file
Start with a program header documenting
Program file name
Directory of input files
Directory of output files
Names of external functions
Names of functions created in this file
Time and major changes to the script file
Each code chunk has
A section header describing the purpose
Code written following Google’s R scripting guide
End the file with a “end of this program” header
8. Program header
What is the file name?
Who created it and when?
What is the main purpose of the file?
Does it depend on another file or function?
13. Code chunk
commenting your code
What to comment?
Above every function. This is called the function header and
provides information about the purpose of this "sub-component" of
the program. When and if there is only one function in a file, the
function header and file header comments should be merged into
a single comment. (See Function Header below)
In line. Any "tricky" code where it is not immediately obvious what
you are trying to accomplish, should have comments right above it
or on the same line with it.
14. Code chunk
commenting your code
How not to comment
Comments should be useful high level descriptions
of what the program is doing. They should not
restate something that is "obvious".
By using appropriate variable names, much of a
program can be (almost) as easy to read as English.
15. Code chunk
Google’s R scripting style
Bioconductor’s coding standards http://wiki.fhcrc.org/bioc/Coding_
Standards
Hadley Wickham’s style guide http://stat405.had.co.nz/r-style.html
Google’s R style guide http://google-styleguide.googlecode.com/
svn/trunk/google-r-style.html
Colin Gillespie’s R style guide http://csgillespie.wordpress.com/2010/
11/23/r-style-guide/
https://journal.r-project.org/archive/2012-2/RJournal_2012-2_Baaaath.pdf