Understanding and setting RStudio working directory when sharing projects in collaboration

It took me a while really to understand working directory since I am not a total “coding” person. For years I have been spoiled by clicking folder icons. How I am supposed to understand the difference between “~/bla/bla/bla” and “./bla/bla/bla”… I still have to work in IDE and going to Terminal still bring hesitation.

If you are same confused, continue to read.

First, we assume we want to have an R project. In the meanwhile, we have several folders to deal with: data folder, figure folder and manuscript folder. Then, we assume again, it is a big house. It has a living room. From living room, we can see there are three individual rooms but doors are closed. Can’t see through so no idea what’s inside.

Let’s assume R project is that house, hosting three folders as those three rooms. So once an R project is built and opened, the fold where it is located is default working directory, like the living room. Once we want to get whatever inside the three individual folders, we have to open the “door” and go inside.

“~” means your computer home directory. This is universal for different operation system. That is why it is better to use this instead of disk sign like “C:\” which is not understandable outside of Windows.

“./” means where you are. Once you double clikc an R project icon, “./” means the folder’s location. You can unerstand “./” as the big house. Using this is convinient so you don’t have to type the long location path. But it is import you are clear-minded where you are.

Now we want to go to room “data”. Keeping standing in living room disable us to see anything inside “data” room so we have to open the door which means changing directory.

Changing working directory is not in the top list of favorite actions. Sometimes it brings confusion and can be a reason others can source your script. Of course, one can always define the path for file’s name but sometimes it is annoying.

What I explain following is only one way if you have a huge load of files to read from some subfolder and you don’t want to use loop.

So now let’s assume I build a project in “plant_UV” folder. I want to set this folder as my root folder in the beginning. (All the lines starting with “#” are R codes and you delete it for running it.)

Now I double click my project. We firstly name the living room as root_wd:

# root_wd <- getwd()

Now we open the door to “data” room.

# setwd(“./data”)

We are in “data” room now. This only works when we are in living room (where .proj locates). If we go out from the house to the yard (for example, wd has been set to another folder outside of folder “plant_UV”, we have a gap between current wd and data because “./” means another folder now.

We can go a subfold within “data” folder.

# setwd(“./spring”)

Again, this only works when we are in “data” room already. What we should do if we want to go directly to “spring” folder if we are in living room yet?

# setwd(“./data/spring”)

See, “./” means where you are.

#setwd(root_wd)

Now we come back to living room/the whole house.

If you change the order of these lines, it won’t work anymore.

Advertisement

One thought on “Understanding and setting RStudio working directory when sharing projects in collaboration

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s