How to Set Working Directory in R for Replication Packages

The AEA Data Editor kicked it all off with this tweet:

“Please stop using “cd” (in Stata) or “setwd()” (in R) all over the place. Once (maybe, not really), that’s enough.”

Replies proliferated on #EconTwitter this week. In this blog post I am collecting solutions for R.   These days you might share the code used to generate your results for an empirical paper. That code would ideally be easy for other people to run on their own computers. File paths are hard (as I blogged previously).

A project for a single paper might have multiple code files. The code interacts with data stored somewhere. Part of the task of the code is to point the statistical program to the data set. It is frustrating if an outsider is trying to replicate a result and must alter the code in multiple places to point to their own location of the data.

Here is a concise summary of good practice, for any code language: “cd and setwd() specify the directory. When you share code and run on a different computer, they don’t work. Therefore, good practice to only specify once, at the beginning”

A suggestion for R code that requires no packages:

𝚌𝚘𝚍𝚎_𝚍𝚒𝚛 <- 𝚐𝚎𝚝𝚠𝚍()

𝚍𝚊𝚝𝚊_𝚍𝚒𝚛 <- 𝚐𝚜𝚞𝚋(‘/𝚌𝚘𝚍𝚎$’, ‘/𝚍𝚊𝚝𝚊’, 𝚌𝚘𝚍𝚎_𝚍𝚒𝚛) 𝚏𝚒𝚐𝚜_𝚍𝚒𝚛 <- 𝚐𝚜𝚞𝚋(‘/𝚌𝚘𝚍𝚎$’, ‘/𝚏𝚒𝚐𝚞𝚛𝚎𝚜’, 𝚌𝚘𝚍𝚎_𝚍𝚒𝚛)

𝚜𝚎𝚝𝚠𝚍(𝚍𝚊𝚝𝚊_𝚍𝚒𝚛)

Some people who responded believe the “here” package is worthwhile.

To summarize, for R users, I saw three main suggestions. Use Projects instead of just making multiple scripts. Use the “here” package. Write your code in a way that only requires the file path to be set once at the top.

The way to do “relative paths” in R: You could change the working directory. Get the address in the beginning getwd(), replace it by your project folder with setwd(). Then, when accessing a file just use read.table("./folder/file.R")

This blog argues that one should still use the “here” package even if doing a Project.

For STATA purposes, this seems worth mentioning https://worldbank.github.io/dime-data-handbook/coding.html#writing-file-paths Also if you want Stata or Python resources, you can click around the Tweet threads.

And another free book, for R users, https://rstats.wtf/project-oriented-workflow.html

More than one person responded negatively on Twitter to the initial statement by the AEA account. It’s hard to produce good research. Being expected to conform to an ever-changing standard in another field of competence is difficult, especially for people who don’t have a research budget for hiring.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s