[10 minute discussion]
Use automatic formatting to do some automatic cleaning of your code
Helps with many things, but not a magic bullet..
Tip
You can automatically style code and entire scripts using the {styler} package.
This is especially handy for using .rmd or .qmd files. Here, you can simply specify the tidier as a knitr chunk option.
Note that this doesn’t affect the source document (i.e. the script), but only affects the knitted document.
Use one system and stick to it. This will help not only with readibility, but also writing code to for example select key variables of interest.
I like the following:
clean_names()
function from the {janitor} package[1] "bad_name" "really_bad_name" "x1_another_bad_name"
Go back to an old script (e.g. for data cleaning, …) of yours (preferably older than 6 months) and take a look at it
General logic: all data cleaning / manipulation first, then analyses
For more complex projects, keep data cleaning and analysis in separate scripts.
Dealing with name conflicts
The {conflicted} package helps navigate name conflicts of functions from different packages (functions having the same name, such as base::filter()
and dplyr::filter()
)
The conflicted::conflict_prefer()
function lets you set defaults for which function you prefer in this case.
If you deal with packages that have naming conflicts, loading the {conflicted} package at the start of your R scripts is a good idea.