3.4 Running Code in RStudio
Now that you’re somewhat familiar with RStudio, let’s run the same code as we ran on the website, but this time let’s run it in R.
3.4.1 The R Console:
In the R console, type 1+1
and press enter
.
The output in the console should look like the following:
Notice that the output 2
is displayed, and the cursor is on a blank line, waiting for more input. This is how coding in the console works.
3.4.2 R scripts
Now let’s run the same code, but in an R script.
If you haven’t already, create a new R script by clicking on the New File
icon, then selecting R Script
like so:
In the script window which opens, type 1+1
and press enter
.
Notice how now, the code did not run?
In a script, you are free to write R code on several lines before you run it.
You can even save the script and load it later in order to run the code it contains.
There are multiple ways to run R code in a script.
To run a single line of code, do one of the following:
- Place the cursor on the desired line, hold the
<control>
key, and pressenter
. On macOS, hold<command>
key and pressreturn
instead - Place the cursor on the desired line and click the
Run
button that looks like this:
To run multiple lines of code, do one of the following:
- Highlight all the code you’d like to run, hold the
<control>
key, and pressenter
. On macOS, hold the<command>
key and pressreturn
instead. - Highlight all the code you’d like to run, and click the
Run
button.
Run the 1+1
code using one of the methods above, and observe the output.
Notice how the output is still in the console window, even though you ran the code in a script!
Even though running R code from the console and an R script are done differently, they should produce the same results. Both are running R!
Now that you’ve run some code in the console and from an R script, let’s try some of the other code we ran previously.
3.4.3 Same Examples, On Your Computer!
In the console, type the command factorial(10)
.
Did you get the same result as you got on the website?
Now type the following two lines in an R script and run them:
This code produces a plot, which should show up in the lower right corner in the “Plots” window. Finally, copy the following code, paste it into your script, and run it:
install.packages("ggplot2")
library(ggplot2)
theme_set(theme_bw())
ggplot(mtcars, aes(y = mpg, fill = as.factor(cyl))) +
geom_boxplot() +
labs(title = "Engine Fuel Efficiency vs. Number of Cylinders",
y = "MPG",
fill = "Cylinders") +
theme(legend.position = "bottom",
axis.ticks.x = element_blank(),
axis.text.x = element_blank())
You’re now running R code on your computer!
The above code block includes a command to install an R package! ggplot2 is a very popular plotting package that can create sophisticated and (arguably) aesthetically pleasing graphs.
Imagine you are practicing programming in R and your classmate tells you they heard about an interesting new R command which they’d like you to try out. Would you run the command in an R script, or the R console? How might your answer change if you wanted to keep a record of all the interesting R commands you found?
3.4.4 R Markdown
You’ve seen how to run R code in the R console, and from an R script, but there’s one more way to run R that we need to talk about: R Markdown.
R scripts are convenient because they can store multiple R commands in one file. R Markdown takes this idea further and stores code alongside human readable text. There is much that could be said about R Markdown, but for now, we’ll just stick with the basics.
To start, watch this video:
This video gives a basic introduction to R Markdown.
As the video stated, there are three types of sections to an R Markdown document:
- Header
- Human readable text
- Code chunks
There’s only one header, but there can be many blocks of human readable text and many code chunks.
See here for more things you can do with R Markdown.
As part of this class, you’ll be filling in an R Markdown document as
you complete the progress checks in the book (except for the first
progress check box, which you completed already) Download the progress check 2
template into your scripts
folder, and follow the
instructions. That document should include all progress checks from
Section 3.4 through (and including) Section 4.3. The next box should be
the first code chunk you will include in the document!
Run the command 8 / (2 * (2 + 2))
and observe the
output!
This video should help get you started with the Progress Check Assignments!
Any feedback for this section? Click here