My Own Style in R

R
code-style
Author

Shu Fai Cheung

Published

September 24, 2022

Although I have written programs since I were a high school student, when computer monitor could only display one color, I have no formal training in programming, and I rarely worked with others in developing a solution until recently. The problem: I did not write with a consistent and professional style. I am pretty sure that my code will look “ugly” to professional programmers.

That said, I do have a loose style, one that suits my own situation:

So, this is my style, with me as the main user and reader:

tmpfct <- function(x) {
    x^2
  }
this_is_long_name <- 1

I don’t like camel case. It is OK for language that is not case sensitive, like Visual Basic and SPSS syntax commands, but is inconvenient for case sensitive languages like R.

# I don't like camel case.
thisIsNotWhatIDo <- 1
# I prefer this:
this_is_what_i_do <- 1
# This is easy to remember
factor_loadings()
# These variants are not
fload()
facload()
facload()
fl()
for (j in 1:10) {
    # Do something
  }
if (x == 1) {
    # Do something
  } else {
    # Do something else
  }
# I may do this:
x  <-   1
y0 <- 100

I also have a GitHub repo for my personal style, in case I forgot the rules:

https://github.com/sfcheung/rstylesf

So, please pardon me if you find my code for packages at odd with professional style. I myself is the main reader and maintainer of the packages. What work for me matters.

:::