-
Hi. I'm Erica.
-
In this video, I'm going to show you
the two different places that you can run
-
your R code and the pros and cons
between them and how you can do that.
-
So, this might be a little bit
-
of a review and a reiteration of something
I've talked of, stuff
-
I've talked about in a previous video, but
I wanted to do a separate video on this
-
just to kind of, hit this topic,
-
kind of really hard.
-
So that way it makes sense.
-
So there are two different places
that you can, type your R code
-
and, use that to run.
-
So down here
we have the in the bottom left
-
corner usually is the console window.
-
And you can type out code here
-
just fine.
-
And then
go ahead and press the enter button
-
and it will run the code.
-
The major downside to doing,
-
anything down here in the console
is that as soon as you exit
-
out of RStudio,
this entire window is cleared and is gone.
-
So any code that you may have written
down here that you wanted to have saved,
-
it would have been gone.
-
So here comes the good reason
for the script window.
-
Up here you can type
-
the exact same code that you want up here.
-
Now notice if I'm to
press the enter button to run it,
-
nothing happens.
-
It works just like a regular enter does.
-
Like in a regular word or text document.
-
In order to get this code to run,
put your cursor on the line of code
-
that you want to run and either,
and you press the run button right here.
-
Notice it pushes that code
-
down here to the console
and runs it for you.
-
Let me try something different
so you can see how that works.
-
So five minus two. Okay.
-
And my cursor is on the line
I want to run. Run.
-
It pushes it down here.
-
Five minus two is three. Sweet.
-
Or a keyboard shortcut
that a lot of people use is-
-
Let's do another command. Just for fun.
-
Three times four is control enter
-
if you have a windows computer or
command enter if you have a mac computer.
-
So I'm going to press control enter.
-
And notice that pushes the code down here
to the console.
-
And it is run.
-
Notice that this also only shows
-
the input where this
shows the input and the output.
-
But if you had
-
some fancy code here
that you wanted to save for later,
-
you can do that by saving this document.
-
And it will save it as a
-
let's do, call this tutorial document,
-
or like tutorial doc.
-
And now it'll save it as a Doc.R file
which means it's a script file.
-
So if you happen to exit out of R,
-
all this code that you have written
can now
-
be saved.
-
One other thing to know
-
about running our code,
-
is that notice this is the little,
this little, like, greater than sign
-
is the R prompt, meaning,
that is where code is entered, to be run.
-
If that little prompt is there,
that means that R is ready for a command
-
or a, piece of code to be given to it
so that it can run.
-
So notice if I was to do three
-
cool, it just spits out the number three.
-
But if I was to do,
-
something like this
-
where I had forgotten a parentheses.
-
It's going to have this little plus sign.
-
What that means is that R is usually
waiting for something else to happen.
-
And it can't finish executing the code
-
that you've given it,
until something else happens.
-
Usually
it's something that you have to do.
-
So in this case, it means you forgot
a parentheses somewhere.
-
That might seem
like a really simple mistake,
-
but as you start coding along
and you get things nested
-
inside of each other,
it gets to be really easy to forget
-
to make sure
all your parentheses are there.
-
Just so that you know,
the difference between,
-
what this is.
This is not a plus sign, like,
-
like,
-
I mean, it is a plus sign,
but it doesn't mean the same thing
-
as it does
in, like, arithmetic in, in mathematics.
-
Here on this side, when it's on the very
left hand side of the console window,
-
the little plus sign means that R
is waiting for something else to happen.
-
Now what if you want to make notes
for yourself in R,
-
but you want it to be in personal note
-
and it's not like actual R code
that you want to run.
-
Like you want to remind yourself,
okay, this is homework
-
problem one. Okay.
-
If you try and run that
-
R is not going to like it
because it doesn't
-
know what you're talking about here.
-
So if you want to create
a personal note to yourself that is, that
-
you don't want to have
the run as R code, you can,
-
make it a comment.
-
And how you can do that in R
is by using the pound symbol
-
or the hashtag symbol in front of it.
-
And anything after that symbol
-
to the end of the line
will be commented out.
-
So, I mean, I could have this
plus a bunch of space and then keep going
-
and notice how it's still kind of blue.
-
All of this is just a comment.
-
Now, if I go to the next line
and I start typing this
-
next line, now is going to be interpreted
as our code.
-
So if I were just to run this line
and then the next line,
-
it said, okay, this is a comment,
I'm not going to worry about it.
-
Then it gets to this line
and it's like, what the heck is this?
-
So if you want to create a personal note
for yourself,
-
you can do
do that with the hashtag or pound symbol.
-
And that works
for the entire rest of the line.
-
Okay.
-
So that is the pros
and cons of running your R code,
-
either in the script window
or in the console window.
-
And how you can also create comments
-
and how the console window is structured,
with the prompt
-
and with the greater than sign
and the plus sign and what those mean.
-
All right.
-
Have a good day
and we'll see you next time in R.