Using the
setwd()
command in your R scripts helps you to set the working directory. This makes it easier for you to refer to describe where your files (e.g., another R script or a CSV data file) are located on your computer. But it also comes with many disadvantages, mainly that it has the side effect that this makes the code only work on your computer because it depends on the folder structure and names on your computer. A more detailed summary of the problems are described in the section 'What’s wrong withsetwd()
?' in this blog post. A better alternative is to organise each project (e.g., all analyses for a specific research project) into a folder on your computer. This folder is the top-level directory of your project, for example the folderphc-coding-club
is the name of top-level directory of this project. Each path used in scripts in your project should be relative to this working directory – thehere
package will help you with that.
- Solution 1: Use the
here
package to replacesetwd()
"Good coding style is like correct punctuation: you can manage without it, butitsuremakesthingseasiertoread." This quote comes from The tidyverse style guide and describes the main motivation behind code style.
- Solution 2: Check the code style using
lintr
- Run:
install.packages("lintr")
- Run:
lintr::lint_dir(".")
- Run:
- Solution 3: Fix the code style using
styler
- Run:
install.packages("styler")
- Run:
styler::style_dir(".")
- Code changes
- Run:
If you intend to make your code publicly available (e.g., on GitHub) it is a good idea to add a licence that specifies how you want your code to be used by someone else. More on this can be found in the book R Packages (2e) - Licensing.
- Solution 4: Add MIT licence to this project repository
- Run:
install.packages("usethis")
- Run:
usethis::use_mit_license()
- Code changes
- Run:
There is at least one more problematic coding practice in this project. More details of the problem can be found in the section 'What’s wrong with
rm(list = ls())
?' in this blog post.
- Solution 5: Improve anything else that suggests a sub-optimal workflow
- Remove
rm(list = ls())
fromscripts/load_data.R
and make sure you follow advice in R for Data Science (2e) - Workflow: projects - Code changes
- Remove