Difference between revisions of "RPR-Coding style"

From "A B C"
Jump to navigation Jump to search
m
m
 
(12 intermediate revisions by the same user not shown)
Line 1: Line 1:
<div id="BIO">
+
<div id="ABC">
  <div class="b1">
+
<div style="padding:5px; border:1px solid #000000; background-color:#b3dbce; font-size:300%; font-weight:400; color: #000000; width:100%;">
 
R Coding Style
 
R Coding Style
  </div>
+
<div style="padding:5px; margin-top:20px; margin-bottom:10px; background-color:#b3dbce; font-size:30%; font-weight:200; color: #000000; ">
 
+
(R coding style; software development)
  {{Vspace}}
+
</div>
 
 
<div class="keywords">
 
<b>Keywords:</b>&nbsp;
 
R coding style; software development
 
 
</div>
 
</div>
  
{{Vspace}}
+
{{Smallvspace}}
  
  
__TOC__
+
<div style="padding:5px; border:1px solid #000000; background-color:#b3dbce33; font-size:85%;">
 
+
<div style="font-size:118%;">
{{Vspace}}
+
<b>Abstract:</b><br />
 
 
 
 
{{LIVE}}
 
 
 
{{Vspace}}
 
 
 
 
 
</div>
 
<div id="ABC-unit-framework">
 
== Abstract ==
 
 
<section begin=abstract />
 
<section begin=abstract />
<!-- included from "../components/RPR-Coding_style.components.wtxt", section: "abstract" -->
 
 
Now that you have encountered some concepts of R programming, how do you write <i>good</i> R code?
 
Now that you have encountered some concepts of R programming, how do you write <i>good</i> R code?
 
<section end=abstract />
 
<section end=abstract />
 
+
</div>
{{Vspace}}
+
<!-- ============================ -->
 
+
<hr>
 
+
<table>
== This unit ... ==
+
<tr>
=== Prerequisites ===
+
<td style="padding:10px;">
<!-- included from "../components/RPR-Coding_style.components.wtxt", section: "prerequisites" -->
+
<b>Objectives:</b><br />
<!-- included from "ABC-unit_components.wtxt", section: "notes-prerequisites" -->
 
You need to complete the following units before beginning this one:
 
*[[RPR-Plotting|RPR-Plotting (Introduction to R Plots)]]
 
 
 
{{Vspace}}
 
 
 
 
 
=== Objectives ===
 
<!-- included from "../components/RPR-Coding_style.components.wtxt", section: "objectives" -->
 
 
This unit will ...
 
This unit will ...
 
* ... introduce tried and proven principles of writing expressive and maintainable R code.
 
* ... introduce tried and proven principles of writing expressive and maintainable R code.
 
+
</td>
{{Vspace}}
+
<td style="padding:10px;">
 
+
<b>Outcomes:</b><br />
 
 
=== Outcomes ===
 
<!-- included from "../components/RPR-Coding_style.components.wtxt", section: "outcomes" -->
 
 
After working through this unit you ...
 
After working through this unit you ...
 
* ... can identify poor practice in formatting R code;
 
* ... can identify poor practice in formatting R code;
 
* ... know better;
 
* ... know better;
 
* ... begin incorporating these principles into your own practice.
 
* ... begin incorporating these principles into your own practice.
 +
</td>
 +
</tr>
 +
</table>
 +
<!-- ============================  -->
 +
<hr>
 +
<b>Deliverables:</b><br />
 +
<section begin=deliverables />
 +
<u>
 +
<li><b>Time management</b>: Before you begin, estimate how long it will take you to complete this unit. Then, record in your course journal: the number of hours you estimated, the number of hours you worked on the unit, and the amount of time that passed between start and completion of this unit.</li>
 +
<li><b>Journal</b>: Document your progress in your [[FND-Journal|Course Journal]]. Some tasks may ask you to include specific items in your journal. Don't overlook these.</li>
 +
<li><b>Insights</b>: If you find something particularly noteworthy about this unit, make a note in your [[ABC-Insights|'''insights!''' page]].</li>
 +
</u>
 +
<section end=deliverables />
 +
<!-- ============================  -->
 +
<hr>
 +
<section begin=prerequisites />
 +
<b>Prerequisites:</b><br />
 +
This unit builds on material covered in the following prerequisite units:<br />
 +
*[[RPR-Plotting|RPR-Plotting (Introduction to R Plots)]]
 +
<section end=prerequisites />
 +
<!-- ============================  -->
 +
</div>
  
{{Vspace}}
+
{{Smallvspace}}
  
  
=== Deliverables ===
 
<!-- included from "../components/RPR-Coding_style.components.wtxt", section: "deliverables" -->
 
<!-- included from "ABC-unit_components.wtxt", section: "deliverables-time_management" -->
 
*<b>Time management</b>: Before you begin, estimate how long it will take you to complete this unit. Then, record in your course journal: the number of hours you estimated, the number of hours you worked on the unit, and the amount of time that passed between start and completion of this unit.
 
<!-- included from "ABC-unit_components.wtxt", section: "deliverables-journal" -->
 
*<b>Journal</b>: Document your progress in your [[FND-Journal|Course Journal]]. Some tasks may ask you to include specific items in your journal. Don't overlook these.
 
<!-- included from "ABC-unit_components.wtxt", section: "deliverables-insights" -->
 
*<b>Insights</b>: If you find something particularly noteworthy about this unit, make a note in your [[ABC-Insights|'''insights!''' page]].
 
  
{{Vspace}}
+
{{Smallvspace}}
  
  
=== Evaluation ===
+
__TOC__
<!-- included from "../components/RPR-Coding_style.components.wtxt", section: "evaluation" -->
 
<!-- included from "ABC-unit_components.wtxt", section: "eval-none" -->
 
<b>Evaluation: NA</b><br />
 
:This unit is not evaluated for course marks.
 
  
 
{{Vspace}}
 
{{Vspace}}
  
  
</div>
+
=== Evaluation ===
<div id="BIO">
+
<b>Evaluation: NA</b><br />
 +
<div style="margin-left: 2rem;">This unit is not evaluated for course marks.</div>
 
== Contents ==
 
== Contents ==
<!-- included from "../components/RPR-Coding_style.components.wtxt", section: "contents" -->
+
What do we even mean by "good" R code? ... This unit is one of those that you will need to come back to from time to time. It won't make a lot of sense to you until you have actually encountered code, read it and written some yourself. So don't try to memorize these principles, but review them every four weeks or so.
What do we even mean by "good" R code? ...
 
  
 
