Site icon R-bloggers

Navigating folders in RStudio: a beginner’s guide

[This article was first published on R – Fabio Marroni's Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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

setwd("C:/Users/marroni/Desktop/tmp/my_work")

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.

To leave a comment for the author, please follow the link and comment on their blog: R – Fabio Marroni's Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.