In 2025, I want to post more regularly (aka more than once a year). I thought one way to is to start a series1 of things I have learnt in R over the last month. Most of this will be a sort of bookmark note for myself, but if you get some use out of it - let me know
1 The mysterious ‘@’ symbol in R and the difference between S3 and S4 objects
I was reading some code my supervisor had worked on for a paper from several years ago. Famously he is very keen on base R and to write your own code yourself instead of using packages functions, which I appreciate and agree with to some extent, however I do like the tidyverse. Anyway, in it he was using the @
…. so what does it do?
1.1 Extractor operators: $
and @
According to documentation, the @
symbol is to extract or replace a slot or property in S4 objects whilst the $
symbol is to extract or replace parts of an object. I feel I’ve used the $
a lot to select columns from a dataframe/tibble before, but I’ve yet to use the @
… which leads me to…
1.2 What is an S3 and S4 Object and how do they differ?
An S3 object are anything with a class attached to it. There is no enforcement of a structure or type. They are “R’s first and simplest [Object-orientated] system”. This is the most common form of object e.g. list, dataframe etc.
Whilst an S4 object has clearly defined “slots” or properties. They state what slots should exist and what types they are. Use the @
to access things with a predefined class. Few packages use S4 objects, but if you want to make sure that a code outputs in a very very particular way they can be used and set up. I think that’s why they were in my supervisor’s code - proto-type to a package.
2 Reformating and tidying code in RStudio
First is using cmd + shift + A
and the second is the Styler plugin in R (which formats in the tidyverse style guide).
So the same code my supervisor sent me was a bit janky laid out, in my opinion. And I usually end up adding new lines, indenting and formatting and adding white spaces etc to the code. Ugh.
HOWEVER, you can get plug-ins in RStudio which do magic things such as indenting and adding whitespaces. Like Styler.
Tools > Addins > Browse Addins
I’m still exploring this. Maybe one day I’ll get round to trying to make one which allows colour customisation of RStudio themes would be neat.
3 Rainbow brackets
I love this. It is such as simple trick and makes it much much easier to debug the classic “Am I missing a bracket?”
Tools > Global Options > Code > Display
and at the bottom is rainbow brackets. Enjoy!
3.1 Collapsable code chunks
Like writing R scripts? if you sandwich code in { }
it collapses the code. Use at will.
3.2 Equatiomatic
Takes a model and turns them into LaTeX equations.
So mod1 <- lm(mpg ~ cyl + disp, mtcars)
would go to:
3.3 ggThemeAssit - an assistant for speedy ggplot themeing
Another RStudio plugin, but makes editing themes of ggplot, much much faster. See below… (gif does run for quite sometime). It hasn’t been updated in sometime so caution with forward compatibility!
4 Deep dive into what makes up a function
I’ve used body()
function before to look at what is in a function - how it was coded in a package. I often use this to work out, when documentation is not clear to why the function is not behaviouring as I expect, or how to adapt or write my own code. I recently discovered another way is to take package:::function
also works to look at the source code!
5 References
Advanced R by Hadley Wickham - S3 and S4 objects
Footnotes
We will see how long this lasts - but I will try.↩︎