[1] FALSE
[1] TRUE
RaukR 2023 β’ Advanced R for Bioinformatics
27-Jun-2023
After this module:
Style β _howTo_style.yourCode?
Structure β how to think π€ about the code and manufacture your own building π§ blocks
Debugging β my code does not run π
Profiling β now it does run butβ¦ out of memory π£
Optimization β making things better π·ββοΈ
Vectorization β more details on optimization via vectorization βοΈ
Parallelization β run things in parallel, rule them all! π
Naming conventions β assigning names to variables
Code formatting β placement of braces, use of white space characters etc.

A syntactically valid name:
Consists of:
._Begins with a letter or the period (.) not followed by a number
Cannot be one of the reserved words: if, else, repeat, while, function, for, in, next, break, TRUE, FALSE, NULL, Inf, NaN, NA, NA_integer_, NA_real_, NA_complex_, NA_character_
Also cannot be: c, q, t, C, D, I as they are reserved function names.
Variable names that are legal are not necessarily a good style and they may be dangerous π:
do not do this!
unless you are a politician π΄β¦
Avoid T and F as variable names.
Also, there is a number of variable names that are traditionally used to name particular variables:
usr β userpwd β passwordx, y, z β vectorsw β weightsf, g β functionsn β number of rowsp β number of columnsi, j, k β indexesdf β data framecnt β counterM, N, W β matricestmp β temporary variablesSometimes these are domain-specific:
p, q β allele frequencies in genetics,N, k β number of trials and number of successes in statsTry to avoid using these in this way to avoid possible confusion.
People use different notation styles throughout their code:
snake_notation_looks_like_thiscamelNotationLooksLikeThisperiod.notation.looks.like.thisBut many also useβ¦
LousyNotation_looks.likeThisTry to be consistent and stick to one of them. Bear in mind period.notation is used by S3 classes to create generic functions, e.g. plot.my.object. A good-enough reason to avoid it?
It is also important to maintain code readability by having your variable names:
genotypes vs. fsjht45jkhsdf4weight vs. phenotype.weight.measuredThere are built-in variable names:
LETTERS: the 26 upper-case letters of the Roman alphabetletters: the 26 lower-case letters of the Roman alphabetmonth.abb: the three-letter abbreviations for the English month namesmonth.name: the English names for the months of the yearpi: the ratio of the circumference of a circle to its diameterVariable names beginning with period are hidden: .my_secret_variable π» will not be shown but can be accessed
Decompose the problem π§© π§©!

source: Wikimedia Commons
when should I write a function β
consider creating an S4 or even an R6 class β data-type safety!
a <<- 42new_counter <- function() {
i <- 0
function() {
# do something useful, then ...
i <<- i + 1
i
}
}
counter1 <- new_counter(); counter2 <- new_counter()
counter1(); counter1(); counter2()[1] 1
[1] 2
[1] 1
based on Stackoverflow answer
%>% pipes sake:
myfun <- function(x, arg) πmyfun <- function(arg, x) βmyfun <- function(x, seed = 42) πmyfun <- function(x, ...) π
ββοΈoptionsIf you are re-using functions written by someone else β write a wrapper function around them
...
source: http://www.xkcd/com/292
_
platform x86_64-pc-linux-gnu
os linux-gnu
major 4
minor 2.3
2023 β’ SciLifeLab β’ NBIS β’ RaukR