How to set up a reproducible and collaborative workflow
Code smells, projects and scaffolding
Important
This does not reset the workspace: all it does is delete user-created objects from the global workspace.
• Any packages that have ever been attached via library()
are still available.
• Any options that have been set to non-default values remain that way.
• Working directory is not affected.
Restart R Session
(shift + cmd + 0
).• When you quit R, do not save the workspace to an .Rdata
file.
• When you launch, do not reload the workspace from an .Rdata
file.
• In RStudio, set this via Preferences > Options > General.
setwd("/Users/jenny/cuddly_broccoli/verbose_funicular/foofy/data")
setwd()
command having the desired effect – making the file paths work – for anyone besides its author is 0%.Projects
• Create a project with the top-left icon next to New file
, or from the Command Palette.
• Open and manage Projects
from the top-right drop-down menu.
• You can also open projects by double clicking on the *.Rproj
file in your filesystem.
{here}
df
.“There are only two hard things in Computer Science: cache invalidation and naming things.”
Filenames, commands and documentation
Warning
Avoid /
and \
for file names especially!
_
to delimit words.-
to delimit meta-data fields.Example
2022_11_05-lecture_01-r_best_practices
Example
01-helper-data-loading.R
02-helper-data-visualisation.R
03-helper-ml-model_tuning.R
.
├── .Rproj.user
├── data
│ ├── external # external data that does not belong to raw
│ ├── interim # intermediate manipulations
│ ├── processed # final data used for analysis/models
│ └── raw # raw data should never change!
├── reports # notebooks with analysis and exploration
├── src # contains the source code, also named `R`
├── your-proj.Rproj # ❗Project file
└── README.md # info about the project
cmd + shift + P
calls the command palette: from there, you can call any command.vignettes
:A vignette is like a book chapter or an academic paper: it can describe the problem that your package is designed to solve, and then show the reader how to solve it.
browseVignettes()
and view one with vignette('your-vignette')
.Use {renv}
to create and manage virtual environments
{renv}
: install.packages('renv')
Project
.renv::init()
: this will initialise a new environment
renv/
folder, as well as a renv.lock
(which contains the details about every library you use).install.packages()
(which is actually calling renv::install()
under the hood).{lintr}
as a static analysis tool:It checks for adherence to a given style, identifying syntax errors and possible semantic issues, then reports them to you so you can take action.
{styler}
git
(you should) and GitHub, you can use GitHub Actions to build a continuous integration (CI) pipeline:{usethis}
package.usethis::use_github_action('lint')
.usethis::use_github_action('style')
.{lintr}
and {styler}
, inside .github/workflows
.UniMi • Coding for Data Science and Data Management • AY 2022/2023