R
Key concepts that you will learn:
R
and RStudio
?R
?R
?You will navigate to rstudio.pomona.edu and use your Pomona credentials to log in. If you are a Pomona student, this is simply your Pomona email. If you are a non-Pomona student, ITS should have provided you a Pomona account. You may need to first activate this account to be able to proceed.
Note! If you run the command
library(package)
and see an error in red text stating
Error in library(package) : there is no package called ‘package’
,
don’t worry! That simply means that the package is not installed in your
R
environment. All you need to do is just run
install.packages(package)
once. (Of
course, replace package
with the name of the package in
question, such as swirl
).
R
and RStudio?In this class, we will take a cookbook approach to environmental data science.What does a “cookbook approach” mean in this context? In this course, I do not assume that you have prior programming experience and instead I will provide worksheets and worked-through example code that you then execute and learn by doing (or code running). Where appropriate, I will also use faded examples to advance learning in a structured and scaffolded way (see Section 7 in Wilson 2019 for more details if you are curious).
Specifically, you will interact with R
, a statistical
programming language, using RStudio. RStudio is an “IDE” or an
integrated development environment. Basically, if R
is the
engine of our (statistical programming) car, then RStudio is our
dashboard, with all of the controls that we’d be more familiar with
(plotting window, file visualization pane, help pane, etc.).
RStudio has 3 different panes:
R
interprets
R
to perform an act by
giving it commands in the console.R
,
view help, and view spreadsheets (Viewer
).R
code!Copy the code below into the console and hit enter. What do you see?
x <- c(1,2,3)
x
You should see the following:
## [1] 1 2 3
Congratulations! You have created your first object in
R
: a vector storing the numbers 1, 2, and 3.
You achieved that by using the assignment operator <-
to tell R
to create a new object, x
, that
stores the values 1, 2, and 3 in a vector, denoted by
c(...)
where the ...
is just a placeholder for
whatever you’d like to enter (where each element is separated by
commas).
R
with swirl
We will use the package swirl
to learn more about
several fundamental concepts in R
.
library(swirl)
swirl()
| Welcome to swirl!...call yourself something unique.
What shall I call you?
R
, you
may see functions popping up in auto-complete.Char
.swirl
will
interact with you in the console, and any time you see three blue dots
(an ellipsis), you should hit enter.| Thanks, <FIRST NAME>. Let's cover a couple of quick housekeeping items...
swirl
steps, which lead
to this query in red text:
| Please choose a course, or type 0 to exit swirl
swirl
has mentioned the
R prompt (>)
several times.>
is an example of a command
prompt where you tell a computer (in this case, the server
hosting RStudio Server which is running R
code) to execute
your command.>>>
(if you are interacting
with Python on the command line in a terminal). In Unix operating
systems (e.g. Linux distributions or Mac OS), the command prompt for the
terminal is often denoted by $
.>
in the R
console within RStudio Server is where R
awaits your
instructions (commands).| Please choose a course...
),
type 1
after the blue text reading
Selection:
| Please choose a lesson...
, please type 1
after the blue text reading Selection:
.
1: Basic Building Blocks
lesson!R
using this first class in swirl
.swirl
instructions
at that step. You can always use the vertical navigation bar on the
right-hand side of the console to roll back up and see what the last
instruction was. For example, in response to this instruction:| To see another example of how this vector 'recycling' works, try
| adding c(1, 2, 3, 4) and c(0, 10). Don't worry about saving the
| result in a new variable.
my command here was incorrectly specified (typed in at the
R
command prompt >
):
c(1, 2, 3, 4) _ c(0, 10)
and threw an error:
Error: unexpected input in "c(1, 2, 3, 4) _"
In this case, I ended up fixing it by typing:
c(1, 2, 3, 4) + c(0, 10)
Unfortunately, with these types of syntax errors (akin to a
grammatical error in a human language, like kluging your verb
conjugation and forgetting the term for a conjunction to join two
clauses together), swirl
won’t jump in and “automagically”
help you.
If you get really stuck, first try entering something that is
syntatically correct in the console at the command prompt. For instance,
you could try myName <- "Char"
. While this isn’t the
correct answer to this swirl
instruction, because this
command can be interpreted by R
, you’ll get kicked back
into swirl
instructions that may offer you something that
you could directly copy and paste into the console, e.g.:
| That's not exactly what I'm looking for. Try again. Or, type info()
| for more options.
| Type c(1, 2, 3, 4) + c(0, 10, 100) to see how R handles adding two
| vectors, when the shorter vector's length does not divide evenly
| into the longer vector's length. Don't worry about assigning the
| result to a variable.
(Note that here you would type or copy and paste
c(1, 2, 3, 4) + c(0, 10, 100)
into the console at the
command prompt >
.)
>
), you can exit the course and swirl
at
any time by typing bye()
into the console.
swirl
will save where you are in the
course so you won’t lose your progress!|=========
, the number at the right hand side,
after another |
represents the percentage of this course
that you have completed.swirl
asking:| Would you like to receive credit for completing this course on
| Coursera.org?
I recommend typing in 2
(representing “No”) at the
Selection:
field.
swirl
will tell you that you’ve completed
the course, you will see the following in red text:| Excellent work!
| You've reached the end of this lesson...
| Please choose a course, or type 0 to exit swirl.
In response to the blue Selection:
query, please type
0.
swirl
course, please
navigate to the Assignment page in our class website and select the
correct option for the
Completion of swirl class (Basic Building Blocks)
assignment.