Javascript required
Skip to content Skip to sidebar Skip to footer

How to Continue a New Line of Code in R Studio

When trying to complete an assignment or a complicated analysis all of the work cannot be completed in one sitting. Even if it could, you should want to have a record of what you did in case you need to rerun the analysis or use it again for a similar but different situation. To do this we can use R script files. RStudio makes it straight forward to do this, you can even just use a script as a scratch space without formally creating a file.

In this post, I'll walk you through adding this capability to your RStudio workspace and some short cuts and tricks to using scripts.

Adding a script file to your RStudio view

Just a reminder, your workspace should look like the below view if you haven't already opened a file or a script in RStudio:

RStudioInitialWindow_labelled
Initial RStudio Interface View

To add a script file, either click on the green plus button on the upper left in the tool bar and selectR Script or go to File > New File > R Script or on Windows use the commands Ctrl + Shift + N. On Mac OS this should be Command + Shift + N.

AddRScript
Adding an R Script via the Add File Icon

After you add the script your RStudio view should look like the following (a new pane is added to the upper left):

RStudioInitialWindow_ScriptPane
RStudio Interface with Script/File Pane

Everything that we can type in the Console we can type in the Script file, the only difference is that you won't see a carat at the beginning of the line. In a script file you can run one line of code at a time or multiple lines.

Running commands from a script file

In my previous post (Learning How to Use RStudio as a Calculator) we created a variable and calculated some summary statistics on the data. Let's use these data again now in a script.

In the script file, copy and paste the following (copy all 4 lines at once):

frisbeeDist <-c(8.22,6.00,-5.11,1.81,2.92,-6.29,8.57,1.18,-4.80,9.81) mean(frisbeeDist) sd(frisbeeDist) summary(frisbeeDist)

Your script file should look like this:

initialCommandsRScript
Four lines of code in an R Script

We will start by running one line of code at a time. Click anywhere in the first line where we are saving the data to the frisbeeDist variable. There are a few ways to run or perform this action.

To run a line of code perform one of the following actions:

  1. Click on the Run button on the upper right of the script window.
  2. Use the command Ctrl + Enter on Windows or Command + Return on Mac OS.
  3. From the Code menu, select, Run Selected Line(s).

After you run the first line, it will be automatically copied to the Console and the command executed. All output will be in the Console.

executingCode.png
Ran first line of code and created the variable

Similarly the next three lines of code can be run one line at a time, or you can highlight all three lines and run all three at once. The actions are performed from top down so that if a line depends on a line above the actions will be performed in the correct order.

threeLinesSelected
You can select multiple lines to run at once

Once you select the lines, use one of the three ways to run code as listed above. The resulting Console window will look like this:

consoleAfterThreeLines
Console pane after running all four lines of code

Adding Comments to a Script File

The whole point of using script files is to keep code that you have run so that you can reuse it at a later time. In order to really take advantage of that you need to leave yourself notes as to what you did and why. You can do this in R Scripts by indicating a line is a comment and not something that should be executed.

We indicate a line in a script is a comment by starting the line with at least one hash or pound symbol: #. Commented lines will be green in the script file. In the image below I have added comments to the previous lines of files. Note these comments are copied to the console but no action is taken and are blue in the console.

commentedScriptandOutput
Commented script file and executed commented code

One other trick to making your files readable is adding white lines or spaces to break up the content. White lines when executed just act as a return in the console and just add blank lines between previous code or output and the next line of code or output.

Question: Which script file is more readable and understandable?

Saving the file

Now, you can take advantage of RStudio automatically saving and remembering anything typed in a script file and just close RStudio without saving this script to a named file. However, if anything happened to your computer you run the risk of losing this information. So I strongly recommend having a single script file for an assignment or project. To save the script, you can either click on the blue save icon, use the keyboard commands Ctrl + S on Windows or Command + S on Mac OS or go to File > Save. This file will be open in RStudio the next time you reopen RStudio unless you click on the X on the file tab to close it.

Viewing other files or data in RStudio

Many other types of files can be opened in RStudio besides RScripts. All of these files or data views will be created as tabs in this upper left hand pane. I will discuss importing data into RStudio in my next blog post, once data is imported, if you view the file it will be available as a tab.

Here is what it will look like to view data in a tab (with the previous script saved as "frisbeeDistAnalysis.R":

DataViewTab
Data view in second tab in upper left pane

What's Next?

The blog post will cover how to import data into RStudio to do analysis on sample data.

If you have any questions please don't hesitate to reach out to greatlineswriting@outlook.com!

mccallacess1991.blogspot.com

Source: https://greatlineswriting.com/2018/03/01/saving-and-reusing-code-and-commands-in-rstudio/