Skip to content

Commit 0d7d728

Browse files
committed
first commit
0 parents  commit 0d7d728

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+10958
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

Base_Graphics/customTests.R

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
notify <- function() {
2+
e <- get("e", parent.frame())
3+
if(e$val == "No") return(TRUE)
4+
5+
good <- FALSE
6+
while(!good) {
7+
# Get info
8+
name <- readline_clean("What is your full name? ")
9+
address <- readline_clean("What is the email address of the person you'd like to notify? ")
10+
11+
# Repeat back to them
12+
message("\nDoes everything look good?\n")
13+
message("Your name: ", name, "\n", "Send to: ", address)
14+
15+
yn <- select.list(c("Yes", "No"), graphics = FALSE)
16+
if(yn == "Yes") good <- TRUE
17+
}
18+
19+
# Get course and lesson names
20+
course_name <- attr(e$les, "course_name")
21+
lesson_name <- attr(e$les, "lesson_name")
22+
23+
subject <- paste(name, "just completed", course_name, "-", lesson_name)
24+
body = ""
25+
26+
# Send email
27+
swirl:::email(address, subject, body)
28+
29+
hrule()
30+
message("I just tried to create a new email with the following info:\n")
31+
message("To: ", address)
32+
message("Subject: ", subject)
33+
message("Body: <empty>")
34+
35+
message("\nIf it didn't work, you can send the same email manually.")
36+
hrule()
37+
38+
# Return TRUE to satisfy swirl and return to course menu
39+
TRUE
40+
}
41+
42+
readline_clean <- function(prompt = "") {
43+
wrapped <- strwrap(prompt, width = getOption("width") - 2)
44+
mes <- stringr::str_c("| ", wrapped, collapse = "\n")
45+
message(mes)
46+
readline()
47+
}
48+
49+
hrule <- function() {
50+
message("\n", paste0(rep("#", getOption("width") - 2), collapse = ""), "\n")
51+
}

Base_Graphics/lesson.yaml

