Best Practices
Last updated: January 18, 2021
These are my current best practices for working in R:
- Prefer
tidyverse
methods and idioms when there are multiple ways of coding something. - Follow the tidyverse coding style guide.
- You can use
lintr
to automatically flag code that doesn’t conform to the style guide.
- You can use
- Use
renv
for dependency management. - Use RStudio projects to make it easy to re-open a given project.
- Use the
here
package rather than usingsetwd()
to define a working directory. - Use tidyverse pipes whenever possible, as opposed to
lots(of(nested(functions())))
. Prefer writing in R Markdown notebook to vanilla R scripts, unless there is a good reason why R Markdown doesn’t make sense.
- Use the
remedy
add-in to RStudio to help with writing Markdown efficiently. Use these metadata settings for RMarkdown:
title: "Some title" output: html_document: df_print: paged toc: yes toc_float: collapsed: false smooth_scroll: false number_sections: true theme: yeti html_notebook: toc: yes
- Use the
For plots, use the BBC’s package on top of ggplot2 as a starting point.
Other Resources
âšī¸ This page is part of my knowledge base for R, the popular statistical programming language. I attempt to use idiomatic practices with the tidyverse
collection of packages as much as possible. If you have suggestions for ways to improve this code, please contact me or use the survey link below..