-
Notifications
You must be signed in to change notification settings - Fork 19
/
rprog-scripts-submitscript1.R
executable file
·280 lines (266 loc) · 14.6 KB
/
rprog-scripts-submitscript1.R
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# This is a modification of the original submit script for R Programming Assignment 1
# which allows for more convenient submissions by allowing you to submit multiple parts
# at once and being able to choose to submit additional parts before exiting.
# The test cases are identical as of July 2015, but that's not guaranteed to be the
# case so you should compare to the original to be sure. Also, if you compare these
# you'll see what changes you can make to the submit scripts for the following
# assignments to have the same convenience with those.
submit <- local({
getOutput <- function(sid) {
## JUST FOR TESTING
## sid <- sub("-dev", "", sid, fixed = TRUE)
if(sid == "pollutantmean-1") {
source("pollutantmean.R")
pollutantmean("specdata", "sulfate", 1:10)
}
else if(sid == "pollutantmean-2") {
source("pollutantmean.R")
pollutantmean("specdata", "nitrate", 70:72)
}
else if(sid == "pollutantmean-3") {
source("pollutantmean.R")
pollutantmean("specdata", "sulfate", 34)
}
else if(sid == "pollutantmean-4") {
source("pollutantmean.R")
pollutantmean("specdata", "nitrate")
}
else if(sid == "complete-1") {
source("complete.R")
cc <- complete("specdata", c(6, 10, 20, 34, 100, 200, 310))
paste(cc$nobs, collapse = "\n")
}
else if(sid == "complete-2") {
source("complete.R")
cc <- complete("specdata", 54)
cc$nobs
}
else if(sid == "complete-3") {
source("complete.R")
set.seed(42)
cc <- complete("specdata", 332:1)
use <- sample(332, 10)
paste(cc[use, "nobs"], collapse = "\n")
}
else if(sid == "corr-1") {
source("corr.R")
cr <- corr("specdata")
cr <- sort(cr)
set.seed(868)
out <- round(cr[sample(length(cr), 5)], 4)
paste(out, collapse = "\n")
}
else if(sid == "corr-2") {
source("corr.R")
cr <- corr("specdata", 129)
cr <- sort(cr)
n <- length(cr)
set.seed(197)
out <- c(n, round(cr[sample(n, 5)], 4))
paste(out, collapse = "\n")
}
else if(sid == "corr-3") {
source("corr.R")
cr <- corr("specdata", 2000)
n <- length(cr)
cr <- corr("specdata", 1000)
cr <- sort(cr)
paste(c(n, round(cr, 4)), collapse = "\n")
}
else {
stop("invalid part number")
}
}
partlist <- list("pollutantmean-1" = "'pollutantmean' part 1",
"pollutantmean-2" = "'pollutantmean' part 2",
"pollutantmean-3" = "'pollutantmean' part 3",
"pollutantmean-4" = "'pollutantmean' part 4",
"complete-1" = "'complete' part 1",
"complete-2" = "'complete' part 2",
"complete-3" = "'complete' part 3",
"corr-1" = "'corr' part 1",
"corr-2" = "'corr' part 2",
"corr-3" = "'corr' part 3"
)
partPrompt <- function() {
pretty_out("Which part(s) are you submitting?")
part <- select.list(partlist, multiple=TRUE, graphics=FALSE)
names(part)
}
promptAgain <- function() {
pretty_out("Would you like to submit another part?")
again <- select.list(list("TRUE" = "Yes", "FALSE" = "No"), graphics = FALSE)
names(again)
}
getChallenge <- function(email, challenge.url) {
params <- list(email_address = email,
response_encoding = "delim")
result <- getForm(challenge.url, .params = params)
s <- strsplit(result, "|", fixed = TRUE)[[1]]
list(ch.key = s[5], state = s[7])
}
challengeResponse <- function(password, ch.key) {
x <- paste(ch.key, password, sep = "")
digest(x, algo = "sha1", serialize = FALSE)
}
submitSolution <- function(email, ch.resp, sid, output, signature, submit.url,
src = "", http.version = NULL) {
output <- as.character(base64(output))
src <- as.character(base64(src))
params <- list(assignment_part_sid = sid,
email_address = email,
submission = output,
submission_aux = src,
challenge_response = ch.resp,
state = signature)
params <- lapply(params, URLencode)
result <- postForm(submit.url, .params = params)
s <- strsplit(result, "\\r\\n")[[1]]
tail(s, 1)
}
get_courseid <- function() {
pretty_out("The first item I need is your Course ID. For example, if the",
"homepage for your Coursera course was",
"'https://class.coursera.org/rprog-001',",
"then your course ID would be 'rprog-001' (without the quotes).", skip_after=TRUE)
repeat {
courseid <- readline("Course ID: ")
## Remove quotes if there are any
courseid <- gsub("\'|\"", "", courseid)
## Set up test cases
is_url <- str_detect(courseid, "www[.]|http:|https:")
is_numbers <- str_detect(courseid, "^[0-9]+$")
is_example <- str_detect(courseid, fixed("rprog-001"))
## Check if courseid is none of the bad things
if(!any(is_url, is_numbers, is_example)){
break
## courseid is one of the bad things
} else {
## Check if courseid is a url
if(is_url) {
pretty_out("It looks like you entered a web address, which is not what I'm",
"looking for.")
}
## Check if courseid is all numbers
if(is_numbers) {
pretty_out("It looks like you entered a numeric ID, which is not what I'm",
"looking for.")
}
## Check if the user stole the example courseid
if(is_example) {
pretty_out("It looks like you entered the Course ID that I used as an",
"example, which is not what I'm looking for.")
}
}
pretty_out("Instead, I need your Course ID, which is the last",
"part of the web address for your Coursera course.",
"For example, if the homepage for your Coursera course was",
"'https://class.coursera.org/rprog-001',",
"then your course ID would be 'rprog-001' (without the quotes).",
skip_after=TRUE)
}
courseid
}
pretty_out <- function(..., skip_before=TRUE, skip_after=FALSE) {
wrapped <- strwrap(str_c(..., sep = " "),
width = getOption("width") - 2)
mes <- str_c("| ", wrapped, collapse = "\n")
if(skip_before) mes <- paste0("\n", mes)
if(skip_after) mes <- paste0(mes, "\n")
message(mes)
}
checkPkgs <- function() {
pkg.inst <- installed.packages()
pkgs <- c("RCurl", "digest", "stringr")
have.pkg <- pkgs %in% rownames(pkg.inst)
if(any(!have.pkg)) {
message("\nSome packages need to be installed.\n")
r <- readline("Install necessary packages [y/n]? ")
if(tolower(r) == "y") {
need <- pkgs[!have.pkg]
message("\nInstalling packages ",
paste(need, collapse = ", "))
install.packages(need)
}
}
}
loginPrompt <- function() {
courseid <- get_courseid()
email <- readline("Submission login (email): ")
passwd <- readline("Submission password: ")
r <- list(courseid = courseid, email = email, passwd = passwd)
assign(".CourseraLogin", r, globalenv())
invisible(r)
}
function(manual = FALSE, resetLogin = FALSE) {
checkPkgs()
suppressPackageStartupMessages(library(RCurl))
library(digest)
library(stringr)
readline("\nPress Enter to continue...")
if(!manual) {
confirmed <- FALSE
need2fix <- FALSE
while(!confirmed) {
if(exists(".CourseraLogin") && !resetLogin && !need2fix)
cred <- get(".CourseraLogin")
else
cred <- loginPrompt()
if(!is.list(cred) || !(names(cred) %in% c("email", "passwd", "courseid")))
stop("problem with login/password")
courseid <- cred$courseid
email <- cred$email
password <- cred$passwd
pretty_out("Is the following information correct?",
skip_after=TRUE)
message("Course ID: ", courseid,
"\nSubmission login (email): ", email,
"\nSubmission password: ", password)
yn <- c("Yes, go ahead!",
"No, I need to change something.")
confirmed <- identical(select.list(yn, graphics=FALSE), yn[1])
if(!confirmed) need2fix <- TRUE
}
## Set urls based on confirmed courseid
challenge.url <- paste("http://class.coursera.org", courseid,
"assignment/challenge", sep = "/")
submit.url <- paste("http://class.coursera.org", courseid,
"assignment/submit", sep = "/")
}
## Prompt Submission Parts (multiple select allowed)
sids <- partPrompt()
for (sid in sids) {
## Get output
output <- getOutput(sid)
if(!manual) {
## Get challenge
ch <- try(getChallenge(email, challenge.url), silent=TRUE)
## Check if url is valid, i.e. challenge received
ch_ok <- !is(ch, "try-error") && exists("ch.key", ch) && !is.na(ch$ch.key)
if(!ch_ok) {
stop("Either the course ID you entered is not valid or your course site ",
"is unreachable at this time. If you'd like to submit manually, you ",
"can run submit(manual=TRUE).")
}
## Attempt submission with challenge
ch.resp <- challengeResponse(password, ch$ch.key)
results <- submitSolution(email, ch.resp, sid, output, ch$state,
submit.url = submit.url)
if(!length(results))
results <- "Incorrect!"
cat("Result for", partlist[[sid]], ":", results, "\n")
}
else {
outfile <- paste(sid, "output.txt", sep = "-")
writeLines(as.character(output), outfile)
cat(sprintf("Please upload the file '%s' to Coursera\n",
outfile))
}
}
## Prompt if user wants to submit again
if (promptAgain()) {
submit(manual, resetLogin)
}
invisible()
}
})