R scripting styles
SPEAKER: LUN-HSIEN CHANG
AFFILIATION: GENETIC EPIDEMIOLOGY, QIMR BERGHOFER MEDICAL
RESEARCH INSTITUTE
MEETING: R USER GROUP 20181003, HERSTON CAMPUS, UQ, BRISBANE
Why should you
care about
scripting style?
What people think of my script files
What they may look like
?
• Follow an established coding standard, naming
conventions
• Respect advise from senior programmers
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
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?
Program header
Location of input, output files
Program header
Revision history
Program Header at PhUSE
https://www.phusewiki.org/wiki/index.php?title=Program_Header
Code chunk
section header
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.
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.
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
Explicitly end a file with a header
The Human Cost of Messy Code
How will my script files look like

R scripting styles

  • 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
  • 2.
    Why should you careabout scripting style?
  • 3.
    What people thinkof my script files
  • 4.
    What they maylook like
  • 5.
  • 6.
    • Follow anestablished coding standard, naming conventions • Respect advise from senior programmers
  • 7.
    Recommended components ina 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 isthe file name? Who created it and when? What is the main purpose of the file? Does it depend on another file or function?
  • 9.
    Program header Location ofinput, output files
  • 10.
  • 11.
    Program Header atPhUSE https://www.phusewiki.org/wiki/index.php?title=Program_Header
  • 12.
  • 13.
    Code chunk commenting yourcode  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 yourcode  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 Rscripting 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
  • 17.
    Explicitly end afile with a header
  • 18.
    The Human Costof Messy Code
  • 19.
    How will myscript files look like