Set working directory for project only

I'm making use of RStudio's projects feature to organize my work. When I create the project I use version control and clone from a repo.

When I clone the repo several directories are pulled into my projects directory. For my analytics work I then drill down into e.g.


Each time I open the project it takes me to the projects home directory which is also the root of the repo. However, I want to place all my work in the path above e.g. rprojects/myproject

I can navigate to myproject in the directory pane, click more > set as working directory and all is fine. Except, I have to do this each time I open the project. Also sometimes I open the project then go to add a file and forget that the file has been created in our_team_directory not our_team_directory/apples/adhoc/rprojects/myproject.

Can I somehow set the working directory of /myproject to always be our_team_directory/apples/adhoc/rprojects/myproject and not default to the root directory of the repo that I cloned?

Referred here by Forecasting: Principles and Practice, by Rob J Hyndman and George Athanasopoulos

It sounds like you maybe aren't quite using Rproj as intended if you find that is isn't actually located at the root dir of the project you are working on. It sounds like the Rproj file should be located in the myproject directory (and also there should be one in all other directories contained in rprojects), not the our_team_directory directory. Unless I am misinterpreting your description.

I use Rproj fairly liberally. Every little project is its own Rproj. This makes it so I can move these smaller project directories around my machine (and even to other machines) without breaking code. This is made especially seamless due to the here package.

Basically, if you feel like every time you open a Rproj you want to change the working directory to somewhere else, that somewhere else is probably where the project should be defined. Does that make sense?

Hi @mattwarkentin. I think it's because when I create the project and use the version control setting, I have no option to define which directory in the repo the project will be in. In fact, after cloning the repo into the project I like to create a new directory to house my ad hoc analysis.

When I did some searching it sounded like the internet was generally opposed to creating multiple projects nested within projects. Is that what you were suggesting in different words?

Do you think I should just mv the rproj file to my desired project directory?

Yes, I agree, you should not nest projects inside projects. To me it sounds like the higher level directories aren't projects (in the Rproj sense), but rather just directories for groups of projects.

One possibility is to delete the high-level Rproj, like you said, and start initializing lower-level projects contained in rprojects with Rproj files. The usethis::create_project() function can be helpful for this.

Or, you could use the here::here() function in scripts to point your script from the Rproj directory to the desired working directory. For example:


dr_here() # this should show you where root dir starts

# This should return an absolute path to your specific project
here('apples', 'adhoc', 'rprojects', 'myproject')

For a nice discussion of RStudio Projects, project workflows generally, and the here::here package, check out this article by Jenny Bryan.

I like to keep all my reporting in a project sub-directory, separate from other /r/ helping files, and end up using here::here a lot.

Thanks for the comments Curtis and Matt. Just for my interest I tried using the linux mv command like so:

mv myproject.Rproj adhoc/myproject/

I then closed the project and tried to reopen. When I tried to reopen I received an error popup notification that the project had been moved. So, I navigated to adhoc/myproject and double clicked the .Rproj file and the project opened.

So this seems to be working now, it's just that my project doesn't appear in my initial list of projects when I select 'open project'. Have not encountered any bother side effects yet, but will see.

I am still a little confused with your directory structure and ultimate goal, but as general advice I tend to avoid moving Rproj files to different directories if at all possible. Instead, I lean towards deleting old Rproj files and creating an entirely new one in the new location.

Hi Matt.

"I am still a little confused with your directory structure and ultimate goal"

Let me try and describe the issue again. When I create a new project within the RStudio GUI I have an option to use version control. If I select that option and type in a repo address, RStudio creates the project and pulls the contents of that Github repo into the new project.

Since I'm going to be pushing my code to the repo where others are working, I just want to add my work all the way down a nested directory for the analytics team.

So, by creating the project with version control, the default is that the root directory of the project is the root directory of the repo, as opposed to my personal analytics directory nested within repo.

Am I making sense?

1 Like

Ah yes, this definitely makes sense. I'm thinking you should use the here package like I described above and avoid touching the Rproj file at all.

You could also perhaps clone the repo to your computer (outside of RStudio), and then open RStudio and use usethis::create_project() to initialize a Rproj somewhere nested inside of the repo (like in the myprojects directory), but I don't know if this is bad form. Rproj files are really meant to be in the root dir. It doesn't feel right to me.

Maybe we can conjure @jennybryan to come in here and settle this one.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.