[MUSIC PLAYING]
SPEAKER: Well, hello, one and all, and welcome to our short on the R
console, which you can use to navigate the files and folders on your computer
as you write R programs.
Now, odds are you're used to a graphical user interface.
You can click on files and folders.
You can certainly do that in RStudio, as well.
But it's worth getting familiar now with the console, which
you can use to actually access files and folders
and create new files and folders, as well.
So here is the R console, with this blinking cursor here.
And everything we'll talk about today, is also inside of the R documentation.
So if I were to type in something like this, ?files,
I could actually open up the R documentation on file manipulation.
And you can get a reminder of the kinds of functions
we'll use today to create files, remove files, rename them, and so on.
So feel free to always use this if you need a hand along the way
while you're programming live on your own.
Now, let's figure out what we're going to do here.
I want to create a new program, maybe one to say hello to the world.
And to do that, I'll need to maybe make some new files, make some new folders,
and rename them and change them along the way.
So let's do just that and learn about the kinds of commands and functions
we can use to create these files and folders.
As we said before, R gives you what's called
a working directory, or the folder that, by default,
you'll create new files and folders inside of.
So if I were to type this function here, getwd(), well,
this would actually print for me the working directory,
where I currently am in my computer's file system.
So if I hit Enter, I'll see that I'm currently in the Users/jharvard folder.
OK, pretty good.
I might also ask, well, what files and folders are already here?
To do that, I could type in this function, list.files().
Hmm, I get back character(0).
And this actually means that there are no files and folders
inside of this directory, so blank slate for now.
Another alias or a name for list.list.files() is this function here
D-I-R, dir(), If I type dir(), I also get the same result.
It's the same function, same functionality now,
just different names for it, one being shorter than the other.
You can use them interchangeably.
So we've seen where we are in our file system, inside Users/jharvard,
and there are no files or folders inside of this working directory,
but let's go ahead and maybe create one.
So I might want to store my program to say hello
to the world inside of a folder.
So how could I create a folder?
Well, I could use a function called dir.create().
dir.create() will take as input, the name of the directory,
the folder I want to create.
So I'll type in "hello" to be the name of the folder in which I'll create this
program to say hello to the world.
Let me hit Enter now, and I'll see nothing seems to happen.
But if you look in your graphical interface,
over here, you might actually see this folder now called hello.
And I can even confirm this in the R console directly.
If I were to type either list.files() or dir(), I could then see, well,
I have a folder called hello inside of my working directory,
OK, so I have a folder, but then how do I get into it
to write, in this case, my program?
Well, I could use another function very similar to getwd(),
but this one is called setwd().
I want to change my working directory to be this particular folder that I
called hello.
So I can type setwd() and give as input, the folder I want to change to be
my working directory.
I'll type "hello" in this case.
And this will work because hello is part of my current working directory.
I'll type Enter here.
And now I'll see my working directory seems to have changed up top.
It used to be /Users/jharvard, but now it's /Users/jharvard/hello.
And I can confirm this using getwd(), hit Enter here,
and I'll see my new working directory.
OK, So here I now am inside of this hello folder.
What I've done metaphorically now, or actually really on my console, is
I've taken this folder that I see here.
I've clicked on it, and I've gone inside of it.
But of course, there are no files here.
We need to first create our file to say hello to the world.
So I can use this function that we've seen before,
file.create() that will create for me now a new file of any kind that I want.
I could maybe make a file to say hello to the world by typing "hello.R"
hitting Enter, and hmm, well, I see the file over here.
But I did make a typo.
So not to worry.
I can use a function to rename this file too.
So if I type list.files(), I'll see my misspelled file name.
But thankfully, we actually can use file.rename(),
another function here that takes as input two arguments.
The first being the file name that exists currently,
and the next being the file name we want to change to.
So this says rename hello.R L, to hello.R with two L's.
I'll hit Enter now, and I'll see true, meaning everything's gone well.
And now, if I actually look inside of my hello folder, well,
I should hopefully see the properly spelled hello.R. And again,
if I were to type either list.files() or dir(), I should now see hello.R,
pretty good.
And as a side note here, as I've been moving,
I've been kind of clearing my console as I go.
And to do that, there's a simple shortcut,
CTRL-L that can clear your console for you
so you can start fresh every time, OK.
So here I have hello.R. Why don't I actually go inside of it by clicking
on this file here and typing in, print("hello world"), just like this.
As we've seen, I could run this R code by clicking on the source button here.
Source means run this program.
So I could do just that here, source, and I'll see hello world down
in the console.
But I could even do this.
I could run this program from the console too.
I could type source myself, and then pass
in the name of the file I want to run, in this case, hello.R, hit Enter.
I'll see hello world.
Now, common mistake is that if you wanted to run some file,
but you're in the wrong working directory.
So always check if the file you want to run is in your working directory
by typing something like this, list.files,
and you're confirming that you see that particular file.
Or if it's not there, go ahead and use setwd
to set your working directory to where you can find that particular file.
All right, so I have my program here.
I can run it just as well.
And let's say I'm done.
I don't want this program anymore.
I might want to delete what I've done here.
So I'll go ahead and x this out.
My file is still there, of course, if I type list.files,
but now I might want to remove this file, for instance.
Well, I could, first, try to remove maybe
the file and the folder it's inside of to kind of clean things up.
Well, right now, I don't see that folder hello anymore.
I'm actually inside of it.
So to get back up from it, I can use setwd, but now
pass in the special kind of argument.
I'll type in dot dot.
And dot dot means move my working directory up one individual level.
So from, in this case, hello to jharvard.
I'll hit Enter here.
And notice how if I type getwd, well now I see I'm back in the jharvard folder.
I've moved up one level thanks to dot dot,
which has this very special meaning saying
move me up one particular directory.
All right, so if I type list.files now, I actually see the hello folder.
And to remove this, well, I could try this file.remove("hello"), hit Enter.
Hmm, And I'll actually see false, meaning that something didn't work,
and I'll see a warning message that says, it cannot remove file hello,
reason being directory not empty.
And that's true.
Because as we see here, hello still has this file hello.R inside of it.
So to remove this directory, we'll actually
first need to remove the hello.R file.
So I could use file.remove, but now first remove hello.R.
I could type file.remove("hello/hello.R").
And this is saying, go into the hello folder, find for me that hello.R file,
and remove it from that folder.
I'll hit Enter here, and I'll see true, seemed to have worked OK,
and now if I type file.remove("hello"), I can remove that directory that is now
empty.
I'll hit Enter here, and if I type list.files,
well, I'm back to having no files and folders inside of this directory.
Now, one other handy trick here.
Why don't I go back and recreate what I had before?
I'll say, dir.create("hello").
And then I'll say, file.create.
I can actually create files from above the given folder.
If I wanted to create hello.R inside of the hello folder
but not change my working directory, I could type out the path to it.
I could say, well, it's inside the hello folder,
and then I want to make this file hello.R.
So to be clear, I'm going first to the hello folder,
and this slash means inside that folder, go ahead and create hello.R for me.
I'll hit Enter and hopefully, see that that seemed to work.
I have true here.
And if I confirm this on the right-hand side,
I can actually see hello.R as well.
OK, so I've created these files again.
But now, you might catch on, do you have a folder with a lot of files?
It could be really annoying to go through and clean up
each one individually using file.remove, file.remove, file.remove.
So actually, R provides us with another approach
to remove a directory and all of its contents in one command.
So this is a bit of a dangerous one but also useful
if you want to clean things up quickly.
So this function is called unlink, unlink.
And I can type, as input, the first argument being
the folder I want to remove.
Now, there is a special parameter to unlink called recursive, recursive.
Recursive means, basically, go into this folder, and any subfolders
and remove those, unlink them as well.
And if I set recursive to true, what this will do,
is first look in the hello folder, and then descend
into any folders inside the hello folder,
and descend into any folders inside those folders and so on,
and go all the way down to remove or unlink
all the files and folders inside of a particular one beginning with hello.
So if I hit Enter now, and I type list.files, I'll see, in one command,
I've removed hello.R and the hello folder,
all thanks to unlink with this recursive parameter set to true.
So this was a brief overview of how you can use the console to write programs
in R, change your files, change your folders, and build things up as you go.
I'm excited to see what you'll create using the R console.
We'll see you next time.