4 followers Follow

can user obtain the path of current Project's directory?

Is there any way to access the path associated with the current RStudio project? Specifically, I'd like a function similar to getwd() but that returns the path to the directory where myProject.Rproj lives. This is also related to how system.file() can be used to get the full path of a file inside a package.

The goal is to define a Project-wide notion of root directory that would be useful for constructing paths in R scripts and R markdown files. Keeping all the data, R source, figures, output, etc. in the top-level directory is not an option for large projects. But knitr, "Compile Notebook", and "Knit HTML" have a strong expectation that working directory is set to wherever that file lives. That is tolerable during development. But long-term, this can become a hindrance. I would love to be able to set working directory and/or build paths relative to the Project directory.

I realize I could achieve some of this with a Project-level .Rprofile but worry it wouldn't be portable to someone else's machine if we are collaborating via Git, for example.

Jenny Bryan

Official comment


Hi Jennifer,

This currently isn't a function we have available, but its definitely a good idea. I've written it up as a feature request and we'll see if we can implement it going forward.

Thanks a lot,

Ian Pylvainen



Thanks. Earlier I requested a button to snap RStudio's file browser back to the Project's top-level directory. Would seem related to me? So these two feature requests make a nice pair!

Jenny Bryan 0 votes

script.dir <- dirname(sys.frame(1)$ofile)

return script's full path

script must be sourced in order to return correct path

Rich Gillin 0 votes

There is a specialized R package called rprojroot.
Function `rprojroot::find_rstudio_root_file()` may be helpful.

Vilmantas Gegzna 1 vote