Skip to main content
v2026.11,986 entries · CC-BY 4.0

Change Working Directory in R: setwd, getwd and Project Paths

How to change the working directory in R with setwd() and getwd(), restore it afterwards, and avoid hard-coded paths with RStudio projects and here().

Written and maintained by CASRAI Editorial Board

Published · Last updated

On this page

To change the working directory in R, call setwd("path/to/folder"); to check where you are first, call getwd(). Both are base R functions documented on the same manual page. For analysis you intend to share or rerun, the more durable pattern is to avoid hard-coded setwd() calls altogether: open an RStudio project, which sets the working directory for you, and build file paths with the here package.

What the working directory is used for in research

The working directory is the folder R resolves relative file paths against. When a script reads "data/survey.csv" or writes "output/model.rds", R looks for that path starting from the working directory. If the directory is not the one the script’s author assumed, reads fail or output lands in the wrong place. That is why the working directory matters for reproducible research: a script that only runs after someone edits a path on line 1 is not reproducible by a collaborator, a reviewer or your future self. See reproducibility for the wider definition, and Reading CSV Into R for the file-reading side of the same problem. The same project-folder discipline applies in other tools, as in Stata do-file structure for a reproducible workflow, and keeping the whole project folder under version control is covered in the GitHub tutorial for beginners.

Install or access R

getwd() and setwd() ship with base R, so there is nothing extra to install. R itself is distributed from the Comprehensive R Archive Network (CRAN) at cran.r-project.org, which hosts the installers for each operating system. RStudio, if you use it, is documented at docs.posit.co; the here package is documented at here.r-lib.org.

Step by step: how to change the working directory in R

Step 1. Check the current directory with getwd()

Run getwd(). The R manual describes the value as a character string, or NULL if the working directory is not available. On Windows the path is returned with forward slashes, in UTF-8, and without a trailing slash unless it is the root of a drive.

getwd()

Step 2. Change it with setwd()

Pass the folder as a character string. The manual states that tilde expansion is applied to the argument, so a leading ~ is expanded to your home directory.

setwd("~/projects/trial-analysis")

Step 3. Restore the previous directory

setwd() returns the directory you were in before the change, invisibly. Store that value and pass it back to setwd() to return to where you started.

old <- setwd("~/projects/trial-analysis")
# ... read and write files here ...
setwd(old)

Step 4. Let an RStudio project set it for you

Posit’s documentation states that RStudio projects are associated with R working directories, and that when a project is opened, the current working directory is set to the project directory. Opening the project’s .Rproj file therefore replaces the setwd() call, and the same script works on any machine that has the project folder.

Step 5. Change it from the Files pane

The RStudio Files pane has a More drop-down that, per the Posit documentation, includes an option to modify or navigate to the working directory. Use it for a one-off change in an interactive session; it leaves no record in your script, so do not rely on it for work you need to repeat.

Step 6. Build paths from the project root with here()

The here package is designed for file referencing in project-oriented workflows; its documentation contrasts it with setwd(). It uses the top-level directory of a project to build paths to files, for example:

library(here)
read.csv(here("inst", "demo-project", "data", "penguins.csv"))

Calling here::i_am("README.Rmd") prints here() starts at followed by the path it has taken as the project root, which is a quick way to confirm where paths will resolve. To pin the R packages those scripts use, see renv: Lock R Package Versions.

Output and how to read it

getwd() prints the current directory as a quoted string, for example [1] "C:/Users/you/projects/trial-analysis". setwd() prints nothing, because its return value (the previous directory) is invisible. To see that value, assign it or wrap the call in print(). The reliable confirmation that a change worked is to call getwd() again.

Troubleshooting

setwd() stops with an error

The manual states that setwd() gives an error if it does not succeed. A script halts at that line, so the usual cause to check is the path string itself: a typo, a folder that is not where you think it is, or a path that is relative to a directory you are not in. Run getwd() to see where R is starting from.

The path works with ~ in one place and not another

Tilde expansion is documented for the dir argument of setwd(). Check what your home directory resolves to on that machine before sharing a script that depends on it.

getwd() returns NULL

The manual says getwd() returns NULL if the working directory is not available. Set a directory explicitly or restart the R session in a known folder.

The directory differs after changing and returning

According to the manual, symbolic links can make the value from getwd() differ after you change directories and return. If you compare paths as strings, expect this on systems that use symbolic links.

Windows paths

R reports Windows paths with forward slashes, so write paths the same way when you pass them to setwd().

Free alternatives to calling setwd() by hand

Approach What it does Source
setwd(dir) / getwd() Changes or reports the working directory in base R; returns the previous directory invisibly. R base manual
RStudio project Sets the working directory to the project directory when the project is opened. Posit RStudio documentation
RStudio Files pane More drop-down includes an option to modify or navigate to the working directory. Posit RStudio documentation
here::here() Builds paths relative to the project’s top-level directory, instead of changing the working directory. here package documentation

Frequently asked questions

How do I change the working directory in R?

Call setwd() with the folder path as a string, for example setwd("~/projects/trial-analysis"), then confirm with getwd(). In RStudio, opening a project sets the working directory to the project folder.

How do I change the working directory in R without setwd()?

Open an RStudio project, or keep the working directory as is and build file paths with here(). The Files pane’s More menu also offers a way to modify or navigate to the working directory.

How do I change the working directory in R and go back afterwards?

Save the invisible return value: old <- setwd(dir), then call setwd(old) when you are done.

Why does getwd() show a different folder from the one I expected?

R resolves relative paths from wherever the session started or was last changed. Check with getwd(), and see the troubleshooting section for the cases the manual documents.

Sources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Change Working Directory in R: setwd, getwd and Project Paths

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →