R tips and tricks – Set Working Directory

This is more an Rstudio tip than an R tip. It would be nice to know how the following works for different editors, but Rstudio is common enough and awesome enough for the following to be relevant.

In Rstudio you can set the default working directory using tools -> Global options -> General -> default working directory . Do that, and next time you open Rstudio your working directory is what you specified there.

My working directory is dropbox, but I always have sub-folders from which I open different scripts.

You can use the following code to automate setting the working directory directly to that folder you open the script from. You can do this using the rstudioapi package:

So when you open a script, does not matter where from, run this to have Rstudio understand which directory you would like to work from.

Take it a step further, create your own setwd function

Now the function set_wd() sets the working directory to the folder you are now working from. So we don’t need the clunkier setwd("paste here the absolute path").

Tech Tip : Now you can work on your code from anywhere with multiple devices using a windows based hosted virtual desktop. You can take a risk-free trial from a Desktop-as-a-Service provider(DaaS) such as CloudDesktopOnline.com. For SharePoint and Exchange hosting you can try Apps4Rent.com.

Leave a Reply

Your email address will not be published. Required fields are marked *