/
lesson.yaml
265 lines (222 loc) · 11 KB
/
lesson.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
- Class: meta
Course: R Programming
Lesson: Workspace and Files
Author: David Kane
Type: Standard
Organization: Williams College
Version: 0.1.2
- Class: text
Output: "In this lesson, you'll learn how to examine your local workspace in R
and begin to explore the relationship between your workspace and the file
system of your machine."
- Class: text
Output: "Because different operating systems have different conventions with
regards to things like file paths, the outputs of these commands may vary
across machines."
- Class: text
Output: However it's important to note that R provides a common API (a common
set of commands) for interacting with files, that way your code will work
across different kinds of computers.
- Class: text
Output: Let's jump right in so you can get a feel for how these special
functions work!
- Class: cmd_question
Output: Determine which directory your R session is using as its current
working directory using getwd().
CorrectAnswer: getwd()
AnswerTests: omnitest(correctExpr='getwd()')
Hint: Type getwd() to determine the current working directory.
- Class: cmd_question
Output: List all the objects in your local workspace using ls().
CorrectAnswer: ls()
AnswerTests: omnitest(correctExpr='ls()')
Hint: Type ls() to view all the objects in your local workspace.
- Class: text
Output: Some R commands are the same as their equivalents commands on Linux or
on a Mac. Both Linux and Mac operating systems are based on an operating
system called Unix. It's always a good idea to learn more about Unix!
- Class: cmd_question
Output: Assign 9 to x using x <- 9.
CorrectAnswer: x <- 9
AnswerTests: omnitest(correctExpr='x <- 9')
Hint: Type x <- 9 to assign 9 to x.
- Class: cmd_question
Output: Now take a look at objects that are in your workspace using ls().
CorrectAnswer: ls()
AnswerTests: omnitest(correctExpr='ls()')
Hint: Type ls() to view all the objects in your workspace.
- Class: cmd_question
Output: List all the files in your working directory using list.files() or
dir().
CorrectAnswer: list.files()
AnswerTests: any_of_exprs('list.files()', 'dir()')
Hint: Type list.files() to all the files in your working directory.
- Class: cmd_question
Output: As we go through this lesson, you should be examining the help page
for each new function. Check out the help page for list.files with the
command ?list.files.
CorrectAnswer: ?list.files
AnswerTests: any_of_exprs('?list.files', '?dir', 'help("list.files")', 'help("dir")')
Hint: Type ?list.files to see the help page.
- Class: text
Output: One of the most helpful parts of any R help file is the See Also
section. Read that section for list.files. Some of these functions may be
used in later portions of this lesson.
- Class: text
Output: Using the args() function on a function name is also a handy way to
see what arguments a function can take.
- Class: cmd_question
Output: Use the args() function to determine the arguments to list.files().
CorrectAnswer: args(list.files)
AnswerTests: omnitest(correctExpr='args(list.files)')
Hint: Type args(list.files) to see the arguments to list.files.
- Class: cmd_question
Output: Assign the value of the current working directory to a variable
called "old.dir".
CorrectAnswer: old.dir <- getwd()
AnswerTests: omnitest(correctExpr='old.dir <- getwd()')
Hint: Type old.dir <- getwd() to assign the value of the current working
directory to a variable called "old.dir".
- Class: text
Output: We will use old.dir at the end of this lesson to move back to the
place that we started. A lot of query functions like getwd() have the
useful property that they return the answer to the question as a result of
the function.
- Class: cmd_question
Output: Use dir.create() to create a directory in the current working
directory called "testdir".
CorrectAnswer: dir.create("testdir")
AnswerTests: omnitest(correctExpr='dir.create("testdir")')
Hint: Type dir.create("testdir") to create a directory in the current working
directory called "testdir".
- Class: text
Output: We will do all our work in this new directory and then delete it after
we are done. This is the R analog to
"Take only pictures, leave only footprints."
- Class: cmd_question
Output: Set your working directory to "testdir" with the setwd() command.
CorrectAnswer: setwd("testdir")
AnswerTests: any_of_exprs('setwd("testdir")', 'setwd("testdir/")')
Hint: Use setwd("testdir") to set your working directory to "testdir".
- Class: text
Output: "In general, you will want your working directory to be someplace
sensible, perhaps created for the specific project that you are working on.
In fact, organizing your work in R packages using RStudio is an excellent
option. Check out RStudio at http://www.rstudio.com/"
- Class: cmd_question
Output: Create a file in your working directory called "mytest.R" using the
file.create() function.
CorrectAnswer: file.create("mytest.R")
AnswerTests: omnitest(correctExpr='file.create("mytest.R")')
Hint: file.create("mytest.R") will get the job done!
- Class: cmd_question
Output: "This should be the only file in this newly created directory. Let's
check this by listing all the files in the current directory."
CorrectAnswer: list.files()
AnswerTests: any_of_exprs('list.files()', 'dir()')
Hint: list.files() shows that the directory only contains mytest.R.
- Class: cmd_question
Output: Check to see if "mytest.R" exists in the working directory using the
file.exists() function.
CorrectAnswer: file.exists("mytest.R")
AnswerTests: omnitest(correctExpr='file.exists("mytest.R")')
Hint: Use file.exists("mytest.R") to check that a file exists.
- Class: text
Output: These sorts of functions are excessive for interactive use. But, if
you are running a program that loops through a series of files and does some
processing on each one, you will want to check to see that each exists before
you try to process it.
- Class: cmd_question
Output: Access information about the file "mytest.R" by using file.info().
CorrectAnswer: file.info("mytest.R")
AnswerTests: omnitest(correctExpr='file.info("mytest.R")')
Hint: file.info("mytest.R") will do the trick!
- Class: text
Output: You can use the $ operator --- e.g., file.info("mytest.R")$mode ---
to grab specific items.
- Class: cmd_question
Output: Change the name of the file "mytest.R" to "mytest2.R" by using
file.rename().
CorrectAnswer: file.rename("mytest.R", "mytest2.R")
AnswerTests: any_of_exprs('file.rename("mytest.R", "mytest2.R")', 'file.rename(from = "mytest.R", to = "mytest2.R")')
Hint: file.rename("mytest.R", "mytest2.R") is the answer. Googling
using key words from the question, along with the letter R, will often give
you a good hint.
- Class: text
Output: "Your operating system will provide simpler tools for these sorts
of tasks, but having the ability to manipulate files programatically
is useful. You might now try to delete mytest.R using
file.remove('mytest.R'), but that won't work since mytest.R no longer
exists. You have already renamed it."
- Class: cmd_question
Output: Make a copy of "mytest2.R" called "mytest3.R" using file.copy().
CorrectAnswer: file.copy("mytest2.R", "mytest3.R")
AnswerTests: any_of_exprs('file.copy("mytest2.R", "mytest3.R")', 'file.copy(from = "mytest2.R", to = "mytest3.R")')
Hint: file.copy("mytest2.R", "mytest3.R") works.
- Class: text
Output: You now have two files in the current directory. That may not seem
very interesting. But what if you were working with dozens, or millions,
of individual files? In that case, being able to programatically act on
many files would be absolutely necessary. Don't forget that you can,
temporarily, leave the lesson by typing play() and then
return by typing nxt().
- Class: cmd_question
Output: Provide the relative path to the file "mytest3.R" by using file.path().
CorrectAnswer: file.path("mytest3.R")
AnswerTests: omnitest(correctExpr='file.path("mytest3.R")')
Hint: file.path("mytest3.R") works.
- Class: cmd_question
Output: "You can use file.path to construct file and directory paths that are
independent of the operating system your R code is running on. Pass 'folder1'
and 'folder2' as arguments to file.path to make a platform-independent pathname."
CorrectAnswer: file.path("folder1", "folder2")
AnswerTests: omnitest(correctExpr='file.path("folder1", "folder2")')
Hint: file.path("folder1", "folder2") works.
- Class: cmd_question
Output: "Take a look at the documentation for dir.create by entering
?dir.create . Notice the 'recursive' argument. In order to create nested
directories, 'recursive' must be set to TRUE."
CorrectAnswer: ?dir.create
AnswerTests: omnitest(correctExpr='?dir.create')
Hint: ?dir.create will show you the docs.
- Class: cmd_question
Output: Create a directory in the current working directory called
"testdir2" and a subdirectory for it called "testdir3", all in one command
by using dir.create() and file.path().
CorrectAnswer: dir.create(file.path("testdir2", "testdir3"), recursive = TRUE)
AnswerTests: omnitest(correctExpr='dir.create(file.path("testdir2", "testdir3"), recursive = TRUE)')
Hint: "dir.create(file.path('testdir2', 'testdir3'), recursive = TRUE) will do
the trick. If you forgot the recursive argument, the command may have
appeared to work, but it didn't create the nested directory."
- Class: cmd_question
Output: Go back to your original working directory using setwd(). (Recall that we
created the variable old.dir with the full path for the orginal working
directory at the start of these questions.)
CorrectAnswer: setwd(old.dir)
AnswerTests: omnitest(correctExpr='setwd(old.dir)')
Hint: Use setwd(old.dir).
- Class: text
Output: "It is often helpful to save the settings that you had before you
began an analysis and then go back to them at the end. This trick is
often used within functions; you save, say, the par() settings that
you started with, mess around a bunch, and then set them back to the
original values at the end. This isn't the same as what we have done
here, but it seems similar enough to mention."
- Class: text
Output: "After you finish this lesson delete the 'testdir' directory that
you just left (and everything in it)"
- Class: text
Output: "Take nothing but results. Leave nothing but assumptions.
That sounds like 'Take nothing but pictures. Leave nothing but
footprints.' But it makes no sense! Surely our readers can come up
with a better motto . . ."
- Class: text
Output: "In this lesson, you learned how to examine your R workspace and
work with the file system of your machine from within R. Thanks for playing!"
- Class: mult_question
Output: "Would you like to receive credit for completing this course on
Coursera.org?"
CorrectAnswer: NULL
AnswerChoices: Yes;No
AnswerTests: coursera_on_demand()
Hint: ""