1 00:00:00,000 --> 00:00:02,370 [MUSIC PLAYING] 2 00:00:02,370 --> 00:00:05,690 3 00:00:05,690 --> 00:00:08,870 SPEAKER: Well, hello, one and all, and welcome to our short on the R 4 00:00:08,870 --> 00:00:12,860 console, which you can use to navigate the files and folders on your computer 5 00:00:12,860 --> 00:00:15,200 as you write R programs. 6 00:00:15,200 --> 00:00:18,050 Now, odds are you're used to a graphical user interface. 7 00:00:18,050 --> 00:00:20,210 You can click on files and folders. 8 00:00:20,210 --> 00:00:23,242 You can certainly do that in RStudio, as well. 9 00:00:23,242 --> 00:00:25,700 But it's worth getting familiar now with the console, which 10 00:00:25,700 --> 00:00:28,190 you can use to actually access files and folders 11 00:00:28,190 --> 00:00:31,260 and create new files and folders, as well. 12 00:00:31,260 --> 00:00:35,120 So here is the R console, with this blinking cursor here. 13 00:00:35,120 --> 00:00:40,130 And everything we'll talk about today, is also inside of the R documentation. 14 00:00:40,130 --> 00:00:43,940 So if I were to type in something like this, ?files, 15 00:00:43,940 --> 00:00:48,770 I could actually open up the R documentation on file manipulation. 16 00:00:48,770 --> 00:00:51,590 And you can get a reminder of the kinds of functions 17 00:00:51,590 --> 00:00:57,120 we'll use today to create files, remove files, rename them, and so on. 18 00:00:57,120 --> 00:01:00,620 So feel free to always use this if you need a hand along the way 19 00:01:00,620 --> 00:01:03,410 while you're programming live on your own. 20 00:01:03,410 --> 00:01:06,330 Now, let's figure out what we're going to do here. 21 00:01:06,330 --> 00:01:10,530 I want to create a new program, maybe one to say hello to the world. 22 00:01:10,530 --> 00:01:14,760 And to do that, I'll need to maybe make some new files, make some new folders, 23 00:01:14,760 --> 00:01:17,320 and rename them and change them along the way. 24 00:01:17,320 --> 00:01:21,120 So let's do just that and learn about the kinds of commands and functions 25 00:01:21,120 --> 00:01:24,790 we can use to create these files and folders. 26 00:01:24,790 --> 00:01:27,240 As we said before, R gives you what's called 27 00:01:27,240 --> 00:01:30,840 a working directory, or the folder that, by default, 28 00:01:30,840 --> 00:01:33,960 you'll create new files and folders inside of. 29 00:01:33,960 --> 00:01:38,820 So if I were to type this function here, getwd(), well, 30 00:01:38,820 --> 00:01:41,940 this would actually print for me the working directory, 31 00:01:41,940 --> 00:01:45,270 where I currently am in my computer's file system. 32 00:01:45,270 --> 00:01:52,260 So if I hit Enter, I'll see that I'm currently in the Users/jharvard folder. 33 00:01:52,260 --> 00:01:53,920 OK, pretty good. 34 00:01:53,920 --> 00:01:58,170 I might also ask, well, what files and folders are already here? 35 00:01:58,170 --> 00:02:02,760 To do that, I could type in this function, list.files(). 36 00:02:02,760 --> 00:02:08,280 Hmm, I get back character(0). 37 00:02:08,280 --> 00:02:11,220 And this actually means that there are no files and folders 38 00:02:11,220 --> 00:02:15,690 inside of this directory, so blank slate for now. 39 00:02:15,690 --> 00:02:20,520 Another alias or a name for list.list.files() is this function here 40 00:02:20,520 --> 00:02:25,380 D-I-R, dir(), If I type dir(), I also get the same result. 41 00:02:25,380 --> 00:02:28,020 It's the same function, same functionality now, 42 00:02:28,020 --> 00:02:31,500 just different names for it, one being shorter than the other. 43 00:02:31,500 --> 00:02:33,990 You can use them interchangeably. 44 00:02:33,990 --> 00:02:39,330 So we've seen where we are in our file system, inside Users/jharvard, 45 00:02:39,330 --> 00:02:43,890 and there are no files or folders inside of this working directory, 46 00:02:43,890 --> 00:02:45,930 but let's go ahead and maybe create one. 47 00:02:45,930 --> 00:02:49,890 So I might want to store my program to say hello 48 00:02:49,890 --> 00:02:51,960 to the world inside of a folder. 49 00:02:51,960 --> 00:02:54,030 So how could I create a folder? 50 00:02:54,030 --> 00:02:58,290 Well, I could use a function called dir.create(). 51 00:02:58,290 --> 00:03:03,120 dir.create() will take as input, the name of the directory, 52 00:03:03,120 --> 00:03:05,590 the folder I want to create. 53 00:03:05,590 --> 00:03:09,970 So I'll type in "hello" to be the name of the folder in which I'll create this 54 00:03:09,970 --> 00:03:12,280 program to say hello to the world. 55 00:03:12,280 --> 00:03:15,440 Let me hit Enter now, and I'll see nothing seems to happen. 56 00:03:15,440 --> 00:03:18,220 But if you look in your graphical interface, 57 00:03:18,220 --> 00:03:22,960 over here, you might actually see this folder now called hello. 58 00:03:22,960 --> 00:03:26,230 And I can even confirm this in the R console directly. 59 00:03:26,230 --> 00:03:30,670 If I were to type either list.files() or dir(), I could then see, well, 60 00:03:30,670 --> 00:03:36,230 I have a folder called hello inside of my working directory, 61 00:03:36,230 --> 00:03:40,220 OK, so I have a folder, but then how do I get into it 62 00:03:40,220 --> 00:03:42,710 to write, in this case, my program? 63 00:03:42,710 --> 00:03:48,650 Well, I could use another function very similar to getwd(), 64 00:03:48,650 --> 00:03:51,020 but this one is called setwd(). 65 00:03:51,020 --> 00:03:54,950 I want to change my working directory to be this particular folder that I 66 00:03:54,950 --> 00:03:56,240 called hello. 67 00:03:56,240 --> 00:04:03,140 So I can type setwd() and give as input, the folder I want to change to be 68 00:04:03,140 --> 00:04:04,400 my working directory. 69 00:04:04,400 --> 00:04:06,470 I'll type "hello" in this case. 70 00:04:06,470 --> 00:04:12,020 And this will work because hello is part of my current working directory. 71 00:04:12,020 --> 00:04:13,700 I'll type Enter here. 72 00:04:13,700 --> 00:04:17,750 And now I'll see my working directory seems to have changed up top. 73 00:04:17,750 --> 00:04:22,790 It used to be /Users/jharvard, but now it's /Users/jharvard/hello. 74 00:04:22,790 --> 00:04:25,310 75 00:04:25,310 --> 00:04:30,500 And I can confirm this using getwd(), hit Enter here, 76 00:04:30,500 --> 00:04:33,980 and I'll see my new working directory. 77 00:04:33,980 --> 00:04:38,640 OK, So here I now am inside of this hello folder. 78 00:04:38,640 --> 00:04:41,640 What I've done metaphorically now, or actually really on my console, is 79 00:04:41,640 --> 00:04:45,410 I've taken this folder that I see here. 80 00:04:45,410 --> 00:04:47,700 I've clicked on it, and I've gone inside of it. 81 00:04:47,700 --> 00:04:49,800 But of course, there are no files here. 82 00:04:49,800 --> 00:04:53,860 We need to first create our file to say hello to the world. 83 00:04:53,860 --> 00:04:56,130 So I can use this function that we've seen before, 84 00:04:56,130 --> 00:05:02,580 file.create() that will create for me now a new file of any kind that I want. 85 00:05:02,580 --> 00:05:07,290 I could maybe make a file to say hello to the world by typing "hello.R" 86 00:05:07,290 --> 00:05:14,920 hitting Enter, and hmm, well, I see the file over here. 87 00:05:14,920 --> 00:05:16,270 But I did make a typo. 88 00:05:16,270 --> 00:05:18,550 So not to worry. 89 00:05:18,550 --> 00:05:22,640 I can use a function to rename this file too. 90 00:05:22,640 --> 00:05:27,340 So if I type list.files(), I'll see my misspelled file name. 91 00:05:27,340 --> 00:05:32,110 But thankfully, we actually can use file.rename(), 92 00:05:32,110 --> 00:05:36,070 another function here that takes as input two arguments. 93 00:05:36,070 --> 00:05:40,660 The first being the file name that exists currently, 94 00:05:40,660 --> 00:05:45,190 and the next being the file name we want to change to. 95 00:05:45,190 --> 00:05:50,320 So this says rename hello.R L, to hello.R with two L's. 96 00:05:50,320 --> 00:05:54,340 I'll hit Enter now, and I'll see true, meaning everything's gone well. 97 00:05:54,340 --> 00:05:58,210 And now, if I actually look inside of my hello folder, well, 98 00:05:58,210 --> 00:06:02,740 I should hopefully see the properly spelled hello.R. And again, 99 00:06:02,740 --> 00:06:08,860 if I were to type either list.files() or dir(), I should now see hello.R, 100 00:06:08,860 --> 00:06:10,020 pretty good. 101 00:06:10,020 --> 00:06:12,520 And as a side note here, as I've been moving, 102 00:06:12,520 --> 00:06:15,200 I've been kind of clearing my console as I go. 103 00:06:15,200 --> 00:06:17,750 And to do that, there's a simple shortcut, 104 00:06:17,750 --> 00:06:20,930 CTRL-L that can clear your console for you 105 00:06:20,930 --> 00:06:24,600 so you can start fresh every time, OK. 106 00:06:24,600 --> 00:06:28,040 So here I have hello.R. Why don't I actually go inside of it by clicking 107 00:06:28,040 --> 00:06:34,790 on this file here and typing in, print("hello world"), just like this. 108 00:06:34,790 --> 00:06:40,730 As we've seen, I could run this R code by clicking on the source button here. 109 00:06:40,730 --> 00:06:43,170 Source means run this program. 110 00:06:43,170 --> 00:06:46,670 So I could do just that here, source, and I'll see hello world down 111 00:06:46,670 --> 00:06:47,570 in the console. 112 00:06:47,570 --> 00:06:50,130 But I could even do this. 113 00:06:50,130 --> 00:06:53,360 I could run this program from the console too. 114 00:06:53,360 --> 00:06:56,720 I could type source myself, and then pass 115 00:06:56,720 --> 00:07:03,150 in the name of the file I want to run, in this case, hello.R, hit Enter. 116 00:07:03,150 --> 00:07:05,420 I'll see hello world. 117 00:07:05,420 --> 00:07:09,860 Now, common mistake is that if you wanted to run some file, 118 00:07:09,860 --> 00:07:12,680 but you're in the wrong working directory. 119 00:07:12,680 --> 00:07:17,390 So always check if the file you want to run is in your working directory 120 00:07:17,390 --> 00:07:20,840 by typing something like this, list.files, 121 00:07:20,840 --> 00:07:24,140 and you're confirming that you see that particular file. 122 00:07:24,140 --> 00:07:27,830 Or if it's not there, go ahead and use setwd 123 00:07:27,830 --> 00:07:33,560 to set your working directory to where you can find that particular file. 124 00:07:33,560 --> 00:07:35,690 All right, so I have my program here. 125 00:07:35,690 --> 00:07:37,500 I can run it just as well. 126 00:07:37,500 --> 00:07:39,163 And let's say I'm done. 127 00:07:39,163 --> 00:07:40,580 I don't want this program anymore. 128 00:07:40,580 --> 00:07:42,660 I might want to delete what I've done here. 129 00:07:42,660 --> 00:07:44,360 So I'll go ahead and x this out. 130 00:07:44,360 --> 00:07:47,690 My file is still there, of course, if I type list.files, 131 00:07:47,690 --> 00:07:51,590 but now I might want to remove this file, for instance. 132 00:07:51,590 --> 00:07:54,920 Well, I could, first, try to remove maybe 133 00:07:54,920 --> 00:07:59,270 the file and the folder it's inside of to kind of clean things up. 134 00:07:59,270 --> 00:08:03,920 Well, right now, I don't see that folder hello anymore. 135 00:08:03,920 --> 00:08:05,790 I'm actually inside of it. 136 00:08:05,790 --> 00:08:11,930 So to get back up from it, I can use setwd, but now 137 00:08:11,930 --> 00:08:14,480 pass in the special kind of argument. 138 00:08:14,480 --> 00:08:16,234 I'll type in dot dot. 139 00:08:16,234 --> 00:08:21,770 And dot dot means move my working directory up one individual level. 140 00:08:21,770 --> 00:08:26,300 So from, in this case, hello to jharvard. 141 00:08:26,300 --> 00:08:27,540 I'll hit Enter here. 142 00:08:27,540 --> 00:08:33,380 And notice how if I type getwd, well now I see I'm back in the jharvard folder. 143 00:08:33,380 --> 00:08:36,830 I've moved up one level thanks to dot dot, 144 00:08:36,830 --> 00:08:39,020 which has this very special meaning saying 145 00:08:39,020 --> 00:08:42,380 move me up one particular directory. 146 00:08:42,380 --> 00:08:46,400 All right, so if I type list.files now, I actually see the hello folder. 147 00:08:46,400 --> 00:08:53,690 And to remove this, well, I could try this file.remove("hello"), hit Enter. 148 00:08:53,690 --> 00:08:57,560 Hmm, And I'll actually see false, meaning that something didn't work, 149 00:08:57,560 --> 00:09:02,210 and I'll see a warning message that says, it cannot remove file hello, 150 00:09:02,210 --> 00:09:05,150 reason being directory not empty. 151 00:09:05,150 --> 00:09:06,140 And that's true. 152 00:09:06,140 --> 00:09:12,450 Because as we see here, hello still has this file hello.R inside of it. 153 00:09:12,450 --> 00:09:15,080 So to remove this directory, we'll actually 154 00:09:15,080 --> 00:09:18,650 first need to remove the hello.R file. 155 00:09:18,650 --> 00:09:22,790 So I could use file.remove, but now first remove hello.R. 156 00:09:22,790 --> 00:09:25,280 I could type file.remove("hello/hello.R"). 157 00:09:25,280 --> 00:09:28,400 158 00:09:28,400 --> 00:09:34,130 And this is saying, go into the hello folder, find for me that hello.R file, 159 00:09:34,130 --> 00:09:36,380 and remove it from that folder. 160 00:09:36,380 --> 00:09:39,920 I'll hit Enter here, and I'll see true, seemed to have worked OK, 161 00:09:39,920 --> 00:09:46,070 and now if I type file.remove("hello"), I can remove that directory that is now 162 00:09:46,070 --> 00:09:46,730 empty. 163 00:09:46,730 --> 00:09:50,360 I'll hit Enter here, and if I type list.files, 164 00:09:50,360 --> 00:09:56,300 well, I'm back to having no files and folders inside of this directory. 165 00:09:56,300 --> 00:09:58,560 Now, one other handy trick here. 166 00:09:58,560 --> 00:10:01,040 Why don't I go back and recreate what I had before? 167 00:10:01,040 --> 00:10:04,100 I'll say, dir.create("hello"). 168 00:10:04,100 --> 00:10:07,160 And then I'll say, file.create. 169 00:10:07,160 --> 00:10:11,840 I can actually create files from above the given folder. 170 00:10:11,840 --> 00:10:16,160 If I wanted to create hello.R inside of the hello folder 171 00:10:16,160 --> 00:10:20,520 but not change my working directory, I could type out the path to it. 172 00:10:20,520 --> 00:10:23,390 I could say, well, it's inside the hello folder, 173 00:10:23,390 --> 00:10:26,570 and then I want to make this file hello.R. 174 00:10:26,570 --> 00:10:29,540 So to be clear, I'm going first to the hello folder, 175 00:10:29,540 --> 00:10:34,910 and this slash means inside that folder, go ahead and create hello.R for me. 176 00:10:34,910 --> 00:10:39,440 I'll hit Enter and hopefully, see that that seemed to work. 177 00:10:39,440 --> 00:10:40,770 I have true here. 178 00:10:40,770 --> 00:10:43,460 And if I confirm this on the right-hand side, 179 00:10:43,460 --> 00:10:48,420 I can actually see hello.R as well. 180 00:10:48,420 --> 00:10:50,750 OK, so I've created these files again. 181 00:10:50,750 --> 00:10:54,890 But now, you might catch on, do you have a folder with a lot of files? 182 00:10:54,890 --> 00:10:58,340 It could be really annoying to go through and clean up 183 00:10:58,340 --> 00:11:02,960 each one individually using file.remove, file.remove, file.remove. 184 00:11:02,960 --> 00:11:06,470 So actually, R provides us with another approach 185 00:11:06,470 --> 00:11:11,210 to remove a directory and all of its contents in one command. 186 00:11:11,210 --> 00:11:13,730 So this is a bit of a dangerous one but also useful 187 00:11:13,730 --> 00:11:15,920 if you want to clean things up quickly. 188 00:11:15,920 --> 00:11:21,110 So this function is called unlink, unlink. 189 00:11:21,110 --> 00:11:24,020 And I can type, as input, the first argument being 190 00:11:24,020 --> 00:11:26,660 the folder I want to remove. 191 00:11:26,660 --> 00:11:32,420 Now, there is a special parameter to unlink called recursive, recursive. 192 00:11:32,420 --> 00:11:36,800 Recursive means, basically, go into this folder, and any subfolders 193 00:11:36,800 --> 00:11:40,140 and remove those, unlink them as well. 194 00:11:40,140 --> 00:11:43,640 And if I set recursive to true, what this will do, 195 00:11:43,640 --> 00:11:47,240 is first look in the hello folder, and then descend 196 00:11:47,240 --> 00:11:50,160 into any folders inside the hello folder, 197 00:11:50,160 --> 00:11:53,070 and descend into any folders inside those folders and so on, 198 00:11:53,070 --> 00:11:55,860 and go all the way down to remove or unlink 199 00:11:55,860 --> 00:12:00,990 all the files and folders inside of a particular one beginning with hello. 200 00:12:00,990 --> 00:12:06,810 So if I hit Enter now, and I type list.files, I'll see, in one command, 201 00:12:06,810 --> 00:12:09,870 I've removed hello.R and the hello folder, 202 00:12:09,870 --> 00:12:15,340 all thanks to unlink with this recursive parameter set to true. 203 00:12:15,340 --> 00:12:19,770 So this was a brief overview of how you can use the console to write programs 204 00:12:19,770 --> 00:12:24,360 in R, change your files, change your folders, and build things up as you go. 205 00:12:24,360 --> 00:12:27,630 I'm excited to see what you'll create using the R console. 206 00:12:27,630 --> 00:12:30,290 We'll see you next time. 207 00:12:30,290 --> 00:12:31,000