getwd()
[1] "/Users/poelstra.1/Library/CloudStorage/Dropbox/mcic/teach/codeclub/codeclub-site/posts/S10E03_reprod_03"
File-related recommendations to improve the reproducibility of your research
Jelmer Poelstra
September 22, 2025
After covering Quarto in the first two sessions, today is the third of a series of Code Club sessions covering several topics under the umbrella of “reproducibility”.
I would like to start by taking a step back to talk about reproducibility in general. What do we mean by reproducibility? Your research is reproducible when third parties are able to perform the same analysis on your data, and produce the same results.
Reproducibility is perhaps a low bar compared to the related concept of replicability, which is the ability to produce the same (qualitative) results when applying the same analysis to different data. Here is a helpful table showing these two and two other related concepts:
For example:
Say that you’ve written a paper in which you present the results of one of your research projects. When this research is fully reproducible, it means that someone else should be able to be able to run the exact same analysis and produce all the results and figures using your paper and its associated documentation.
Relatedly, when you work in a reproducible manner and you abandon an analysis for say two years, you will be able to pick up from where you left off without much trouble.
It is inherently more reproducible to write code, such as in R, than to do analyses by clicking in a program with a Graphical User Interface (GUI). This is because it it is easy to save your code and thereby to document and communicate what you did, but rather tedious to do this when you worked in a GUI.
In addition, R is open source and freely available. If you use a proprietary program that requires an expensive license, your work may be reproducible in principle, but for many people. won’t be in practice.
Research that is fully reproducible should use a set of tools and best-practices related to:
Today, we’ll go over the following four recommendations related to file organization that improve your research projects’ reproducibility.
setwd()
And at the bottom of the page, there is at-home reading material on a fifth recommendation:
Using a self-contained folder, or really a hierarchy of folders, for one project means that you:
For example:
data
folder is contained within the project1
folder,Using a self-contained folder for each projects has many downstream benefits and is fundamental to using other reproducibility-related best practices. It also is more convenient in the long run, as it is easier to find files, harder to accidentally throw away stuff, etc.
Within your project’s directory hierarchy, you should use a consistent subfolder structure to separate different kinds of files – for example, you should separate:
Additionally, you should use plenty of subfolders to separate different kinds of results, and so on. While the specifics can depend a lot on what kind of data you have and how you are analyzing it, here is one good way of organizing a research project folder:
doc
.)To understand the recommendation to use relative paths in your code, let’s start by going over the following terms:
“Directory” (“dir” for short) is another word for folder that is commonly used in coding contexts.
Your “working directory” is the directory where you are currently located. When you start R, it will always have a starting point at a specific location in your computer. You can see what your working directory is with the function getwd()
(short for “get working dir”):
The output of getwd()
is a path, which specifies the location of a file or folder on a computer. In R’s output, folders are separated by forward slashes, as shown above.
Even though R will report paths with forward slashes regardless of the operating system, Windows natively uses backslashes. Additionally, there are multiple root directories on Windows, indicated by letters. Here is an example native Windows file path:
C:\Users\John Doe\Desktop\cats.png
Versus an example Mac (or Linux) file path:
/Users/John Doe/Desktop/cats.png
If you are on Windows, we recommend you specify paths in R with forward slashes, because:
setwd("C:\\Users\\John Doe")
. This is confusing and error-prone!There are two types of paths:
Absolute paths, also referred to as full paths, start from (one of) the computer’s root (top-level) directory. They correctly point to a file or folder regardless of what your working dir is.
If you think of a path as a way to point to a geographic location, then absolute paths are like GPS coordinates.
Relative paths start from a specific working dir, and won’t work if you’re located elsewhere.
Again thinking of a path as a way to point to a geographic location, then relative paths are like directions like “Take the second left”.
project1
folder as the starting point.todo_list.txt
?oneils
, what is the relative path to todo_list.txt
?documents
, what is the relative path to todo_list.txt
?Don’t absolute paths sound better? What could be a disadvantage of them?
Absolute paths:
On the other hand, this does not apply to relative paths that use the root project folder as the working dir. Those paths keep working when moving the folder within and between computers:
OndeDrive
.So, relative paths are preferred, but only when used in the following way:
project1
in the example above)setwd()
If your R working directory is not where you want it to be, you can change it with the setwd()
function. However, there is a better way – letting RStudio Projects set your working dir!
RStudio Projects are an RStudio-specific concept that create a special file, .Rproj
, whose location designates the R working directory when you open that project.
project1
in the diagram above.
Create a new RStudio Project:
File
> New Project
> New Directory
> New project
codeclub-project-practice
as the “Directory name”.After RStudio automatically reloads, the R working directory will be set to the dir in which your RStudio Project file is located. Therefore, you should see the file ending in .Rproj
in the RStudio Files
tab in the lower right pane. Also, you can check your working dir:
In brief, RStudio Projects help you to organize your work and make it more reproducible:
When using Projects, you can avoid manually setting your working directory altogether. To refer to files within the project, you can use relative file paths. This way, even if you move the project directory, or copy it to a different computer, the same paths will still work.
Projects encourage you to organize research projects inside self-contained folder hierarchies exactly as recommended above.
When you switch between Projects, R will restart — and this is a good thing, since you don’t want to randomly carry over objects and loaded packages across research projects.
RStudio Projects are also convenient because:
Let’s recall the aforementioned way you should use relative paths:
setwd()
Here are a few examples of using relative paths in R:
Two key aspects of specifying paths in R:
Occasionally, you may need to access a file that is outside of your project dir (but the less that happens, the better – and this comes down to proper file organization). In that case, you can either use an absolute path, or a relative path that starts by going “up” one or more levels. You can navigate up with the ..
notation, for example:
In this exercise, you are working on a mock research project within the folder that you created above (your RStudio Project folder). You will practice with file organization and using relative paths.
You will be writing to file some mock data, a computed summary of the data, a plot based on the data, and an R script. Create an appropriate dir structure for those files within your RStudio Project dir. It’s up to you how you do this: in the RStudio file panes, in your computer’s file explorer, or with R functions (dir.create)
).
Open a new R script (File
> New File
> R Script
) and save it in an appropriate location within your newly created dir structure. Then, add all the code for the next exercises in that script.
Click here to see a solution
It would be appropriate to save the script in a dir specifically for script, which was called scripts
in the answer above. E.g., you could save it as scripts/exercise.R
.
Load the tidyverse as follows:
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.5.2 ✔ tibble 3.3.0
✔ lubridate 1.9.4 ✔ tidyr 1.3.1
✔ purrr 1.1.0
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Now, you will have access to a dataset (data frame) called diamonds
. Modify the code below to write that data to file in an approriate location.
Run the ggplot()
code below to create a plot of the diamonds data, then modify the ggsave()
line to save your plot to file in an appropriate location.
Run the code below to summarize one aspect of the diamonds data, then modify the write_tsv()
line to save your resulting data frame to file in an appropriate location.
In the RStudio files tab, take a look at the file (structure) you produced.
Here are three principles for good file names (from Jenny Bryan) — good file names:
Consistent and informative naming helps you to programmatically find and process files.
sample032_2016-05-03.txt
One good way to combine machine- and human-readable (opinionated recommendations):
grass-samples
.For example:
mmus001_treatmentA_filtered.tsv
mmus002_treatmentA_filtered.tsv
.
.
mmus086_treatmentA_filtered.tsv
sample005
.YYYY-MM-DD
: 2020-10-11
.DE-01_normalize.R
DE-02_test.R
DE-03_process-significant.R