<div class="alert">
 
<div class="alert">
Line 94: Line 77:
 
Warning:
 
Warning:
 
<small>
 
<small>
Coding style is a volatile topic. Friendships have been renounced, eternal vows of marriage have been dissolved, stock-options have been lost, all over a disagreement about the One True Brace Style, or whether <tt>fetchSequenceFromPDB()</tt>is a good function name or not. I am laying out coding rules below that reflect a few years of experience. They work for me, they may not work for you.
+
Coding style is a volatile topic. Friendships have been renounced, eternal vows of marriage have been dissolved, stock-options have been lost, all over a disagreement about the One True Brace Style, or whether <tt>fetchSequenceFromPDB()</tt>is a good function name or whether it '''must''' be  <tt>fetch.sequence.from.PDB()</tt> instead. I am laying out coding rules below that reflect a few years of experience. They work for me, they may not work for you.
  
 
'''However''':
 
'''However''':
Line 115: Line 98:
  
 
==General==
 
==General==
 +
 +
One of the goals of the coding style expressed below is that the code should be easy to read for people for whom R is not the first language, or even the language of choice. There are many things that R-purists might do differently, however those code idioms probably are not well suited for a research collaboration in which people speak python, C++, javascript and R all at the same time.
  
 
It should always be your goal to code as clearly and explicitly as possible. '''R''' has many complex idioms, and since it is a functional language that can generally insert functions anywhere into expressions, it is possible to write very terse, expressive code. Use this with discretion. Pace yourself, and make sure your reader can follow your flow of thought. You should aim for a generic coding style that can easily be translated to other languages if necessary, and easily understood by others whose background is in another language. And resist being crafty: more often than not the poor soul who will be confused by a particularly witty use of the language will be you, yourself, half a year later. There is an astute observation by Brian Kernighan that applies completely:
 
It should always be your goal to code as clearly and explicitly as possible. '''R''' has many complex idioms, and since it is a functional language that can generally insert functions anywhere into expressions, it is possible to write very terse, expressive code. Use this with discretion. Pace yourself, and make sure your reader can follow your flow of thought. You should aim for a generic coding style that can easily be translated to other languages if necessary, and easily understood by others whose background is in another language. And resist being crafty: more often than not the poor soul who will be confused by a particularly witty use of the language will be you, yourself, half a year later. There is an astute observation by Brian Kernighan that applies completely:
Line 123: Line 108:
 
* Never sacrifice '''being explicit''' for saving on keystrokes. Code is read much more often than it is written!
 
* Never sacrifice '''being explicit''' for saving on keystrokes. Code is read much more often than it is written!
  
