Alternatives to setwd

Hi everyone,

I am looking for an alternative for setwd. I know that there is one but I cannot seem to find it. :wink:
I am working on an R script in Markdown which other people want to access from their computer, too. So instead of always changing the Working Directory by changing the path in setwd() on their computer, how can I implement something that works for everyone from everywhere?

Thanks!

I'm not sure what's the exact problem with setwd provided that you are using relative paths, but you can take a look at here package - https://cran.r-project.org/web/packages/here/index.html.

3 Likes

I agree that here is the way to go. For an informative read, check out Jenny Bryan's blog post that touches on it.

1 Like

I'll double down on @mishabalyasin and @kmprioli's advise to check out here, and also suggest using RStudio's project functionality. The only time I find myself needing to use here inside an R project created with RStudio is when I am using an RMarkdown file that is saved in a subdirectory:

| data/
|    data.csv
| docs/
|    notebook.Rmd
| results/
| .gitignore
| LICENSE
| README
| sample.Rproj

In this case, you need here to open data.csv because the RMarkdown file will treat the docs/ subdirectory as the working directory. Using syntax like readr::read_csv(here("data", "data.csv")) makes it possible to point your notebook to the correct data/ subdirectory!

2 Likes