Are you ready for Shiny?
You should know a little about R before you learn Shiny. Shiny is not a substitute for the R language, but a way to extend R to a new domain, interactive web apps.
But how much R do you need to know?
This quiz will help you decide whether you know enough about R to feel confident with Shiny. You are ready to make the most of Shiny if you can answer each of the questions below.
1. Common Errors
qplot() is a function that comes in the
ggplot2 package in R. You can use
qplot() to create quick scatterplots if you pass
qplot() two variable names and the name of the data set that contains the variables.
The code below is a correctly written
qplot() call; but if you copy and paste the code into R, you will get an error message when you run the code. Why?
qplot() is defined in the
ggplot2 package, which means that you will not be able to use
qplot() until you install and load the
ggplot2 package. If you try to use
qplot() without loading the
ggplot2 package, R will return the error
You can install the
ggplot2 package from CRAN with
Once you’ve installed
ggplot2 you can run the command above with
You will need to load
library() once in each new R session that you would like to use
2. More Common Errors
You can assign values to R objects. For example, you could assign the age of your cat to an object named
cat like this,
cat <- 4.
Suppose you ran the code below and received the error message that follows. What does it mean?
The error message suggests that you have not yet run
cat <- 4. In this case, R will evaluate the expression with the object named
cat that comes in base R. This object is a function. Since
+ does not know how to operate on functions, it returns an error message.
Compare this to
Notice that the error message would have been more clear had we used a different object name, one that is not already used by base R, e.g.
But in practice it is difficult to tell which names are already in use and which are not.
3. One More Common Error
The code below creates a function that returns a list. Assume that you run the code.
You can call the function and immediately subset its result with R’s dollar sign syntax. However, the code below will fail to do this. Why does the code fail, and how can you fix it?
You can subset the result of
make_list(), but not
When you run the name of an object in R, R will show you the contents of the object. If the object is a function, this content will be the code in the function body, e.g.
This body of code is not subsettable so
To return the result of a function in R, you must follow the function name with a pair of parentheses,
(). If you add these parentheses to the call above, R will use
$time to subset the list returned by
make_fun(), which produces a valid result.
The code below creates a list object.
What will each of these return? What type of object will each be?
lst[] will return the first element of
lst as a vector of integers.
lst will return a new list that has one element – the first element of
In summary, subsetting a list with dollar signs or double brackets will return elements of the list just as they are. Subsetting with single brackets will return the elements as part of a new list. This is an important distinction because many R functions cannot work with an element when it is contained in a list.
5. Data frames
Here is a data frame that comes with R. How can you calculate the sum of its
You can calculate the sum with
pressure$temperature returns the
temperature column of the
pressure data frame as a vector. The
sum function calculates the sum of the vector.
How can you make a scatterplot of the
pressure data? The plot should show temperature on the x axis and pressure on the y axis.
You can draw a scatterplot with the
Or you could use the
qplot function in the
You can make a scatterplot in R with other functions as well. As long as you know at least one way to visualize your data, you will be able to include visualizations in your Shiny apps.
7. Missing values
Suppose I change the first temperature value to
NA, which stands for a missing value.
sum(pressure$temperature) return? How can I ask
sum to ignore the
sum will return an
NA because it no longer has enough information to calculate the sum of the column. You can avoid this by including the argument
na.rm = TRUE. Then
sum will return the sum of all of the elements that do not equal
Write a function that can take a vector of numbers as input, and return the mean of the numbers as output. Recall that the mean of a vector is the sum of the vector divided by the length of the vector.
Your function should look and work like this.
To make the most of Shiny, you should feel comfortable writing your own functions in R.
What will this code return?
The code returns
f returns the last line of its function body,
c(x, y), which uses an object named
x. There is no object named
x defined in the function body or arguments of
f looks in the environment where
f was defined. There it finds an object named
x that is equal to 1.
What will the code below return?
The code will return
change_obj creates a copy of
obj in its local environment when it runs, and it gives that copy the value
2. However, this does not affect the verison of
obj that lives in the global workspace. As a result,
obj still returns
1 after you run
As a general rule in R, the value of an object will not change when you change a copy of the object. However, there are some exceptions to this rule. For example, reference class objects (also known as RC or R5 objects) will change when a copy of the object changes. The Shiny package uses this behavior, but you should realize that it is not common in other parts of the R language.
How would you install and load the
shiny package so that you can use it in your R session? How often will you need to install the package? How often will you need to load it?
You can install the Shiny package by running the R command
R will then download Shiny from cran.r-project.org and install it on your hard drive (so you will need to be connected to the internet when you install the package).
You only need to install a package once, but you may wish to reinstall the package when an updated version becomes available.
To use the Shiny package, you will need to load it with
You’ll need to reload Shiny each time you start a new R session (if you want to use Shiny in that R session).
12. Working directory
What is your working directory and how can you change it?
Your working directory is a folder on your computer that R associates itself with. When you ask R to open a file, it will look for the file in the working directory. When you ask R to save a file, it will save the file in the working directory. In general, R will interpret file paths as if they begin in the working directory. You can prevent this by using full file paths that start at your root directory.
You can learn the location of your working directory with
You can change your working directory with
setwd with a file path that leads to the new working directory, e.g.
You can also set your working directory with the RStudio menu command Session > Set Working Directory > Choose Directory…
or with the More > Set as Working Directory option in RStudio’s Files tab.
What is an R script? How can you “source” one, and what will that do?
An R script is a text file that contains R code.
When you “source” an R script, R reads the file and runs all of the code in the script. You can source an R script with the
source command. Give
source the filepath to the script you wish to source, e.g.
You can also source a script by opening it in RStudio’s Scripts pane and then clicking the “Source” icon in the top right corner of the pane.
If you stumbled on these questions, you may find learning Shiny to be frustrating or confusing. But don’t feel glum, R is easy to learn!
If you answered all of the questions above, you’re ready to go! A good way to learn Shiny is with our online tutorial.