How to set a working directory in R for replication packages

Data publisher AEA kicked it all off with this tweet:

“Please stop using ‘cd’ (in Stata) or ‘setwd()’ (in R) everywhere. Once (maybe not really) is enough.

Responses proliferated on #EconTwitter this week. In this blog post, I collect solutions for R. These days, you can share the code used to generate your results for an empirical article. Ideally, this code would be easy for other people to run on their own computer. File paths are tricky (as I blogged before).

A project for a single item can have multiple code files. The code interacts with data stored somewhere. Part of the code’s job is to direct the statistical program to the data set. It’s frustrating when a stranger tries to replicate a result and has to modify the code in multiple places to point to their own location of the data.

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

A suggestion for R code that does not require any package:

𝚌𝚘𝚍𝚎_𝚍𝚒𝚛

𝚍𝚊𝚝𝚊_𝚍𝚒𝚛

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

Some people who responded believe that the “here“The package is worth it.

To sum up, for R users, I saw three main suggestions. Use projects instead of just creating multiple scripts. Use the “here” package. Write your code so that the file path is set only once at the top.

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

This Blog argues that one should always use the “here” package even if one is 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 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 from the AEA account. It is difficult to produce good research. Being held to an ever-changing standard in another skill area is difficult, especially for people who don’t have a search budget to hire.



Source link

Comments are closed.