In this brief tutorial we will discuss how to navigate through folders in RStudio.
When you open RStudio, the software will also open an instance of R. As any other software, when opened R is set to read and write files in a default location, which much likely is not the location in which you want to save your data.
In my case, the default folder is my Documents directory.
Let’s now assume dat you have to read the file named iris.csv, that is located in the folder shown below.
You have to change folder. It is pretty easy: Session -> Set Working Directory -> Choose Directory (note the shortcu, if you are a shortcut fan).
You can then navigate to your folder (e.g. my work) easily, and click on Open
After this, you will get notified of the directory change, and you can finally, easily read your file.
I get the chance for a slightly more advanced thing. If you set your directory to “my_work” as I did, and your file is in “my_work/data”, you have to read the file “data/iris.csv”. If you try to read the file “iris.csv” without specifying the directory you will get an error.
The more careful readers may have noticed that, after changing the folder, and without any typing on our side, the text
appeared. Well, this is how you change directory in R, and I suggest you get acquainted to that. It is very easy. In Windows it is simply: setwd(“D:/folder/that/you/want”) where D is the name of the drive.
You may ask: do I REALLY have to type the “data” folder name to read my iris.csv? Well, this is not necessary. You can navigate to the “data” folder and then simply type the file name, as I show below:
Well. This is all. Setting working directories is a very important task in R, and you can find many tutorials (all better than mine!) on the internet. I suggest you take your time to learn and become confident in setting working directory in R.