Built-in help pages
There is an online help facility that can help you to see what a particular
function is supposed to do.
If you know the name of the function that you need help with, the
help() function is likely sufficient.
It may be called with a string or function name as an argument, or you
can simply put a question mark (?) in front of your query.
2
Built-in help pages
For example, for help on the q() function, type
?q
or
help(q)
or just hit the F1 key while pointing at q in RStudio. Any of these will
open a help page containing a description of the function for quitting R.
3
Quitting R
To quit your R session, type q()
q()
If you then hit the Enter key, you will be asked whether to save an image
of the current workspace, or not, or to cancel.
The workspace image contains a record of the computations you’ve
done, and may contain some saved results.
4
Functions
Most of the work in R is done using functions.
For example, we saw that to quit R we type q(). This tells R to call the
function named q.
The brackets surround the argument list, which in this case contains
nothing: we just want R to quit, and do not need to tell it how.
5
q is a function
Attempting to quit without the brackets gives:
q
## function (save = "default", status = 0, runLast = TRUE)
## .Internal(quit(save, status, runLast))
## <bytecode: 0x55b98f4fb3b8>
## <environment: namespace:base>
This has happened because q is a function that is used to
tell R to quit.
6
q is a function
Typing q by itself tells R to show us the contents of the function q. By
typing q(), we are telling R to call the function q.
The action of this function is to quit R.
q has three arguments: save, status, and runLast.
7
Default Values of Parameters
Each argument has a default value: "default", 0, and TRUE
respectively.
What happens when we execute q() is that R calls the q function with
the arguments set to their default values.
8
Changing from the Defaults
To change from the default values, specify them in the function call.
Arguments are identified by their position or by their name.
For example,
q("no") # these calls tell R to quit without
q(save = "no") # saving the workspace image
9
Changing from the Defaults
If we had given two arguments without names, they would apply to save
and status.
If we want to accept the defaults of the early parameters but change later
ones, we give the name when calling the function, e.g.
q(runLast = FALSE)
10
Changing from the Defaults
Alternatively, commas can be used to mark the missing arguments, e.g.
q( , , FALSE)
It is a good idea to use named arguments when calling a function which
has many arguments or when using uncommon arguments, because it
reduces the risk of specifying the wrong argument, and makes your
code easier to read.
11
Named storage: Graphics Example
The amounts of time spent by a person watching 4 different types of TV
shows were measured.
15% of the time was spent on sports, 10% on game shows, 30% on
movies, and 45% on comedies. Set up a pie chart and a bar chart.
12
Named storage: Graphics Example
First, you need to set up an object that contains the information to be
plotted. Here, an object called tv is assigned the required information.
tv <- c("sports" = 15, "game shows"= 10,
"movies" = 30, "comedies" = 45)
The pie() function can then be used to create the pie chart.
13
Named storage: Graphics Example
pie(tv)
game shows
movies
sports
comedies
14
Named storage: Bar Chart Example
barplot(tv)
40
30
20
10
0
sports game shows movies comedies
15
Polling question - Yes or No?
My friend has baked 3 apple pies, 4 blueberry pies and 7 cherry pies.
Create a pie chart for these data. The first step is:
pies <- c(apple = 3, blueberry = 4, cherry = 7)
16
Polling question - Answer
No! Don’t forget the quotation marks.
pies <- c("apple" = 3, "blueberry" = 4, "cherry" = 7)
17
Polling question - Yes or No?
The second step to create the pie chart is for my friend’s pie data is
pie(pies)
18
Polling question - Answer
Yes!
pie(pies)
blueberry
apple
cherry
19
R is case-sensitive
Let’s try to find the length of the longest river:
MAX(rivers)
## Error in MAX(rivers): could not find function "MAX"
Now try
max(rivers)
## [1] 3710
The function max() is built in to R. R would consider MAX to be a
different function, because it is case-sensitive: m is different from M.
20
R is case-sensitive
If you really want a function called MAX to do the work of max, you would
type
MAX <- max
Now, MAX will do what you want:
MAX(rivers)
## [1] 3710
21
Listing the objects in the workspace
Our workspace now contains some objects that we have created.
A list of all objects in the current workspace can be printed to the screen
using the objects() function:
objects()
## [1] "MAX" "pies" "tv"
The same result is obtained with the alias function
ls()
## [1] "MAX" "pies" "tv"
Remember that if we quit our R session without saving the workspace
image, then these objects will disappear.
22
The simplest model for random noise - the runif function
Suppose you are measuring a length with a ruler that gives you
accuracy to the nearest millimeter.
When you measure the length of your pencil as 273 millimeters, the truth
could be anywhere between 272.5 and 273.5 millimeters.
The uniform distribution on the interval [−.5, .5] provides a model for the
error in your measurement, and we can simulate values from this
distribution using the runif() function.
Following is a sample of 4 such simulated values:
runif(4, min = -.5, max = .5)
## [1] -0.05448498 0.32835214 0.46347435 0.47992511
23
Packages
One of the major strengths of R is the availability of add-on packages
that have been created by statisticians and computer scientists from
around the world.
There are thousands of packages, e.g. graphics, ggplot2, and MPV.
A package contains functions and data which extend the abilities of R.
Every installation of R contains a number of packages by default (e.g.
base, stats, and graphics) which are automatically loaded when you
start R.
24
Packages
To load an additional package, for example, called DAAG, type
library(DAAG)
If you get a warning that the package is can’t be found, then the package
doesn’t exist on your computer, but it can likely be installed. Try
install.packages("DAAG")
25
Packages
In RStudio, it may be simpler to use the Tools menu.
26
Packages
Choose “Install Packages”:
27
Packages
Type in the name of the package you are requesting, and click “Install”:
28
Packages
Before DAAG is installed, if we type;
> seedrates
Error: object 'seedrates' not found
Once DAAG is installed, it can be loaded using the library() function,
and you can access data frames and functions that were not avaiable
previously. For example, the seedrates data frame is now available:
seedrates
## rate grain
## 1 50 21.2
## 2 75 19.9
## 3 100 19.2
## 4 125 18.4
## 5 150 17.9
29
Polling question - Yes or No?
To install the MPV package, type
install.packages(MPV)
30
Polling question - Answer
No! We forgot the quotation marks again.
install.packages("MPV")
31
Using one object from a package at a time
MPV::p2.12
## temp usage
## 1 21 185.79
The MPV package is installed on my
## 2 24 214.47
system, but I have not loaded it. I
## 3 32 288.03
only want to access the p2.12 data
## 4 47 424.84
frame and nothing else.
## 5 50 454.68
## 6 59 539.03
To do this, just type the package ## 7 68 621.55
name (MPV), followed by two colons ## 8 74 675.06
(::) and the object name you seek. ## 9 62 562.03
## 10 50 452.93
## 11 41 369.95
## 12 30 273.98
32
Polling question - Yes or No?
If I don’t load the DAAG package, but I want to assign seedrates to an
object called plantingData, the following should work:
plantingData <- DAAG::seedrates
33
Polling question - Answer
Yes!
plantingData <- DAAG::seedrates
Look at the result:
plantingData
The contents of
## rate grain
plantingData
## 1 50 21.2
are the same as
## 2 75 19.9
the contents of
## 3 100 19.2
seedrates.
## 4 125 18.4
## 5 150 17.9
34
Packages
You might want to know which packages are loaded into your system
already.
To see which packages are loaded, run
search()
## [1] ".GlobalEnv" "package:DAAG" "package:stat
## [4] "package:graphics" "package:grDevices" "package:util
## [7] "package:datasets" "package:methods" "Autoloads"
## [10] "package:base"
(Your list will likely be different from mine.)
35
Packages
This list also indicates the search order: a package can only contain one
function of any given name, but the same name may be used in another
package.
When you use that function, R will choose it from the first package in the
search list.
If you want to force a function to be chosen from a particular package,
prefix the name of the function with the name of the package and ::,
e.g.
stats::median(x)
36
Packages
Thousands of contributed packages are available, though you likely
have only a few dozen installed on your computer.
If you try to use one that isn’t already there, you will receive an error
message:
library(notInstalled)
## Error in library(notInstalled): there is no package
called ’notInstalled’
This means that the package doesn’t exist on your computer, but it
might be available in a repository online.
37
Packages
The biggest repository of R packages is known as CRAN. To install a
package from CRAN, you can run a command like
install.packages("knitr")
or, within RStudio, click on the Packages tab in the Output Pane,
choose Install, and enter the name in the resulting dialog box.
38
Packages
Because there are so many contributed packages, it is hard to know
which one to use to solve your own problems.
If you can’t get help from someone with more experience, you can get
information from the CRAN task views at
https://cloud.r-project.org/web/views.
These are reviews of available packages written by experts in dozens of
different subject areas.
39