+215
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
- Class: meta
2+
Course: R Programming
3+
Lesson: Base Graphics
4+
Author: David Kane
5+
Type: Standard
6+
Organization: Williams College
7+
Version: 0.1.1
8+
9+
- Class: text
10+
Output: One of the greatest strengths of R, relative to other programming languages, is the ease with which we can create publication-quality graphics. In this lesson, you'll learn about base graphics in R.
11+
12+
- Class: text
13+
Output: We do not cover the more advanced portions of graphics in R in this lesson. These include lattice, ggplot2 and ggvis.
14+
15+
- Class: text
16+
Output: There is a school of thought that this approach is backwards, that we should teach ggplot2 first. See http://varianceexplained.org/r/teach_ggplot2_to_beginners/ for an outline of this view.
17+
18+
- Class: cmd_question
19+
Output: Load the included data frame cars with data(cars).
20+
CorrectAnswer: data(cars)
21+
AnswerTests: omnitest(correctExpr='data(cars)')
22+
Hint: Type data(cars) to load the data.
23+
24+
- Class: text
25+
Output: To fix ideas, we will work with simple data frames. Our main goal is to introduce various plotting functions and their arguments. All the output would look more interesting with larger, more complex data sets.
26+
27+
- Class: cmd_question
28+
Output: Pull up the help page for cars.
29+
CorrectAnswer: ?cars
30+
AnswerTests: any_of_exprs('?cars', 'help(cars)', 'help("plot")')
31+
Hint: Type ?cars or help(cars) to view a help page with details on the car data frame.
32+
33+
- Class: text
34+
Output: "As you can see in the help page, the cars data set has only two variables: speed and stopping distance. Note that the data is from the 1920s."
35+
36+
- Class: cmd_question
37+
Output: Run head() on the cars data.
38+
CorrectAnswer: head(cars)
39+
AnswerTests: omnitest(correctExpr='head(cars)')
40+
Hint: Type head(cars) to see the top of the cars data frame.
41+
42+
- Class: text
43+
Output: Before plotting, it is always a good idea to get a sense of the data. Key R commands for doing so include, dim(), names(), head(), tail() and summary().
44+
45+
- Class: cmd_question
46+
Output: Run the plot() command on the cars data frame.
47+
CorrectAnswer: plot(cars)
48+
AnswerTests: omnitest(correctExpr='plot(cars)')
49+
Hint: Type plot(cars) to create a plot of the cars data frame.
50+
51+
- Class: text
52+
Output: As always, R tries very hard to give you something sensible given the information that you have provided to it. First, R notes that the data frame you have given it has just two columns, so it assumes that you want to plot one column versus the other.
53+
54+
- Class: text
55+
Output: Second, since we do not provide labels for either axis, R uses the names of the columns. Third, it creates axis tick marks at nice round numbers and labels them accordingly. Fourth, it uses the other defaults supplied in plot().
56+
57+
- Class: text
58+
Output: We will now spend some time exploring plot, but many of the topics covered here will apply to most other R graphics functions. Note that 'plot' is short for scatterplot.
59+
60+
- Class: cmd_question
61+
Output: Look up the help page for plot().
62+
CorrectAnswer: ?plot
63+
AnswerTests: any_of_exprs('?plot', 'help(plot)')
64+
Hint: Type ?plot or help(plot) to view a help page for plot().
65+
66+
- Class: text
67+
Output: The help page for plot() highlights the different arguments that the function can take. The two most important are x and y, the variables that will be plotted. For the next set of questions, include the argument names in your answers. That is, do not type plot(cars$speed, cars$dist), although that will work. Instead, use plot(x = cars$speed, y = cars$dist).
68+
69+
- Class: cmd_question
70+
Output: Use plot() command to show speed on the x-axis and dist on the y-axis from the cars data frame. Use the form of the plot command in which vectors are explicitly passed in as arguments for x and y.
71+
CorrectAnswer: plot(x = cars$speed, y = cars$dist)
72+
AnswerTests: omnitest(correctExpr='plot(x = cars$speed, y = cars$dist)')
73+
Hint: Type plot(x = cars$speed, y = cars$dist) to create the plot.
74+
75+
- Class: text
76+
Output: Note that this produces a slightly different answer than plot(cars). In this case, R is not sure what you want to use as the labels on the axes, so it just uses the arguments which you pass in, data frame name and dollar signs included.
77+
78+
- Class: text
79+
Output: Note that there are other ways to call the plot command, i.e., using the "formula" interface. For example, we get a similar plot to the above with plot(dist ~ speed, cars). However, we will wait till later in the lesson before using the formula interface.
80+
81+
- Class: cmd_question
82+
Output: Use plot() command to show dist on the x-axis and speed on the y-axis from the cars data frame. This is the opposite of what we did above.
83+
CorrectAnswer: plot(x = cars$dist, y = cars$speed)
84+
AnswerTests: omnitest(correctExpr='plot(x = cars$dist, y = cars$speed)')
85+
Hint: Type plot(x = cars$dist, y = cars$speed) to create the plot.
86+
87+
- Class: text
88+
Output: It probably makes more sense for speed to go on the x-axis since stopping distance is a function of speed more than the other way around. So, for the rest of the questions in this portion of the lesson, always assign the arguments accordingly.
89+
90+
- Class: text
91+
Output: In fact, you can assume that the answers to the next few questions are all of the form plot(x = cars$speed, y = cars$dist, ...) but with various arguments used in place of the ...
92+
93+
- Class: cmd_question
94+
Output: Recreate the plot with the label of the x-axis set to "Speed".
95+
CorrectAnswer: plot(x = cars$speed, y = cars$dist, xlab = "Speed")
96+
AnswerTests: omnitest(correctExpr='plot(x = cars$speed, y = cars$dist, xlab = "Speed")')
97+
Hint: Type plot(x = cars$speed, y = cars$dist, xlab = "Speed") to create the plot.
98+
99+
- Class: cmd_question
100+
Output: Recreate the plot with the label of the y-axis set to "Stopping Distance".
101+
CorrectAnswer: plot(x = cars$speed, y = cars$dist, ylab = "Stopping Distance")
102+
AnswerTests: omnitest(correctExpr='plot(x = cars$speed, y = cars$dist, ylab = "Stopping Distance")')
103+
Hint: Type plot(x = cars$speed, y = cars$dist, ylab = "Stopping Distance") to create the plot.
104+
105+
- Class: cmd_question
106+
Output: Recreate the plot with "Speed" and "Stopping Distance" as axis labels.
107+
CorrectAnswer: plot(x = cars$speed, y = cars$dist, xlab = "Speed", ylab = "Stopping Distance")
108+
AnswerTests: omnitest(correctExpr='plot(x = cars$speed, y = cars$dist, xlab = "Speed", ylab = "Stopping Distance")')
109+
Hint: Type plot(x = cars$speed, y = cars$dist, xlab = "Speed", ylab = "Stopping Distance") to create the plot.
110+
111+
- Class: text
112+
Output: The reason that plots(cars) worked at the beginning of the lesson was that R was smart enough to know that the first element (i.e., the first column) in cars should be assigned to the x argument and the second element to the y argument. To save on typing, the next set of answers will all be of the form, plot(cars, ...) with various arguments added.
113+
114+
- Class: text
115+
Output: For each question, we will only want one additional argument at a time. Of course, you can pass in more than one argument when doing a real project.
116+
117+
- Class: cmd_question
118+
Output: Plot cars with a main title of "My Plot". Note that the argument for the main title is "main" not "title".
119+
CorrectAnswer: plot(cars, main = "My Plot")
120+
AnswerTests: omnitest(correctExpr='plot(cars, main = "My Plot")')
121+
Hint: Type plot(cars, main = "My Plot") to create the plot.
122+
123+
- Class: cmd_question
124+
Output: Plot cars with a sub title of "My Plot Subtitle".
125+
CorrectAnswer: plot(cars, sub = "My Plot Subtitle")
126+
AnswerTests: omnitest(correctExpr='plot(cars, sub = "My Plot Subtitle")')
127+
Hint: Type plot(cars, sub = "My Plot Subtitle") to create the plot.
128+
129+
- Class: text
130+
Output: The plot help page (?plot) only covers a small number of the many arguments that can be passed in to plot() and to other graphical functions. To begin to explore the many other options, look at ?par. Let's look at some of the more commonly used ones. Continue using plot(cars, ...) as the base answer to these questions.
131+
132+
- Class: cmd_question
133+
Output: Plot cars so that the plotted points are colored red. (Use col = 2 to achieve this effect.)
134+
CorrectAnswer: plot(cars, col = 2)
135+
AnswerTests: omnitest(correctExpr='plot(cars, col = 2)')
136+
Hint: Type plot(cars, col = 2) to create the plot.
137+
138+
- Class: cmd_question
139+
Output: Plot cars while limiting the x-axis to 10 through 15. (Use xlim = c(10, 15) to achieve this effect.)
140+
CorrectAnswer: plot(cars, xlim = c(10, 15))
141+
AnswerTests: omnitest(correctExpr='plot(cars, xlim = c(10, 15))')
142+
Hint: Type plot(cars, xlim = c(10, 15)) to create the plot.
143+
144+
- Class: text
145+
Output: You can also change the shape of the symbols in the plot. The help page for points (?points) provides the details.
146+
147+
- Class: cmd_question
148+
Output: Plot cars using triangles. (Use pch = 2 to achieve this effect.)
149+
CorrectAnswer: plot(cars, pch = 2)
150+
AnswerTests: omnitest(correctExpr='plot(cars, pch = 2)')
151+
Hint: Type plot(cars, pch = 2) to create the plot.
152+
153+
- Class: text
154+
Output: Arguments like "col" and "pch" may not seem very intuitive. And that is because they aren't! So, many/most people use more modern packages, like ggplot2, for creating their graphics in R.
155+
156+
- Class: text
157+
Output: It is, however, useful to have an introduction to base graphics because many of the idioms in lattice and ggplot2 are modeled on them.
158+
159+
- Class: text
160+
Output: Let's now look at some other functions in base graphics that may be useful, starting with boxplots.
161+
162+
- Class: cmd_question
163+
Output: Load the mtcars data frame.
164+
CorrectAnswer: data(mtcars)
165+
AnswerTests: omnitest(correctExpr='data(mtcars)')
166+
Hint: Type data(mtcars) to load the data.
167+
168+
- Class: text
169+
Output: Anytime that you load up a new data frame, you should explore it before using it. In the middle of a swirl lesson, just type play(). This temporarily suspends the lesson (without losing the work you have already done) and allows you to issue commands like dim(mtcars) and head(mtcars). Once you are done examining the data, just type nxt() and the lesson will pick up where it left off.
170+
171+
- Class: cmd_question
172+
Output: Look up the help page for boxplot().
173+
CorrectAnswer: ?boxplot
174+
AnswerTests: any_of_exprs('?boxplot', 'help(boxplot)')
175+
Hint: Type ?boxplot or help(boxplot) to view a help page with details about boxplot.
176+
177+
- Class: text
178+
Output: Instead of adding data columns directly as input arguments, as we did with plot(), it is often handy to pass in the entire data frame. This is what the "data" argument in boxplot() allows.
179+
180+
- Class: text
181+
Output: boxplot(), like many R functions, also takes a "formula" argument, generally an expression with a tilde ("~") which indicates the relationship between the input variables. This allows you to enter something like mpg ~ cyl to plot the relationship between cyl (number of cylinders) on the x-axis and mpg (miles per gallon) on the y-axis.
182+
183+
- Class: cmd_question
184+
Output: Use boxplot() with formula = mpg ~ cyl and data = mtcars to create a box plot.
185+
CorrectAnswer: boxplot(formula = mpg ~ cyl, data = mtcars)
186+
AnswerTests: omnitest(correctExpr='boxplot(formula = mpg ~ cyl, data = mtcars)')
187+
Hint: Type boxplot(formula = mpg ~ cyl, data = mtcars) to create the plot.
188+
189+
- Class: text
190+
Output: The plot shows that mpg is much lower for cars with more cylinders. Note that we can use the same set of arguments that we explored with plot() above to add axis labels, titles and so on.
191+
192+
- Class: text
193+
Output: When looking at a single variable, histograms are a useful tool. hist() is the associated R function. Like plot(), hist() is best used by just passing in a single vector.
194+
195+
- Class: cmd_question
196+
Output: Use hist() with the vector mtcars$mpg to create a histogram.
197+
CorrectAnswer: hist(mtcars$mpg)
198+
AnswerTests: any_of_exprs('hist(mtcars$mpg)', 'hist(x = mtcars$mpg)')
199+
Hint: Type hist(mtcars$mpg) to create the plot.
200+
201+
# Not sure what a good lesson length is for this.
202+
# Might add some information on saving plots.
203+
# Other functions that I use include identify().
204+
205+
- Class: text
206+
Output: In this lesson, you learned how to work with base graphics in R. The best place to go from here is to study the ggplot2 package. If you want to explore other elements of base graphics, then this web page (http://www.ling.upenn.edu/~joseff/rstudy/week4.html) provides a useful overview.
207+
208+
- Class: mult_question
209+
Output: Would you like to inform someone about your successful completion of
210+
this lesson via email?
211+
CorrectAnswer: NULL
212+
AnswerChoices: Yes; No
213+
AnswerTests: notify()
214+
Hint: NULL
215+

Basic_Building_Blocks/customTests.R

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
notify <- function() {
2+
e <- get("e", parent.frame())
3+
if(e$val == "No") return(TRUE)
4+
5+
good <- FALSE
6+
while(!good) {
7+
# Get info
8+
name <- readline_clean("What is your full name? ")
9+
address <- readline_clean("What is the email address of the person you'd like to notify? ")
10+
11+
# Repeat back to them
12+
message("\nDoes everything look good?\n")
13+
message("Your name: ", name, "\n", "Send to: ", address)
14+
15+
yn <- select.list(c("Yes", "No"), graphics = FALSE)
16+
if(yn == "Yes") good <- TRUE
17+
}
18+
19+
# Get course and lesson names
20+
course_name <- attr(e$les, "course_name")
21+
lesson_name <- attr(e$les, "lesson_name")
22+
23+
subject <- paste(name, "just completed", course_name, "-", lesson_name)
24+
body = ""
25+
26+
# Send email
27+
swirl:::email(address, subject, body)
28+
29+
hrule()
30+
message("I just tried to create a new email with the following info:\n")
31+
message("To: ", address)
32+
message("Subject: ", subject)
33+
message("Body: <empty>")
34+
35+
message("\nIf it didn't work, you can send the same email manually.")
36+
hrule()
37+
38+
# Return TRUE to satisfy swirl and return to course menu
39+
TRUE
40+
}
41+
42+
readline_clean <- function(prompt = "") {
43+
wrapped <- strwrap(prompt, width = getOption("width") - 2)
44+
mes <- stringr::str_c("| ", wrapped, collapse = "\n")
45+
message(mes)
46+
readline()
47+
}
48+
49+
hrule <- function() {
50+
message("\n", paste0(rep("#", getOption("width") - 2), collapse = ""), "\n")
51+
}

0 commit comments

Comments
 (0)