Skip to main content
SAGE
Search form
  • 00:01

    MATT DENNY: Hi, everyone.This is your instructor, Matt Denny,and welcome to this lecture on accessing RStudiofunctionality.So this is a part two of our setup for RStudio,and we're going to be digging into how to really getthe most out of RStudio.So one of the things that's great about RStudiois that it combines together a whole lot of different add-ons

  • 00:26

    MATT DENNY [continued]: or companions to R that tend to be really useful for gettingthe most out of R, and oftentimes tendto be necessary for especially writing reproduceable R code.So as we talked about in the part one of this serieson using RStudio, it includes a text editor, an R script

  • 00:50

    MATT DENNY [continued]: editor.And if you want to be able to rerun any of your code,if you want to be able to apply your code to other projects,if you want to produce reproducible research,you're going to need to save your R code somewhere.So you're going to need to have a text editor open,and RStudio just includes that nicely in a bundle for you.Again, it also includes a whole lot of other functionality.

  • 01:12

    MATT DENNY [continued]: And so in the last video, we went overhow to make it look pretty and what all those windows mean.In this lecture, I'm going to coversome of the other functionality that Ithink is going to be really helpful for youin learning how to write R code.Things like some basics of debugging,code completion, some of the menus

  • 01:33

    MATT DENNY [continued]: that I'll tend to use, and also code formatting, whichcan be really, really helpful.And so with that, I'm going to head over to my desktop,we're going to open up RStudio, and we're goingto pick up where we left off.OK, so I'm over here on my desktop,and I'm going to open up RStudio.So by now, hopefully, you've picked a sweet themethat you like.Maybe it's a dark theme or a light theme

  • 01:54

    MATT DENNY [continued]: or it's a beige theme or a red theme or whatever kind of themeyou like, an appropriate font size, and so on.And hopefully you've maybe consideredat least giving the window pane set upthat I have a try for starters.I have spent a lot of time-- probably too much time--thinking about my windows setup.So what I want us to do now is we're

  • 02:16

    MATT DENNY [continued]: going to click on the Tools tab, and now we'regoing to click on Global Options again,and this is going to take us to this big bank of optionsthat we can use to modify RStudio to make it maximallyuseful for us.OK, so there are a couple of thingsI want to touch on right on this General tab.

  • 02:38

    MATT DENNY [continued]: One of the first things is that Iwould suggest that you set your default working directory,and I would suggest that you set it to your desktop.So you can use this little Browse option here,and you can set it to a file location on your computer.I would suggest you set it to your desktop.The reason for this is we're goingto be dealing with loading files in and saving files out

  • 02:60

    MATT DENNY [continued]: of RStudio, and sometimes you will notbe thinking, particularly when youwant to save something out of RStudio or out of your session.So R actually has its own particular kind of file type.These are all things that we're goingto deal with in the future.But in general, when you want to save something,it will save to your default working directory.

  • 03:22

    MATT DENNY [continued]: That's the default folder location on your computer.And if that isn't a place that you can find,then you might end up saving something to the void.You can go here and check where it is.You can also look up here in the top rightin your console screen.It will give you this little path to a working directory

  • 03:43

    MATT DENNY [continued]: that will actually be useful for you to know whereyour working directory is.But in general, I would say, set it somewherethat you can find easily.The other thing that I would suggest you dois make sure that this box, Restore R data Into Workspaceat Startup, is unchecked.So what R will do is, oftentimes when you close R,it will ask you, hey, by default,

  • 04:05

    MATT DENNY [continued]: do you want to save everything that you're working on?And then automatically load it back in.This can seem like a great idea at the start,but if it automatically loads things backin every time, what you can end up doingis you can end up accumulating a bunch of junkin your R session.Again, this will make more sense as weget into actually working with R programming,but definitely don't automatically

  • 04:26

    MATT DENNY [continued]: restore R data into workspace at startup.So that's something I would definitely select.And I think it's no longer checked by default. OK.You also definitely want to be automatically notifiedof updates to RStudio.Good thing.So the next tab I want to click on is the Code tab.So in general, I select all of these check boxes

  • 04:48

    MATT DENNY [continued]: here except for Focus Council After Executing From Source.And so what this does is, these areoptions that are there to help you write neater looking code.And one of the most important things,actually, is checking this soft wrap our source files.So what this will do-- let me click OK.

  • 05:10

    MATT DENNY [continued]: In the editing tab under code, and I've checked everything.I also like to set my tab with equal to 4.I want to insert matching prints in quotes.I want to auto-indent code after paste.I want to vertically align arguments.Some of these won't make a lot of sense to you right now.But they're all things that will be really useful when

  • 05:31

    MATT DENNY [continued]: you're working with our code.And there are options that I select by default.I'm going to click OK and just show you what I mean.So if I were to just type in a whole bunch of--so let's just typing in a whole bunch of stuff into our script.If I did not select autowrap, then what would happenis this bit of code would just sort of run off to the right.

  • 05:52

    MATT DENNY [continued]: And I'd get a little scroll bar down here.And I would have to scroll back and forthto see what was happening with my line of R code.But with autowrapping, as you can see,I can keep typing stuff in.And it will automatically wrap it around downto the next line.Now, one of the nice things you're going to see hereis I have these little line numbers.And so we're going to touch on how to get

  • 06:13

    MATT DENNY [continued]: those to show up for you.But I just want to, again, point out that it is so mucheasier when your code doesn't literally just run offto the right.I will go back to my global options.I will uncheck soft wrap our source files.And then what we're going to see is so we're down here.And we're going to get this little line that we're goinghave to scroll back and forth--super, super unhelpful.

  • 06:34

    MATT DENNY [continued]: You do not want to be dealing with that.So oops, I don't want to save this.So you're definitely going to want to select soft wrap URsource files.It's going to make your life so much easier.OK.If you only learn that from this video,you've already learned a good amount.So the next thing we're going to deal with is display.

  • 06:57

    MATT DENNY [continued]: So I like to check the highlights, selected word,highlight selected line.So this will tell you what line you're on, show line numbers.This is hugely, hugely important.When I'm working in our studio, I'mgoing to often reference, hey, let's look on lines 46 to 62of this r script.

  • 07:18

    MATT DENNY [continued]: So I'm actually going to be providing our scripts thatalready have R code in them.To you, it's the same R code that I'mgoing to be working through.So you can copy it.You can modify it.But you also going to want to knowwhere we're at in the R script.And so showing line numbers, it will just show them right downthe side here in this gray column--super helpful.

  • 07:38

    MATT DENNY [continued]: I also suggest that you show margin.So it's going to show this little line right here.So in general, there are actuallyrules regarding this, particularly,if you want to publish any software that you write in R.So there's things called R packages thatsort of extend the functionality of R. There's about 10,000of them now.They're kind of almost like a publication sometimes.

  • 07:59

    MATT DENNY [continued]: You can actually get your R package sighted.For example, if you have some new statistical methodor some cool new plot or just some useful functionalityadd-on for R, you can create a user-created packagewhich will then be distributed by the CRAN website.And if you want to publish a lot about that,you need to make sure that your R scripts don't go

  • 08:19

    MATT DENNY [continued]: more than 80 characters wide.It's just like part of the requirementsfor a lot of the journals that will publish this.And it's just good practice.You don't want to have lines of R code that shoot off foreverthat are super, super wide.And this little margin right herewill tell you, hey, OK, I've reached 80 characters.I should move onto the next line.I also definitely would just show indenting guides.

  • 08:41

    MATT DENNY [continued]: This is going to come in really handywhen we deal with things like looping and conditionalstatements.A blinking cursor can be helpful to find things and show syntaxhighlighting and console input.So this is going to actually--it's going to take the syntax highlighting.That's the helpful coloring that RStudio gives you.And it's going to show it in your actual console which

  • 09:02

    MATT DENNY [continued]: is again very, very helpful.Let's see-- under saving, we don't needto worry about anything there.Code completion-- definitely allow automatic completionsin console, insert parentheses after function completions,insert spaces around equals for argument completions.I check these four little boxes right here.

  • 09:24

    MATT DENNY [continued]: What these will do is, basically, they'llhelp R try and guess what you're actually looking forand then help you write neater code.I would also select a completion delay of three.So what this will do is that after youtype in three characters--

  • 09:45

    MATT DENNY [continued]: let me cancel out of here.So for example, one of the thingswe're going to learn about later on is the help function.So R will actually-- when you start to type inhelp, it will give you an option to select the help function,for example.And it will even give you some helpful little informationabout this.This is sort of a really cool add-on to R studio.Also, when you're creating variables, another thing

  • 10:05

    MATT DENNY [continued]: we'll be dealing with in a series of lectures.If you start typing in the name of some long variable,it will then give you an option to sort of click on it.And it will automatically complete that command for you--super, super helpful.I find after three characters is good,because you don't want it popping up immediately for you.So that's another useful option.So let's go back here into code.

  • 10:27

    MATT DENNY [continued]: So completion-- want to select those.And then under diagnostics, check everything.So this will allow R to tell you if there'ssomething potentially wrong.So I'm going to, again, show you what this means.Check all of these for now--don't worry too much about what they are mean.They will come back to help you.It's basically just saying, our studio,

  • 10:49

    MATT DENNY [continued]: I want the maximum possible amount of helpin making sure that I write good article.It will be like a good helpful friendand trying to tell you, OK, yeah, sure.So let's show what I mean by that.So I'm going to type in a line of code that'sgoing to have an error in it.So I'm going to call it Fun Times and then

  • 11:13

    MATT DENNY [continued]: a question mark, question mark, question mark.So let's see.Let's run in here.Well, that's not going to give me happy times.Let's see if we can get a better example here.But what should happen is that you'll

  • 11:34

    MATT DENNY [continued]: get little red squiggly-- let's make our own squiggly here.Let's call it temp equals temp.Don't worry about what I'm typing in here.Cat.It's really just not wanting to be--print.

  • 11:57

    MATT DENNY [continued]: Let's see.Oh, the reason it's not showing me anythingis because I haven't save this file.So how do I save things?Let's take a second here.So I can click on the little Save current document.This is this old little floppy disk here.So I'm going to save it to my desktop.I'm just going to call it example.So this is how you save an R script as you save one

  • 12:20

    MATT DENNY [continued]: to your desktop.Here we go.It'll give you a little symbols like this.It says, hey, there's no symbol named dog in scope.And what that kind of means is that you'retrying to print out something.And we're going to go for what thisall means that does not exist that is sort of invalid.Let's see if we can get a syntax error.

  • 12:41

    MATT DENNY [continued]: Will this give us a syntax error?OK, this little I, it will say, hey, expected whitespace.Or there's something weird going on here.And this all comes from this option over here in code.This all comes from these diagnostics options.So you'll definitely want to select all of those.

  • 13:02

    MATT DENNY [continued]: It'll just be super helpful to you down the line.Let's leave things at that.I want to touch now on some other options here.So over on the top left here, we have this little green plus.This is how we can create new R scripts.So this can actually be very helpful for creating new R

  • 13:25

    MATT DENNY [continued]: scripts.You can have more than one of them open at a time.And you can go back and forth between them.They're just sort of like little tabs in our studiowhich, again, is super, super helpful.Let's see, another couple of helpful tools--so remember, what I talked about your working directory?If you go to the session working directoryand then set working directory, you

  • 13:45

    MATT DENNY [continued]: can choose a directory to set your working directory to.This is, again, the place where R's goingto look for and save files.And it will give you a dropdown menu.And if I say, hey, I want to set it to--where's a good example?I can send it to my documents, or something like that.And then I can click to open.It will actually enter in our commands for you

  • 14:06

    MATT DENNY [continued]: into your console.One or two other-- well, one in particular other really usefullittle feature of our studio.And so remember when I talked about you write your R codehere in your source window.And then you want to enter it into the R console.So I'm going to just skip a little bit ahead.We're going to do a preview.All works like a calculator.

  • 14:27

    MATT DENNY [continued]: You can do things like 2 plus 2, and itwill give you the answer.So I could either write them over here.And then I could copy them into the R console.And it will run it.And it won't give me answers.Or I can highlight code and then click Run.And it will actually just automatically runthat code for me.Another option that you can do to run this code

  • 14:48

    MATT DENNY [continued]: is you can highlight your code.And if you're on a Mac, you would hit the Apple buttonand then enter.And that will automatically enter your code.Or if you're on, I believe, Linux or a Windows machine,if you highlight code, and then you hit control plus enter,that will also enter your code in.So these are just some useful little things

  • 15:10

    MATT DENNY [continued]: getting our studio ready to go.And so we're going to sort of take this.I'm going to assume that you've sort of been throughand check these options.And this is going to sort of set us upto really blast off into the stratosphere with using R.We've sort of set up our integrated development

  • 15:30

    MATT DENNY [continued]: environment--this program, this skin that lies on top of Rthat these other windows that sort of live around it.And we're going to then use all of this extra functionalityto make learning R and using R easier for us.So thanks for watching.This sort of part 2 of my intro, my sortof setting up our studio video.

  • 15:52

    MATT DENNY [continued]: And I will see you in the next lecture.

Video Info

Series Name: Practical Data Management with R

Episode: 7

Publisher: SAGE Publications Ltd

Publication Year: 2017

Video Type:Tutorial

Methods: Data management, RStudio, R statistical package, Programming

Keywords: coding; computer programming; computer programs; open source software; programming and scripting languages

Segment Info

Segment Num.: 1

Persons Discussed:

Events Discussed:

Keywords:

Abstract

Matt Denny explains how to setup RStudio to maximize its functionality, including the basics for debugging, code completion, and code formatting.

Looks like you do not have access to this content.

Introduction to RStudio: Accessing RStudio Functionality

Matt Denny explains how to setup RStudio to maximize its functionality, including the basics for debugging, code completion, and code formatting.

Copy and paste the following HTML into your website