* Use lots of '''comments'''. Don't describe what the code does, but explain '''why'''.
+
* Use lots of '''comments'''. Don't describe what the code does, but explain '''why''' you wrote it that way.
* '''Indent''' comment hashes to align with the expressions in a block.
+
* '''Indent''' comment "#"-characters to align with the expressions in a block.
* Use only {{c|<-}} for assignment, not {{c|{{=}}}}
+
* Use only <code>&lt;-</code> for assignment, not <code>=</code>
* ...but do use {{c|{{=}}}} when passing values into the arguments of functions.
+
* ...but do use <code>=</code> when passing values into the arguments of functions.
* Don't use {{c|<<-}} (global assignment) except in very unusual cases. Actually never.
+
* Define global constants  at the beginning of the code, use all caps names for such constants (e.g. {{c|MAXWIDTH}}). Never have "magic numbers" appear in your code.
* Define global variables at the beginning of the code, use all caps variable names ({{c|MAXWIDTH}}) for such parameters. Never have "magic numbers" appear in your code.
+
 
* If such variables are meant to be ''truly'' global use {{c|options()}} to set them.
+
* Always use <code>for (i in seq(along = x)) {...}</code>  rather than  <code>for (i in 1:length(x)) {...}</code> because if <code>x == NULL</code> the loop is executed once, with an undefined variable.
  
 
* Don't use {{c|attach()}}.
 
* Don't use {{c|attach()}}.
* Always use {{c|for (i in seq(along{{=}}x)) {...}}}  rather than {{c|for (i in 1:length(x)) {...}}} because if {{c|x {{=}}{{=}} NULL}} the loop is executed once, with an undefined variable.
+
* Avoid importing functions wholesale from packages with {{c|library()}}. Rather use the {{c|<package>::<function>()}} syntax to make it clear which function you mean<ref>It is happening more and more frequently that functions in different packages we load have the same name. Then our code's behaviour will depend on the order in which the libraries were loaded. Evil.</ref>. That's what package namespaces are for in the first place.
 +
 
 +
 
 +
Bad:
 +
<pre>
 +
library(igraph)
 +
 
 +
...
 +
 
 +
...
 +
 
 +
clu <- components(g)
 +
</pre>
 +
 
 +
Good:
 +
<pre>
 +
clu <- igraph::components(g)
 +
</pre>
 +
 
 +
 
 +
 
 +
{{Vspace}}
 +
 
 +
===Don't change the global state===
 +
{{Smallvspace}}
 +
We '''do''' understand why our functions should not have side-effects (other than the explicit intended effects of printing, plotting, or writing files). But there are subtle ways to change the global state that we need to remember - and avoid. Here's an obvious one:
 +
 
 +
* Don't use <code><<-</code> (global assignment) except in very unusual cases. Actually never.
 +
 
 +
Less obvious is:
 +
 
 +
* Don't use {{c|set.seed()}} in functions.
 +
 
 +
{{c|set.seed()}} changes the state of the Random Number Generator (RNG), which is part of Rs global state. If this state is changed inside a function, it might result in  '''vastly''' smaller space of random numbers than you would expect. Even resetting the RNG is not a good idea: a repeatable script might require the RNG to be in a defined state and if your function does {{c|set.seed(NULL)}}, your enclosing script is no longer repeatable. But of course, we need to be able to compute simulations repeatably. The only acceptable idiom is something like:
 +
 
 +
<pre>
 +
mySim <- function(N) { ...
 +
  # do something random N times
 +
  return(result)
 +
}
 +
 
 +
set.seed(112358)                      # set RNG seed for repeatable randomness
 +
x <- mySim(N)
 +
set.seed(NULL)                        # reset the RNG
 +
</pre>
 +
 
 +
Then you can comment out the lines, or change them to a different seed, or reset the RNG with {{c|set.seed(NULL)}} - everything is explicit.
 +
 
 +
{{Vspace}}
 +
 
 +
===Don't use <tt>save()</tt> and <tt>load()</tt>===
 +
{{Smallvspace}}
 +
This is a corrollary to the principle not to change the global state. <code>save() / load()</code> saves '''one or more R objects''' and restores them to the same object name(s) they originally had. But there is no good way to know what that object name is. If you already have an object by that name in your global workspace, it gets overwritten!
 +
 
 +
The sane alternative is to use <code>writeRDS() / readRDS()</code>. <code>writeRDS()</code> serializes, compresses and saves '''a single R object''', and <code>readRDS()</code> restores the object and returns it as a return value, thus it can be assigned:
 +
<pre>
 +
save(aThing, file="savedAthing.rds")
 +
myNewThing <- readRDS("savedAthing.rds")
 +
</pre>
  
  
Line 140: Line 183:
  
 
* Limit yourself to 80 characters per line.
 
* Limit yourself to 80 characters per line.
* Don't use semicolons to write more than one expression on a line.
+
* Don't write more than one expression on a line.
  
  
Line 156: Line 199:
  
  
* If the code for a function does not fit on approxiamtaley one prnted page, you should probably break it up further.
+
* If the code for a function does not fit on approximately one printed page, you should probably break it up further.
  
* if your loops or conditionals are nested more than three levels deep, you should rethink the logic.
+
* if your loops or conditionals are nested more than three levels deep, you should rethink the logic. Actually three is already a lot. Unless you are working on 3D-objects.
  
  
Line 165: Line 208:
 
==Headers==
 
==Headers==
  
* Give your '''sources''' headers stating purpose, author, date and version information, and note bugs and issues.
+
* Give your '''script files''' headers that state purpose, author, date and version information, and note bugs and issues.
* Give your '''functions''' headers that describe purpose, arguments (including required datatypes), and return values. Callers should be able to work with the function without having to read the code.
+
* Give your '''functions''' headers that describe purpose, parameters (including required datatypes), and return values. Callers should be able to work with the function without having to read the code.
  
  
Line 180: Line 223:
  
 
* In mathematical expressions, always use '''parentheses''' to define priority explicitly. Never rely on implicit operator priority. {{c|(( 1 + 2 ) / 3 ) * 4}}
 
* In mathematical expressions, always use '''parentheses''' to define priority explicitly. Never rely on implicit operator priority. {{c|(( 1 + 2 ) / 3 ) * 4}}
* Always use braces {{c|{}}}, even if you write single-line {{c|if}} statements and loops.
+
* Always use braces <code>{ }</code>, even if you write single-line {{c|if}} statements and loops where they are not legally required.
  
 
{{Vspace}}
 
{{Vspace}}
Line 189: Line 232:
  
 
Good:
 
Good:
<source lang="R">
+
<pre>
 
if (silent) { ...
 
if (silent) { ...
</source>
+
</pre>
  
 
Bad:
 
Bad:
<source lang="R">
+
<pre>
 
if(silent) { ...
 
if(silent) { ...
</source>
+
</pre>
  
  
* Always separate operators and arguments with spaces.<ref>Separating operators with spaces is especially important for the assignment operator {{c|<-}}. Consider this: {{c| myPreciousData < -2}} returns a vector of {{c|TRUE}} and {{c|FALSE}}, depending on whether the values in {{c|myPreciousData}} are less than -2. But {{c| myPreciousData<-2}} overwrites every single element with the number {{c|2}}!</ref><ref>The {{c|{{=}}}} sign is a bit of a special case. When I write e.g. a plot statement, or construct a dataframe, I prefer '''not''' to use spaces if the expression ends up all on one line, but to '''use''' spaces when the arguments are on separate lines.</ref>
+
* Always separate operators and arguments with spaces.<ref>Separating operators with spaces is especially important for the assignment operator {{c|<-}}. Consider this: {{c| myPreciousData < -2}} returns a vector of {{c|TRUE}} and {{c|FALSE}}, depending on whether the values in {{c|myPreciousData}} are less than -2. But {{c| myPreciousData<-2}} overwrites every single element with the number {{c|2}}! I'm not even making this up - happened to a student in a workshop I taught.</ref><ref>The <code>=</code> sign is a bit of a special case. When I write e.g. a plot statement, or construct a dataframe, I prefer '''not''' to use spaces if the expression ends up all on one line, but to '''use''' spaces when the arguments are on separate lines.</ref>
 
* Never separate function names and their following parentheses with spaces.
 
* Never separate function names and their following parentheses with spaces.
* Always use a '''space''' after a comma, and never before a comma.
+
* Always use a '''space''' after a comma, and never before a comma. Except in subsetting expressions, where we don't want the comma to hide against the bracket.
  
 
Good:
 
Good:
<source lang="R">
+
<pre>
 
print(1 / 3, digits = 10)
 
print(1 / 3, digits = 10)
if (! id %in% IDs) { ...
+
if (! id %in% IDs) { ...'
</source>
+
expressionProfiles[ , 1:3]
 +
</pre>
  
 
Bad:
 
Bad:
<source lang="R">
+
<pre>
print (1 / 3 ,digits=10)
+
print (1 / 3 ,digits=10)     # space before the comma
if (!id %in% IDs) { ...
+
if (!id %in% IDs) { ...     # "!" hides against the parenthesis
</source>
+
expressionProfiles[, 1:3]    # "," hides against the bracket
 +
</pre>
  
  
Line 227: Line 272:
  
 
* Use informative and specific '''filenames''' for code sources; give them the extension {{c|.R}}
 
* Use informative and specific '''filenames''' for code sources; give them the extension {{c|.R}}
* Periods have a syntactic meaning in object-oriented classes. I consider their use in normal variables names wrong, even though this is not a syntax error.
+
* Periods have a syntactic meaning in object-oriented classes. I consider their use in normal variables names '''wrong''', even though this is not a syntax error and many R library functions have such names (e.g. <code>Sys.time()</code> and other system calls.)
* Alphabetically sort names for related together, code autocomplete will be more useful.
+
* Create names so that related variables or functions are alphabetically sorted together, code autocomplete will be more useful.
* Use the concise {{c|camelCaseStyle}} for variable names, don't use the {{c|confusing.dot.style}} or the rambling {{c|pothole_style}}.
+
* Use the concise {{c|camelCaseStyle}} for variable names, don't use the {{c|confusing.dot.style}} or the rambling {{c|pothole_style}}<ref>But nevert hesitate to make exceptions if this makes your code more legible.</ref><ref>This is not a random opinion but based on that it's easier to keep within the 80-character line limit. Also see the [http://citeseerx.ist.psu.edu/viewdoc/similar?doi=10.1.1.158.9499&type=ab linked article]</ref>.
* Don't abbreviate argument names. You can, but you shouldn't.
+
* Don't abbreviate argument names when calling functions. You can, but you shouldn't.
  
* Never reassign reserved words.
+
* Never reassign reserved words<ref>In my opinion, base R uses far too many function names that would be useful for variables. But we're not going to change that. So I often just prefix my variable names with <code>my</code> or <code>this</code>, eg <code>myDf, thisLength</code> etc.</ref>.
 
* Don't use {{c|c}} as a variable name since {{c|c()}}  is a function.
 
* Don't use {{c|c}} as a variable name since {{c|c()}}  is a function.
* Don't call your data frames {{c|df}} since {{c|df()}} is a function.<ref>Here are more names that may seem attractive as variable names but that are in fact functions in the '''base R''' package and thus may cause confusion: <code> all(), args(), attr(), beta(), body(), col(), date(), det(), diag(), diff(), dim(), dir(), dumpp(), eigen(), file(), files(), gamma(), kappa(), length(), list(), load(), log(), max(), mean(), min(), open(), q(), raw(), row(), sample(), seq(), sub(), summary(), table(), type(), url(), vector(), and version()</code>. I'm sure you get the idea - composite names of the type proposed above in CamelCase are usually safe.</ref>
+
* Don't call your data frames {{c|df}} since {{c|df()}} is a function.<ref>Here are more names that may seem attractive as variable names but that are in fact functions in the '''base R''' package and thus may cause confusion: <code> all(), args(), attr(), beta(), body(), col(), date(), det(), diag(), diff(), dim(), dir(), dump(), eigen(), file(), files(), gamma(), kappa(), length(), list(), load(), log(), max(), mean(), min(), open(), q(), raw(), row(), sample(), seq(), sub(), summary(), table(), type(), url(), vector(), and version()</code>. I'm sure you get the idea - composite names of the type proposed above in CamelCase are usually safe.</ref>
  
* Name length should be commensurate with the scope of a variable. Short names for local scope. More explicit names for global scope. I often write global parameters in ALL CAPS: {{c|MAXWIDTH}} if they are defined at the top of a code module.
+
* Name length should be commensurate with the scope of a variable. Short names for local scope. More explicit names for global scope. I often write global constants<ref>"constants" - i.e. variables that are not supposed to change; R has no "constants" in the actual sense</ref> in ALL CAPS: (e.g. {{c|MAXWIDTH}}) if they are defined at the top of a code module.
  
 
;Specific naming conventions I like:
 
;Specific naming conventions I like:
:{{c|isValid}}, {{c|hasNeighbour}}  ... Boolean variables
+
<ul>
:{{c|findRange()}}, {{c|getLimits()}} ... simple function names (verbs!)
+
* {{c|isValid}}, {{c|hasNeighbour}}  ... Boolean variables
:{{c|initializeTable()}} ... not {{c|initTab()}}
+
* {{c|findRange()}}, {{c|getLimits()}} ... simple function names (verbs!)
:Use plurals to good advantage. {{c|node}} ... for one element; {{c|nodes}} ... for more elements - you can then write code like:
+
* {{c|initializeTable()}} ... not {{c|initTab()}}
<source lang = "R">
+
* Use plurals to good advantage. {{c|node}} ... for one element; {{c|nodes}} ... for more elements - you can then write code like:
 +
</ul>
 +
<pre>
 
for (node in nodes) { ...
 
for (node in nodes) { ...
</source>
+
</pre>
:{{c|nPoints}} ... for number-of
+
<ul>
:{{c|iPoints}} ... for number-of
+
* <code>nPoints</code> ... for number-of
:{{c|isError}} ... not {{c|isNotError}}: avoid double negation
+
* <code>iPoints</code> ... for indices-of-points
 
+
* <code>isError</code> ... don't use {{c|isNotError}}: avoid double negation
 +
</ul>
 
{{Vspace}}
 
{{Vspace}}
  
 
==Conditionals==
 
==Conditionals==
 +
 +
This may be controversial. The code block in an <code>if (&lt;condition&gt;) {...}</code> statement is evaluated if <code>&lt;condition&gt;</code> is <code>TRUE</code>. But what if we use a boolean variable in the condition? Should we write:
 +
 +
<pre>
 +
if (<boolean variable>) { ...
 +
</pre>
 +
 +
or
 +
 +
<pre>
 +
if (<boolean variable> == TRUE) { ...
 +
</pre>
 +
 +
It depends. Remember - the goal is to make your code as explicit and readable as possible. If our variable is e.g. <code>a</code>, then ...
 +
 +
<pre>
 +
if (a) { ...
 +
</pre>
 +
 +
... is '''not''' good. Better write ...
 +
 +
<pre>
 +
if (a == TRUE) { ...
 +
</pre>
 +
 +
... and treat this as any other condition that needs to be evaluated. '''However''' - if you have given this a meaningful variable name in the first place, something like ...
 +
 +
<pre>
 +
if (recordIsValid) { ...
 +
</pre>
 +
 +
... is great, whereas ...
 +
 +
<pre>
 +
if (recordIsValid == TRUE) { ...
 +
</pre>
 +
 +
... is something that feels oddly self-contradictory. So best practice here depends on context. Myself,  I more often than not end up write {{c|if (something-something-that-is-boolean == TRUE) ...}}, (and that's '''not''' because I don't understand how conditionals work).
 +
 +
Make the {{c|FALSE}} behaviour explicit. Always use an {{c|else}} at the end of a conditional to define what your code does if the condition is not {{c|TRUE}}. Otherwise your reader will wonder whether your code covers all cases. What if your code '''should''' do nothing in the {{c|FALSE}} case? Make that explicit:
 +
 +
<pre>
 +
if (a > b) {
 +
  tmp <- b
 +
  b <- a
 +
  a <- tmp
 +
} else {
 +
  ;        # does nothing
 +
}
 +
</pre>
  
  
Line 259: Line 357:
 
==Indent Style==
 
==Indent Style==
  
No need for much discussion. Follow the One True Bracing Style and we will both be happy. If you don't immediately see why: read about {{WP|Indent style|indent style here}}.
+
No need for much discussion. Follow the One True Bracing Style and we will all be happy. If you don't immediately see why: read about {{WP|Indentation_style#Variant:_1TBS_(OTBS)|indent style here}}. (1) Opening brace on the same line as the function or control declaration; (2) closing brace aligned with the declaration; (3) braces mandatory even if there is only one statement to execute. Sample:
 +
 
 +
<pre>
 +
if (length(x) > 1) {
 +
  perm <- sample(x)
 +
} else if (length(x) > 0) {
 +
  perm <- x
 +
} else  {
 +
  perm <- NULL
 +
}
 +
</pre>
  
  
Line 272: Line 380:
 
==Loops==
 
==Loops==
  
 +
Pre-allocate your result objects to have the correct size if at all possible. Growing objects dynamically with {{c|c()}}, {{c|cbind()}}, or {{c|rbind()}} is much, much slower.
  
 +
Use {{c|seq_along()}}, not {{c|length()}} to compute the range of index variables. If the object you are iterating over has length zero (i.e. it is {{c|NULL}}, like e.g. the result of a {{c|grep()}} operation if the pattern was not found) then using ...
 +
 +
<pre>
 +
for (idx in 1:length(myVector)) { ...
 +
</pre>
 +
 +
... will result in an iteration range of {{c|1:0}} since {{c|length(NULL)}} is zero, and the loop will be executed twice even though it should not have been. The correct and safe way to iterate is ...
 +
 +
<pre>
 +
for (idx in seq_along(myVector)) { ...
 +
</pre>
 +
 +
... which will not execute since {{c|seq_along(NULL)}} is {{c|NULL}}.
  
 
{{Vspace}}
 
{{Vspace}}
Line 278: Line 400:
 
==Functions==
 
==Functions==
  
* Always '''explicitly return''' values from functions, never rely on the implicit behaviour that returns the last expression. This is not superfluous, it's explicit.
+
* Explicitly assign values to crucial function arguments, even if you think you know that that value is the language default. For example ...
 +
 
 +
<pre>
 +
sort(x)
 +
</pre>
 +
 
 +
... sorts in increasing order, smallest first. But even though ...
 +
 
 +
<pre>
 +
sort(x, decreasing = FALSE)
 +
</pre>
 +
 
 +
... does the same thing, the expression '''explicitly''' tells the reader what it is going to do. And that's good.
 +
 
 +
* Always '''explicitly return''' values from functions, never rely on the implicit behaviour that returns the last expression. This is not superfluous, it is explicit.
 +
 
 +
* If there is nothing to return because the function is invoked for its side effects of writing a file or plotting a graph, write it into your code that nothing will be returned. This prevents you from accidentally returning the result of last expression anyway (as the language does by default), or the reader might think you forgot something. The idiom is:
 +
 
 +
<pre>
 +
return(invisible(NULL))
 +
</pre>
  
 
* In general, return only from the end of the function, not from multiple places.
 
* In general, return only from the end of the function, not from multiple places.
* Explicitly assign values to crucial function arguments, even if you think you know that that value is the default.
 
  
 
{{Vspace}}
 
{{Vspace}}
Line 289: Line 430:
 
If possible, do not grow data structures dynamically, but create the whole structure with "empty" values, then assign values to its elements. This is '''much''' faster.
 
If possible, do not grow data structures dynamically, but create the whole structure with "empty" values, then assign values to its elements. This is '''much''' faster.
  
<source lang = "rsplus">
+
<pre>
  # This is bad:
+
  # This is really bad:
  v <- numeric()
+
  system.time({
for (i in 1:100000) {
+
  N <- 100000
    v <- c(v, sqrt(i))
+
  v <- numeric()
  }
+
  for (i in 1:N) {
 +
      v <- c(v, sqrt(i))
 +
  }
 +
  })
 
     user  system elapsed
 
     user  system elapsed
  20.192  2.182 22.540
+
  16.718  11.258 27.988
  
  # This is marginally better:
+
  # Even only writing directly to new elements is much, much better:
  v <- numeric()
+
  system.time({
for (i in 1:100000) {
+
  N <- 100000
    v[i] <- sqrt(i)
+
  v <- numeric()
  }
+
  for (i in 1:N) {
 +
      v[i] <- sqrt(i)
 +
  }
 +
  })
 
   user  system elapsed
 
   user  system elapsed
14.185   2.036  16.230
+
  0.025  0.003   0.027
 +
 
 +
# That's abaout as fast as doing the same thing with a vapply() function.
  
  # This is much, much better (200 times faster):
+
  # The fastest way is to preallocate memory, it actually comes close to the
 +
# vectorized operation:
  
  v <- numeric(100000)
+
  system.time({
for (i in seq_along(v)) {
+
  N <- 100000
    v[i] <- sqrt(i)
+
  v <- numeric(N)
  }
+
  for (i in seq_along(v)) {
 +
      v[i] <- sqrt(i)
 +
  }
 +
  })
 
   user  system elapsed
 
   user  system elapsed
   0.101   0.008   0.108
+
   0.008   0.000   0.007
</source>
 
  
  
 +
# Using a vectorized operation is the fastest approach overall and the
 +
# method of choice:
  
{{Vspace}}
+
system.time({ v <- sqrt(1:100000) })
 +
  user  system elapsed
 +
  0.001  0.001  0.002
  
==<tt># [END]</tt>==
+
</pre>
  
*Always end your code with an {{c|# [END]}} comment. This way you can be sure it was copied or saved completely and nothig has been inadvertently omitted. This is important in teamwork. If even ONE team member does not adhere to this, it invalidates the efforts of EVERYONE.
+
Don't buy into the "apply is good, for-loop is bad" nonsense that you might encounter on the Web. Especially not if you need speed: a well-written for-loop will outperform an apply() statement, which internally uses a for-loop anyway. The reason we often use apply() is because we are following a functional programming idiom, not because there is something magical and exalted about the apply() function. It's usually a bit subtle which idiom is "better" at any given time. But <code>apply()</code> is NOT as trivial to be understood by a python or C programmer as a for-loop is.
  
  
 
{{Vspace}}
 
{{Vspace}}
  
 +
==<tt># [END]</tt>==
 +
 +
*Always end your code with an {{c|# [END]}} comment. This way you can be sure it was copied or saved completely and nothig has been inadvertently omitted. This is important in teamwork. If even ONE team member does not adhere to this, it invalidates the efforts of EVERYONE.
  
  
Line 340: Line 499:
 
* [http://agileinaflash.blogspot.ca/2009/02/meaningful-names.html Tim Ottinger on Naming]
 
* [http://agileinaflash.blogspot.ca/2009/02/meaningful-names.html Tim Ottinger on Naming]
 
* [https://www.joelonsoftware.com/2005/05/11/making-wrong-code-look-wrong/ Joel Spolski on '''collocation''']
 
* [https://www.joelonsoftware.com/2005/05/11/making-wrong-code-look-wrong/ Joel Spolski on '''collocation''']
 
 
{{Vspace}}
 
 
  
 
== Notes ==
 
== Notes ==
<!-- included from "../components/RPR-Coding_style.components.wtxt", section: "notes" -->
 
<!-- included from "ABC-unit_components.wtxt", section: "notes" -->
 
 
<references />
 
<references />
  
 
{{Vspace}}
 
{{Vspace}}
  
 
</div>
 
<div id="ABC-unit-framework">
 
== Self-evaluation ==
 
<!-- included from "../components/RPR-Coding_style.components.wtxt", section: "self-evaluation" -->
 
<!--
 
=== Question 1===
 
 
Question ...
 
 
<div class="toccolours mw-collapsible mw-collapsed" style="width:800px">
 
Answer ...
 
<div class="mw-collapsible-content">
 
Answer ...
 
 
</div>
 
  </div>
 
 
  {{Vspace}}
 
 
-->
 
 
{{Vspace}}
 
 
 
 
{{Vspace}}
 
 
 
<!-- included from "ABC-unit_components.wtxt", section: "ABC-unit_ask" -->
 
 
----
 
 
{{Vspace}}
 
 
<b>If in doubt, ask!</b> If anything about this learning unit is not clear to you, do not proceed blindly but ask for clarification. Post your question on the course mailing list: others are likely to have similar problems. Or send an email to your instructor.
 
 
----
 
 
{{Vspace}}
 
  
 
<div class="about">
 
<div class="about">
Line 401: Line 514:
 
:2017-08-05
 
:2017-08-05
 
<b>Modified:</b><br />
 
<b>Modified:</b><br />
:2017-09-10
+
:2020-09-18
 
<b>Version:</b><br />
 
<b>Version:</b><br />
:1.0
+
:1.3
 
<b>Version history:</b><br />
 
<b>Version history:</b><br />
 +
*1.3 Deprecate save()/load()
 +
*1.2 Some practical revisions
 +
*1.1.1 Maintenance
 +
*1.1 Add: Don't change the global state; Avoid library()
 +
*1.0.1 Maintenance
 
*1.0 Completed to first live version
 
*1.0 Completed to first live version
 
*0.1 Material collected from previous tutorial
 
*0.1 Material collected from previous tutorial
 
</div>
 
</div>
[[Category:ABC-units]]
 
<!-- included from "ABC-unit_components.wtxt", section: "ABC-unit_footer" -->
 
  
 
{{CC-BY}}
 
{{CC-BY}}
  
 +
[[Category:ABC-units]]
 +
{{UNIT}}
 +
{{LIVE}}
 
</div>
 
</div>
 
<!-- [END] -->
 
<!-- [END] -->

Latest revision as of 09:28, 25 September 2020

R Coding Style

(R coding style; software development)


 


Abstract:

Now that you have encountered some concepts of R programming, how do you write good R code?


Objectives:
This unit will ...

  • ... introduce tried and proven principles of writing expressive and maintainable R code.

Outcomes:
After working through this unit you ...

  • ... can identify poor practice in formatting R code;
  • ... know better;
  • ... begin incorporating these principles into your own practice.

Deliverables:

  • Time management: Before you begin, estimate how long it will take you to complete this unit. Then, record in your course journal: the number of hours you estimated, the number of hours you worked on the unit, and the amount of time that passed between start and completion of this unit.
  • Journal: Document your progress in your Course Journal. Some tasks may ask you to include specific items in your journal. Don't overlook these.
  • Insights: If you find something particularly noteworthy about this unit, make a note in your insights! page.

  • Prerequisites:
    This unit builds on material covered in the following prerequisite units:


     



     



     


    Evaluation

    Evaluation: NA

    This unit is not evaluated for course marks.

    Contents

    What do we even mean by "good" R code? ... This unit is one of those that you will need to come back to from time to time. It won't make a lot of sense to you until you have actually encountered code, read it and written some yourself. So don't try to memorize these principles, but review them every four weeks or so.

    Warning: Coding style is a volatile topic. Friendships have been renounced, eternal vows of marriage have been dissolved, stock-options have been lost, all over a disagreement about the One True Brace Style, or whether fetchSequenceFromPDB()is a good function name or whether it must be fetch.sequence.from.PDB() instead. I am laying out coding rules below that reflect a few years of experience. They work for me, they may not work for you.

    However:

    • If you are taking one of my workshops, I recommend you to follow these rules: I write this way, and we will find it easier to communicate if you do too.
    • If you are collaborating on a software project, these rules embody the standard across the project, and I will not check-in code that deviates. Here, consistency is key; but if you think you have a better approach, you only need to convince me and we will change the rule and apply it throughout the codebase[1].
    • If you are taking one of my courses, you may lose marks if you do not adhere to these standards. Of course, following rules must not be done blindly – we are training future collaborators, not parrots – but you need to write in the spirit of the one rule we all agree on:

    Well written code helps the reader to understand the intent.


     




     

    General

    One of the goals of the coding style expressed below is that the code should be easy to read for people for whom R is not the first language, or even the language of choice. There are many things that R-purists might do differently, however those code idioms probably are not well suited for a research collaboration in which people speak python, C++, javascript and R all at the same time.

    It should always be your goal to code as clearly and explicitly as possible. R has many complex idioms, and since it is a functional language that can generally insert functions anywhere into expressions, it is possible to write very terse, expressive code. Use this with discretion. Pace yourself, and make sure your reader can follow your flow of thought. You should aim for a generic coding style that can easily be translated to other languages if necessary, and easily understood by others whose background is in another language. And resist being crafty: more often than not the poor soul who will be confused by a particularly witty use of the language will be you, yourself, half a year later. There is an astute observation by Brian Kernighan that applies completely:

    "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."


    • Never sacrifice being explicit for saving on keystrokes. Code is read much more often than it is written!
    • Use lots of comments. Don't describe what the code does, but explain why you wrote it that way.
    • Indent comment "#"-characters to align with the expressions in a block.
    • Use only <- for assignment, not =
    • ...but do use = when passing values into the arguments of functions.
    • Define global constants at the beginning of the code, use all caps names for such constants (e.g. MAXWIDTH). Never have "magic numbers" appear in your code.
    • Always use for (i in seq(along = x)) {...} rather than for (i in 1:length(x)) {...} because if x == NULL the loop is executed once, with an undefined variable.
    • Don't use attach().
    • Avoid importing functions wholesale from packages with library(). Rather use the <package>::<function>() syntax to make it clear which function you mean[2]. That's what package namespaces are for in the first place.


    Bad:

    library(igraph)
    
    ...
    
    ...
    
    clu <- components(g)
    

    Good:

    clu <- igraph::components(g)
    



     

    Don't change the global state

     

    We do understand why our functions should not have side-effects (other than the explicit intended effects of printing, plotting, or writing files). But there are subtle ways to change the global state that we need to remember - and avoid. Here's an obvious one:

    • Don't use <<- (global assignment) except in very unusual cases. Actually never.

    Less obvious is:

    • Don't use set.seed() in functions.

    set.seed() changes the state of the Random Number Generator (RNG), which is part of Rs global state. If this state is changed inside a function, it might result in vastly smaller space of random numbers than you would expect. Even resetting the RNG is not a good idea: a repeatable script might require the RNG to be in a defined state and if your function does set.seed(NULL), your enclosing script is no longer repeatable. But of course, we need to be able to compute simulations repeatably. The only acceptable idiom is something like:

    mySim <- function(N) { ...
      # do something random N times
      return(result)
    }
    
    set.seed(112358)                       # set RNG seed for repeatable randomness
    x <- mySim(N)
    set.seed(NULL)                         # reset the RNG
    

    Then you can comment out the lines, or change them to a different seed, or reset the RNG with set.seed(NULL) - everything is explicit.


     

    Don't use save() and load()

     

    This is a corrollary to the principle not to change the global state. save() / load() saves one or more R objects and restores them to the same object name(s) they originally had. But there is no good way to know what that object name is. If you already have an object by that name in your global workspace, it gets overwritten!

    The sane alternative is to use writeRDS() / readRDS(). writeRDS() serializes, compresses and saves a single R object, and readRDS() restores the object and returns it as a return value, thus it can be assigned:

    save(aThing, file="savedAthing.rds")
    myNewThing <- readRDS("savedAthing.rds")
    


     

    Layout

    • Limit yourself to 80 characters per line.
    • Don't write more than one expression on a line.


     

    Design and granularity

    • Don't repeat code. Use functions instead.
    • Don't repeat code. If you feel the urge to type code more than once, that's how you know you should break up the code into functions.
    • Don't repeat code. I'm repeating this for emphasis.


    One of the general principles of writing clear, maintainable code is collocation. This means that information items that can affect each other should be viewable on the same screen. Spolski makes a great argument for this, together with a few excellent examples; he also makes a case for a special kind of prefix notation for variable and function names that has a lot of merit.


    • If the code for a function does not fit on approximately one printed page, you should probably break it up further.
    • if your loops or conditionals are nested more than three levels deep, you should rethink the logic. Actually three is already a lot. Unless you are working on 3D-objects.


     

    Headers

    • Give your script files headers that state purpose, author, date and version information, and note bugs and issues.
    • Give your functions headers that describe purpose, parameters (including required datatypes), and return values. Callers should be able to work with the function without having to read the code.


     

    Sections

    • Use separators (# --- SECTION -----------------) to structure your code.


     

    Parentheses and Braces

    • In mathematical expressions, always use parentheses to define priority explicitly. Never rely on implicit operator priority. (( 1 + 2 ) / 3 ) * 4
    • Always use braces { }, even if you write single-line if statements and loops where they are not legally required.


     

    Spaces

    if and for are language keywords, not functions. Separate the following parenthesis from the keyword with a space.

    Good:

    if (silent) { ...
    

    Bad:

    if(silent) { ...
    


    • Always separate operators and arguments with spaces.[3][4]
    • Never separate function names and their following parentheses with spaces.
    • Always use a space after a comma, and never before a comma. Except in subsetting expressions, where we don't want the comma to hide against the bracket.

    Good:

    print(1 / 3, digits = 10)
    if (! id %in% IDs) { ...'
    expressionProfiles[ , 1:3]
    

    Bad:

    print (1 / 3 ,digits=10)     # space before the comma
    if (!id %in% IDs) { ...      # "!" hides against the parenthesis
    expressionProfiles[, 1:3]    # "," hides against the bracket
    


     

    Names

    There are only two hard things in Computer Science: cache invalidation and naming things.

    - Phil Karlton[5]

    • Use informative and specific filenames for code sources; give them the extension .R
    • Periods have a syntactic meaning in object-oriented classes. I consider their use in normal variables names wrong, even though this is not a syntax error and many R library functions have such names (e.g. Sys.time() and other system calls.)
    • Create names so that related variables or functions are alphabetically sorted together, code autocomplete will be more useful.
    • Use the concise camelCaseStyle for variable names, don't use the confusing.dot.style or the rambling pothole_style[6][7].
    • Don't abbreviate argument names when calling functions. You can, but you shouldn't.
    • Never reassign reserved words[8].
    • Don't use c as a variable name since c() is a function.
    • Don't call your data frames df since df() is a function.[9]
    • Name length should be commensurate with the scope of a variable. Short names for local scope. More explicit names for global scope. I often write global constants[10] in ALL CAPS: (e.g. MAXWIDTH) if they are defined at the top of a code module.
    Specific naming conventions I like
      • isValid, hasNeighbour ... Boolean variables
      • findRange(), getLimits() ... simple function names (verbs!)
      • initializeTable() ... not initTab()
      • Use plurals to good advantage. node ... for one element; nodes ... for more elements - you can then write code like:
    for (node in nodes) { ...
    
      • nPoints ... for number-of
      • iPoints ... for indices-of-points
      • isError ... don't use isNotError: avoid double negation
     

    Conditionals

    This may be controversial. The code block in an if (<condition>) {...} statement is evaluated if <condition> is TRUE. But what if we use a boolean variable in the condition? Should we write:

    if (<boolean variable>) { ...
    

    or

    if (<boolean variable> == TRUE) { ...
    

    It depends. Remember - the goal is to make your code as explicit and readable as possible. If our variable is e.g. a, then ...

    if (a) { ...
    

    ... is not good. Better write ...

    if (a == TRUE) { ...
    

    ... and treat this as any other condition that needs to be evaluated. However - if you have given this a meaningful variable name in the first place, something like ...

    if (recordIsValid) { ...
    

    ... is great, whereas ...

    if (recordIsValid == TRUE) { ...
    

    ... is something that feels oddly self-contradictory. So best practice here depends on context. Myself, I more often than not end up write {{{1}}}, (and that's not because I don't understand how conditionals work).

    Make the FALSE behaviour explicit. Always use an else at the end of a conditional to define what your code does if the condition is not TRUE. Otherwise your reader will wonder whether your code covers all cases. What if your code should do nothing in the FALSE case? Make that explicit:

    if (a > b) {
      tmp <- b
      b <- a
      a <- tmp
    } else {
      ;         # does nothing
    }
    


     

    Indent Style

    No need for much discussion. Follow the One True Bracing Style and we will all be happy. If you don't immediately see why: read about indent style here. (1) Opening brace on the same line as the function or control declaration; (2) closing brace aligned with the declaration; (3) braces mandatory even if there is only one statement to execute. Sample:

    if (length(x) > 1) {
      perm <- sample(x)
    } else if (length(x) > 0) {
      perm <- x
    } else  {
      perm <- NULL
    }
    


    Indentation of long function declarations

    • Use spaces to align repeating parts of code, so errors become easier to spot.


     

    Loops

    Pre-allocate your result objects to have the correct size if at all possible. Growing objects dynamically with c(), cbind(), or rbind() is much, much slower.

    Use seq_along(), not length() to compute the range of index variables. If the object you are iterating over has length zero (i.e. it is NULL, like e.g. the result of a grep() operation if the pattern was not found) then using ...

    for (idx in 1:length(myVector)) { ...
    

    ... will result in an iteration range of 1:0 since length(NULL) is zero, and the loop will be executed twice even though it should not have been. The correct and safe way to iterate is ...

    for (idx in seq_along(myVector)) { ...
    

    ... which will not execute since seq_along(NULL) is NULL.


     

    Functions

    • Explicitly assign values to crucial function arguments, even if you think you know that that value is the language default. For example ...
    sort(x)
    

    ... sorts in increasing order, smallest first. But even though ...

    sort(x, decreasing = FALSE)
    

    ... does the same thing, the expression explicitly tells the reader what it is going to do. And that's good.

    • Always explicitly return values from functions, never rely on the implicit behaviour that returns the last expression. This is not superfluous, it is explicit.
    • If there is nothing to return because the function is invoked for its side effects of writing a file or plotting a graph, write it into your code that nothing will be returned. This prevents you from accidentally returning the result of last expression anyway (as the language does by default), or the reader might think you forgot something. The idiom is:
    return(invisible(NULL))
    
    • In general, return only from the end of the function, not from multiple places.


     

    Efficiency

    If possible, do not grow data structures dynamically, but create the whole structure with "empty" values, then assign values to its elements. This is much faster.

     # This is really bad:
     system.time({
       N <- 100000
       v <- numeric()
       for (i in 1:N) {
           v <- c(v, sqrt(i))
       }
     })
        user  system elapsed
     16.718  11.258  27.988
    
     # Even only writing directly to new elements is much, much better:
     system.time({
       N <- 100000
       v <- numeric()
       for (i in 1:N) {
           v[i] <- sqrt(i)
       }
     })
       user  system elapsed
      0.025   0.003   0.027
    
    # That's abaout as fast as doing the same thing with a vapply() function.
    
     # The fastest way is to preallocate memory, it actually comes close to the
     # vectorized operation:
    
     system.time({
       N <- 100000
       v <- numeric(N)
       for (i in seq_along(v)) {
           v[i] <- sqrt(i)
       }
     })
       user  system elapsed
      0.008   0.000   0.007
    
    
    # Using a vectorized operation is the fastest approach overall and the
    # method of choice:
    
    system.time({ v <- sqrt(1:100000) })
       user  system elapsed
      0.001   0.001   0.002
    
    

    Don't buy into the "apply is good, for-loop is bad" nonsense that you might encounter on the Web. Especially not if you need speed: a well-written for-loop will outperform an apply() statement, which internally uses a for-loop anyway. The reason we often use apply() is because we are following a functional programming idiom, not because there is something magical and exalted about the apply() function. It's usually a bit subtle which idiom is "better" at any given time. But apply() is NOT as trivial to be understood by a python or C programmer as a for-loop is.


     

    # [END]

    • Always end your code with an # [END] comment. This way you can be sure it was copied or saved completely and nothig has been inadvertently omitted. This is important in teamwork. If even ONE team member does not adhere to this, it invalidates the efforts of EVERYONE.


     


    Further reading, links and resources

    Notes

    1. I'm serious: I have reformatted major pieces of code more than once after learning of a better approach, and if that creates better code it is very satisfying.
    2. It is happening more and more frequently that functions in different packages we load have the same name. Then our code's behaviour will depend on the order in which the libraries were loaded. Evil.
    3. Separating operators with spaces is especially important for the assignment operator <-. Consider this: myPreciousData < -2 returns a vector of TRUE and FALSE, depending on whether the values in myPreciousData are less than -2. But myPreciousData<-2 overwrites every single element with the number 2! I'm not even making this up - happened to a student in a workshop I taught.
    4. The = sign is a bit of a special case. When I write e.g. a plot statement, or construct a dataframe, I prefer not to use spaces if the expression ends up all on one line, but to use spaces when the arguments are on separate lines.
    5. For a complementary perspective, see here.
    6. But nevert hesitate to make exceptions if this makes your code more legible.
    7. This is not a random opinion but based on that it's easier to keep within the 80-character line limit. Also see the linked article
    8. In my opinion, base R uses far too many function names that would be useful for variables. But we're not going to change that. So I often just prefix my variable names with my or this, eg myDf, thisLength etc.
    9. Here are more names that may seem attractive as variable names but that are in fact functions in the base R package and thus may cause confusion: all(), args(), attr(), beta(), body(), col(), date(), det(), diag(), diff(), dim(), dir(), dump(), eigen(), file(), files(), gamma(), kappa(), length(), list(), load(), log(), max(), mean(), min(), open(), q(), raw(), row(), sample(), seq(), sub(), summary(), table(), type(), url(), vector(), and version(). I'm sure you get the idea - composite names of the type proposed above in CamelCase are usually safe.
    10. "constants" - i.e. variables that are not supposed to change; R has no "constants" in the actual sense


     


    About ...
     
    Author:

    Boris Steipe <boris.steipe@utoronto.ca>

    Created:

    2017-08-05

    Modified:

    2020-09-18

    Version:

    1.3

    Version history:

    • 1.3 Deprecate save()/load()
    • 1.2 Some practical revisions
    • 1.1.1 Maintenance
    • 1.1 Add: Don't change the global state; Avoid library()
    • 1.0.1 Maintenance
    • 1.0 Completed to first live version
    • 0.1 Material collected from previous tutorial

    CreativeCommonsBy.png This copyrighted material is licensed under a Creative Commons Attribution 4.0 International License. Follow the link to learn more.