< Return to Video

Running R Code in RStudio

  • 0:02 - 0:03
    >> Hi. I'm Erica.
  • 0:03 - 0:07
    In this video, I'm going to show you
    the two different places that you can run
  • 0:07 - 0:11
    your R code and the pros and cons
    between them and how you can do that.
  • 0:12 - 0:15
    So, this might be a little bit
  • 0:15 - 0:19
    of a review and a reiteration of something
    I've talked of, stuff
  • 0:19 - 0:23
    I've talked about in a previous video, but
    I wanted to do a separate video on this
  • 0:23 - 0:26
    just to kind of, hit this topic,
  • 0:27 - 0:28
    kind of really hard,
  • 0:28 - 0:31
    so that way it makes sense.
  • 0:31 - 0:34
    So there are two different places
    that you can, type your R code
  • 0:34 - 0:37
    and, use that to run.
  • 0:38 - 0:41
    So down here we have the,
    in the bottom left
  • 0:41 - 0:44
    corner usually is the console window.
  • 0:45 - 0:47
    And you can type out code here
  • 0:48 - 0:50
    just fine.
  • 0:50 - 0:53
    And then go ahead
    and press the enter button,
  • 0:54 - 0:57
    and it will run the code.
  • 0:58 - 1:00
    The major downside to doing,
  • 1:00 - 1:04
    anything down here in the console
    is that as soon as you exit
  • 1:04 - 1:10
    out of RStudio,
    this entire window is cleared and is gone.
  • 1:10 - 1:14
    So any code that you may have written
    down here that you wanted to have saved,
  • 1:15 - 1:17
    it would have been gone.
  • 1:17 - 1:20
    So here comes the good reason
    for the script window.
  • 1:21 - 1:23
    Up here you can type
  • 1:23 - 1:26
    the exact same code that you want up here.
  • 1:26 - 1:29
    Now notice if I'm to
    press the enter button to run it,
  • 1:31 - 1:33
    nothing happens.
  • 1:33 - 1:36
    It works just like a regular enter does.
  • 1:36 - 1:39
    Like in a regular word or text document.
  • 1:39 - 1:44
    In order to get this code to run,
    put your cursor on the line of code
  • 1:44 - 1:49
    that you want to run and either,
    and you press the run button right here.
  • 1:51 - 1:52
    Notice it pushes that code
  • 1:52 - 1:55
    down here to the console
    and runs it for you.
  • 1:56 - 1:59
    Let me try something different
    so you can see how that works.
  • 1:59 - 2:02
    So five minus two. Okay.
  • 2:02 - 2:05
    And my cursor is on the line
    I want to run. Run.
  • 2:05 - 2:06
    It pushes it down here.
  • 2:06 - 2:09
    Five minus two is three. Sweet.
  • 2:09 - 2:13
    Or a keyboard shortcut
    that a lot of people use is-
  • 2:13 - 2:16
    Let's do another command. Just for fun.
  • 2:16 - 2:19
    Three times four is control enter
  • 2:19 - 2:24
    if you have a windows computer or
    command enter if you have a mac computer.
  • 2:24 - 2:27
    So I'm going to press control enter.
  • 2:28 - 2:31
    And notice that pushes the code down here
    to the console.
  • 2:32 - 2:34
    And it is run.
  • 2:34 - 2:36
    Notice that this also only shows
  • 2:36 - 2:39
    the input where this
    shows the input and the output.
  • 2:41 - 2:43
    But if you had
  • 2:43 - 2:46
    some fancy code here
    that you wanted to save for later,
  • 2:46 - 2:49
    you can do that by saving this document.
  • 2:49 - 2:52
    And it will save it as a
  • 2:53 - 2:56
    let's do, call this tutorial document,
  • 2:57 - 3:00
    or like tutorial doc.
  • 3:01 - 3:05
    And now it'll save it as a .R file
    which means it's a script file.
  • 3:05 - 3:08
    So if you happen to exit out of R,
  • 3:08 - 3:10
    all this code that you have written
    can now
  • 3:10 - 3:12
    be saved.
  • 3:13 - 3:15
    One other thing to know
  • 3:15 - 3:18
    about running our code,
  • 3:18 - 3:23
    is that notice this is the little,
    this little, like, greater than sign
  • 3:23 - 3:29
    is the R prompt, meaning,
    that is where code is entered, to be run.
  • 3:30 - 3:35
    If that little prompt is there,
    that means that R is ready for a command
  • 3:35 - 3:39
    or a, piece of code to be given to it
    so that it can run.
  • 3:39 - 3:43
    So notice if I was to do three
  • 3:44 - 3:47
    cool, it just spits out the number three.
  • 3:47 - 3:50
    But if I was to do,
  • 3:51 - 3:52
    something like this
  • 3:52 - 3:55
    where I had forgotten a parentheses.
  • 3:57 - 3:59
    It's going to have this little plus sign.
  • 3:59 - 4:03
    What that means is that R is usually
    waiting for something else to happen.
  • 4:03 - 4:06
    And it can't finish executing the code
  • 4:06 - 4:09
    that you've given it,
    until something else happens.
  • 4:09 - 4:12
    Usually
    it's something that you have to do.
  • 4:13 - 4:16
    So in this case, it means you forgot
    a parentheses somewhere.
  • 4:16 - 4:19
    That might seem
    like a really simple mistake,
  • 4:19 - 4:22
    but as you start coding along
    and you get things nested
  • 4:22 - 4:25
    inside of each other,
    it gets to be really easy to forget
  • 4:25 - 4:28
    to make sure
    all your parentheses are there.
  • 4:28 - 4:31
    Just so that you know,
    the difference between,
  • 4:31 - 4:34
    what this is.
    This is not a plus sign, like,
  • 4:35 - 4:36
    like,
  • 4:36 - 4:39
    I mean, it is a plus sign,
    but it doesn't mean the same thing
  • 4:40 - 4:43
    as it does
    in, like, arithmetic in, in mathematics.
  • 4:43 - 4:47
    Here on this side, when it's on the very
    left hand side of the console window,
  • 4:48 - 4:52
    the little plus sign means that R
    is waiting for something else to happen.
  • 4:53 - 4:56
    Now what if you want to make notes
    for yourself in R,
  • 4:56 - 4:58
    but you want it to be in personal note
  • 4:58 - 5:01
    and it's not like actual R code
    that you want to run.
  • 5:01 - 5:06
    Like you want to remind yourself,
    okay, this is homework
  • 5:07 - 5:10
    problem one. Okay.
  • 5:10 - 5:12
    If you try and run that
  • 5:12 - 5:15
    R is not going to like it
    because it doesn't
  • 5:15 - 5:17
    know what you're talking about here.
  • 5:17 - 5:20
    So if you want to create
    a personal note to yourself that is, that
  • 5:20 - 5:24
    you don't want to have
    the run as R code, you can,
  • 5:25 - 5:27
    make it a comment.
  • 5:27 - 5:30
    And how you can do that in R
    is by using the pound symbol
  • 5:30 - 5:33
    or the hashtag symbol in front of it.
  • 5:34 - 5:37
    And anything after that symbol
  • 5:37 - 5:40
    to the end of the line
    will be commented out.
  • 5:40 - 5:44
    So, I mean, I could have this
    plus a bunch of space and then keep going
  • 5:45 - 5:48
    and notice how it's still kind of blue.
  • 5:48 - 5:50
    All of this is just a comment.
  • 5:50 - 5:53
    Now, if I go to the next line
    and I start typing this
  • 5:53 - 5:57
    next line, now is going to be interpreted
    as our code.
  • 5:58 - 6:01
    So if I were just to run this line
    and then the next line,
  • 6:01 - 6:04
    it said, okay, this is a comment,
    I'm not going to worry about it.
  • 6:04 - 6:07
    Then it gets to this line
    and it's like, what the heck is this?
  • 6:08 - 6:11
    So if you want to create a personal note
    for yourself,
  • 6:12 - 6:15
    you can do
    do that with the hashtag or pound symbol.
  • 6:16 - 6:19
    And that works
    for the entire rest of the line.
  • 6:21 - 6:21
    Okay.
  • 6:21 - 6:25
    So that is the pros
    and cons of running your R code,
  • 6:25 - 6:29
    either in the script window
    or in the console window.
  • 6:30 - 6:32
    And how you can also create comments
  • 6:32 - 6:36
    and how the console window is structured,
    with the prompt
  • 6:37 - 6:40
    and with the greater than sign
    and the plus sign and what those mean.
  • 6:42 - 6:42
    All right.
  • 6:42 - 6:45
    Have a good day
    and we'll see you next time in R.
Title:
Running R Code in RStudio
Video Language:
English
Duration:
06:47
Utah_State_University edited English subtitles for Running R Code in RStudio
Utah_State_University edited English subtitles for Running R Code in RStudio
Utah_State_University edited English subtitles for Running R Code in RStudio

English subtitles

Revisions Compare revisions