Time commitment
2 - 5 minutes
Description
Using RStudio to open data video 2 of 2: opening .csv and .xlsx files.
Video
Transcript
What we're going to do is move on to the first bit of actual coding you might do. If you've never coded before and you have R or RStudio on your computer and you want to follow along, feel free to open it. If you just want to listen because you're just trying to figure out how to do stuff and you're a little bit nervous, that's okay, you can just listen. So how do we set a working directory? What does that even mean? One of the trickiest aspects of working with R or RStudio if you've never done it before, maybe you did it once for class a couple years ago and you don't really remember what you were doing, the trick is remembering to set your working directory, which is just a fancy way of saying “I want to work on a specific location on my computer”. Let's say a lot of folks use R for data analysis, so let's say you wanted to use a file that you've stored on your computer, an Excel file or a .csv file, you have to be able to tell RStudio where that file is on your computer. That's what we mean when we say set working directory.
How do you do this? You want to tell the computer where your file is, and you want to put that file somewhere that you're going to be able to find it. So I generally recommend: don't keep the files somewhere like your downloads, generally, you don't even want to keep them on your desktop. You generally want a file folder somewhere where you're like: “This is where I'm going to put my file, my code, any analysis stuff that I do”. I want it in the same spot that's easy to remember and easy to find. Generally not downloads, because that can change. Generally not the desktop, because if you move it, everything breaks.
Okay, so working directory. Where's the file? You want to tell the computer where you're working. There are so many ways to do anything in RStudio; if you've taken a course before, even if you've done one workshop, they might have shown you one way. There's so many ways to do this. The way that I recommend is using the point-and-click buttons at the top. So if I have our script open for today, I would click the “Session” [tab], I would click “Set Working Directory”, and I would click “Choose Directory”. I recommend doing it this way because it's really easy, it's really familiar, because what this does is if I click this button it pops open my File Explorer., the way I would normally look through my own files on my computer. It opens this window and I can say – your file extension is hopefully shorter than mine – I’m in my documents, classes and workshops, Lindsay's workshops, micro workshop,
RStudio. I know this is the correct location. So yours is probably not quite this in-depth, you might just be working on your desktop, but you find the location of where you've stored the files for today's class, and even if it doesn't show you anything, if you know you're in the right spot, you say “Open”. I know I'm in the right spot because if I click this files window in my bottom right corner here, I can see all of the workshops that I've got so far; I'm in the right spot, I've got my intro class (which is today), I've got my fake data set, I've got the rest of the classes for the rest of the semester, so I'm in the right location. If you don't see your files in the files window, you either forgot to set your working directory, or it somehow is set to the wrong location. So you could do the same thing again, you could say Session > Set Working Directory > Choose Directory and try to find where your files are.
So I said this is probably one of the trickiest things about using R and RStudio. The reason is: people forget this step. You might do it with me in the workshop right now, or maybe you've done it in a class before, and you like how you did it before, so you've got your information ready to go. But let's say you open this tonight, or next week, or next month or next year…if you forget to set the working directory and you try to open a file, it will not work. The trick here is even though we clicked Session > Set Working Directory > Choose Directory…we used buttons to do this, the computer still used code. And it gives you the code in the bottom left, in your console. Mine's in blue text. Yours is probably in blue text if you're on a newer-ish version of R. Blue text is generally good; blue text means it worked. If you have red text, it generally means that's an error code and it didn't work for some reason, or something broke. So I have blue text, which means it worked, and let me read what it says. It says: setwd (which is shorthand for “set working directory”). So setwd left bracket so we've got some text and then round brackets [setwd()]. Any time you have text and round brackets, you have used a function. So setwd() is a function with those brackets, saying set the working directory to this specific location in quotation marks.
So mine is: “C:/Users/lplater/OneDrive/Documents/Classes/Lindsay's workshops/MICRO WORKSHOPS/RStudio”. Yours is probably shorter than that, hopefully. But the computer has run this line of code to change the directory so that you can see it down in this files window, and that you know where you're working, and you can open files from that location. The trick is: you'll remember I said the console doesn't save, so you'll want to take this blue text, copy it, and replace what's on line 10. If you have the script that I have written, on line 10 is my working directory, Lindsay's working directory. That's not the same as your working directory. Your computer has different folder structures, so you will have to take your blue text and replace my line 10. So mine's going to be the same, so if I copy-paste this and I paste it on line 10, it's the same thing that was already there because it's my working directory. But yours should have your location. So maybe you're on OneDrive, maybe you're not, but it should have your name and your file structure. And then make sure you save the file. The reason we do this, is if you want to come back to the script, either tonight or next week or next month or next year, you know where your file is saved. Not mine, because if you run line 10 it won't work.
So someone says: “from working directory what need to upload, I think I missed it”. You don't have to upload anything. The working directory is telling the computer which file structure you want to work out of. So if I say Session > Set Working Directory > Choose Directory, I want to use this File Explorer window to tell the computer where is my file saved, because together in about two minutes we're about to open our first file in RStudio. You have to tell the computer where is that file so we can open it. If you forget the working directory and you try to open a file, it won't work. So we've set our working directory.
Easiest way to set your working directory. We do the point and click method. It opens your File Explorer. You go through your File Explorer, say where your file is located. I recommend you save your code and the files you plan to work with in that same folder to make your life a little bit easier. And then if you have set your working directory successfully in the files window, you should see your files.
So here I see my RStudio files in the console I should see set working directory and then a bunch of blue text.
You'll have to adjust it so copy paste this onto line 10 for your own structure and you don't need this blue Pac-Man. To be clear, you don't need the very entire line, you just need the part that says set WD left bracket. What's the name of the path? All right. And then you want to save it to make sure that's in your source window.
Easy peasy. You have now set your working directory. That's the trickiest part of RStudio. If you just managed to do that or understood the concept, even if you don't have R on your computer, you just figured out the trickiest thing to learning RStudio for the first time.
License

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
- Ask Chat is a collaborative service
- Ask Us Online Chat hours
- Contact Us