[1] FALSE
[1] TRUE
RaukR 2024 β’ Advanced R for Bioinformatics
Marcin Kierczak, Sebastian DiLorenzo
21-Jun-2024
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 π·
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 (.
), if .
should 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_this
camelNotationLooksLikeThis
period.notation.looks.like.this
But many also useβ¦
LousyNotation_looks.likeThis
Try 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. fsjht45jkhsdf4
weight
vs. phenotype.weight.measured
There 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 β
a <<- 42
new_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
Source: Stackoverflow
%>%
pipes sake:
myfun <- function(x, arg)
πmyfun <- function(arg, x)
π
myfun <- function(x, seed = 42)
πmyfun <- function(x, ...)
π―options
If you are re-using functions written by someone else β write a wrapper function around them
...
source: http://www.xkcd/com/292
source: https://github.com/edu/students
_
platform aarch64-apple-darwin20
os darwin20
major 4
minor 4.0
2024 β’ SciLifeLab β’ NBIS β’ RaukR