Writing Scripts in R | R Tutorial 1.12 | MarinStatsLectures

Hi! I am Mike Marin and in this video we’ll talk about
writing scripts of code in R. scripts are useful as they allow
one to easily pick up where they left off on a project and progressively build and refine code and analyses. they
also allow one to easily reproduce analyses that were run earlier. a script
is a set of commands that usually includes some commenting on what each piece of
code is intended to do I’ve already written a bit of script as
you can see here recall that the number sign or hash (#) can be used to prompt R to ignore
anything that immediately follows the simplest way to get started and the
least efficient way of doing things is to simply cut and paste snippets of
code and paste them into R. here I’ll go ahead and copy this and paste it into R a better approach is to have this script appear within RStudio .
an R script should be saved with the extension “.R” one can create a new script using the menu options in R. you can
select “File” and then “New” and here you see an option
for new “R script” or one may also open an existing R script using the
menu here we can select “Open File” and then we
can open up the R script directly from here. we can
now see this script of code in RStudio source
editor we can also adjust the size of the
windows in RStudio to our desired size. now we can see in here is where we’re able to write new
code and add to our existing script as we
progress through working with our data if you would like to submit a line of
code rather than copying this code and pasting it
into the R Console we can place the cursor on the line of
code we would like to have submitted and then we can either click on this “Run”
option to have this piece of code submitted you’ll notice that after submitting a
line of code the cursor would jump down to the next
line. we can also submit pieces of code using keyboard shortcuts: the shortcut to
enter this code would be “command & enter”; you’ll also
notice that if we submit one of our commented
lines R will ignore this. sometimes it will be the case that we would like to submit multiple
lines of code for example maybe to start, we would like to set our working directory and load the
previous one all at once. to do so we can highlight or select all the pieces of code we would
like to have entered and then click on “Run” or we can use the keyboard shortcut of
“command & enter” it’s worth noting that most of the functionality of a
regular text editor can be found in RStudio script editor.
for example under the “Edit” menu we can do a
“Find and Replace…” there are also few other useful menu options. we can also see under
the “Code” menu there’s this “Comment /Uncomment Lines”
this will allow us to take an entire section of code and add
comments that is the number sign or hash sign (#) in
front of them or take an entire section and remove the
number or hash signs (#) let’s go ahead and load our previous workspace. we place the
cursor on this line and click “Run” to submit this piece of
code. now is the point where we would continue along with the analysis we’ve been working on
let’s suppose that as a next step we decide to add a piece of code asking
for the class of the variable Gender. to submit this we can click on “Run” and you will notice ooops! we made a typo here. we can just go
back and correct this in our script and then resubmit the piece of code. once we
have the code as we like then we can go ahead and save
this script now there’s one handy feature in RStudio that we have not talked about yet and that’s the use the “Tab” key. you can
type the start of an R command and then hit the
“Tab” key and R will return to you a list of
suggestions of what you may be looking for for example we want to calculate the
mean we can select this and now we found the command we like and we
saved ourselves some typing time it’s worth noting that the use of the
Tab key will also work directly in the R Console and it will also work to autocomplete
objects that you’ve created for example “Lun” we can hit the Tab key and we can see RStudio returns to us
options of were you looking for LungCapData or the measurement LungCap which is stored inside the object LungCapData here let’s take a look at lung
capacities for person 1 up to 5 well not completely necessary use of the Tab key can help save some time
typing or help you find the name of a command
if you don’t quite remember what it was now once you’re done working for the day
you’re going to want to go ahead and save your script we can do this by placing the cursor in
the window where our script is located going up to “File” and then “Save” or
“Save As…” depending on if this is a newly created
script or one with already existing name. I’ll go ahead and save this you may also wish to save your workspace
image depending on how you like to work and how you like to organize your work
I’m gonna go ahead and submit this line of code (“save.image”) to save the
workspace image as well I hope this video has helped you see the
benefits of using a script in R thanks for watching this video and make
sure to check out my other instructional videos

Leave a Reply

Your email address will not be published. Required fields are marked *