-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSW5.R
More file actions
615 lines (532 loc) · 15.9 KB
/
SW5.R
File metadata and controls
615 lines (532 loc) · 15.9 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
#Mendoza, Luigi G.
#1710040
#Entering Input
x<-1
print(x)
x
msg<-"hello"
#Evaluation
x<-5
x
print(x)
#Printing
x<-1:20
#Creating Vectors
x<-c(0.5,0.6)
x
x<-c(TRUE,FALSE)
x
x<-c(T,F)
x
x<-c("a","b","c")
x
x<-9:29
x
x<-c(1+0i,2+4i)
x
#Mixing Objects
y<-c(1.7,"a")
y
y<- c(TRUE,2)
y
y<-c("a",TRUE)
y
#Explicit Coercion1
x<-0:6
class(x)
as.numeric(x)
as.logical(x)
as.character(x)
#Explicit Coercion2
x<-c("a","b","c")
as.numeric(x)
as.logical(x)
as.complex(x)
#Lists
x<-list(1,"a",TRUE,1+4i)
#Matrices1
m<-matrix(nrow=2,ncol=3)
m
dim(m)
attributes(m)
#Matrices2
m<-matrix(1:6, nrow=2, ncol=3)
m
#Matrices3
m<-1:10
m
dim(m)<-c(2,5)
m
#cbing and rbing
x<-1:3
y<-10:12
cbind(x,y)
rbind(x,y)
#Factors1
x<-factor(c("yes","yes","no","yes","no"))
x
table(x)
unclass(x)
#Factors2
x<-factor(c("yes","yes","no","yes","no"),levels=c("yes","no"))
x
#Missing Values
x<-c(1,2,NA,10,3)
is.na(x)
is.nan(x)
x<-c(1,2,NaN,NA,4)
is.na(x)
is.nan(x)
#Data Frames
x<-data.frame(foo=1:4,bar=c(T,T,F,F))
x
nrow(x)
ncol(x)
#Names1
x<-1:3
names(x)
names(x)<-c("foo","bar","norf")
x
names(x)
#Names2
x<-list(a=1,b=2,c=3)
x
#Names3
m<-matrix(1:4,nrow=2,ncol=2)
dimnames(m)<-list(c("a","b"),c("c","d"))
m
#dput-ting R objects
y<-data.frame(a=1,b="a")
dput(y)
dput(y,file="y.R")
new.y<-dget("y.R")
new.y
#Dumping R objects
x<-"foo"
y<-data.frame(a=1,b="a")
dump(c("x","y"),file="data.R")
rm(x,y)
source("data.R")
y
x
#Subsetting
x<-c("a","b","c","c","d","a")
x[1]
x[2]
x[1:4]
x[x>"a"]
u<-x>"a"
u
x[u]
#Subsetting Lists1
x<-list(foo=1:4,bar=0.6)
x[1]
x[[1]]
x$bar
x[["bar"]]
#Subsetting Lists2
x<-list(foo=1:4,bar=0.6,baz="hello")
x[c(1,3)]
#Subsetting Lists3
x<-list(foo=1:4,bar=0.6,baz="hello")
name<-"foo"
x[[name]]
x$name
x$foo
#Subsetting Nested Elements of a List
x<-list(a=list(10,12,14),b=c(3.14,2.81))
x[[c(1,3)]]
x[[1]][[3]]
x[[c(2,1)]]
#Subsetting a Matrix1
x<-matrix(1:6,2,3)
x[1,2]
x[2,1]
x[1,]
x[,2]
#Subsetting a Matrix2
x<-matrix(1:6,2,3)
x[1,2]
x[1,2,drop=FALSE]
#Subsetting a Matrix3
x<-matrix(1:6,2,3)
x[1,]
x[1,,drop=FALSE]
#Partial Matching
x<-list(aadvark=1:5)
x$a
x[["a"]]
x[["a",exact=FALSE]]
x<-c(1,2,NA,4,NA,5)
bad<-is.na(x)
x[!bad]
#Removing NA Values1
x<-c(1,2,NA,4,NA,5)
y<-c("a","b",NA,"d",NA,"f")
good<-complete.cases(x,y)
good
x[good]
y[good]
#Removing NA Values2
airquality[1:6,]
good<-complete.cases(airquality)
airquality[good,][1:6,]
#Vectorized Operations
x<-1:4; y<-6:9
x+y
x>2
x>=2
y==8
x*y
x/y
#Vectorized Matrix Operations
x<-matrix(1:4,2,2);y<-matrix(rep(10,4),2,2)
x*y
x/y
x%*%y
#if else
y<--1
y
if (y < 10) {
print("y is less than 10")
} else if(y==10){
print("y is equal to 10")
} else{
print("y is greater than 10")
}
#FOR Loop
x <- c("apples", "oranges", "bananas", "strawberries")
#for (i in x){ print(x[i])}
for (i in 1:4) print(x[i])
for (i in seq(x)) print(x[i])
#NESTED FOR
m <- matrix(1:10, 2,5)
m
for (i in seq(nrow(m))) {
for (j in seq(ncol(m))) {
print(m[i, j])
}
}
#WHILE
i <- 1
while (i < 10) {
print(i)
i <- i + 1
}
#REPEAT AND BREAK
v <- c("Hello","loop")
cnt <- 2
repeat {
print(v)
cnt <- cnt + 1
if(cnt > 5) {
break
}
}
v<-0
repeat{
print(v)
v<-v+1
if(v==5){
break
}
}
for (i in 1:20) {
if (i%%2 == 1) {
print()
next
} else {
print(i)
}
}
data<-read.csv("C:\\Users\\RM A-225\\Documents\\Midterm_Rep\\Seatwork Midterm\\midetrmseatwork_data.csv")
data
sub<-subset(data)
sub
if(sub[1:153,"Ozone"]>25&sub[1:153,"Temp"]>70){
data2<-sub[,"Wind"]
}
apply(data2,2,mean)
for(i in 153)
if(data[i,"Ozone"]>25)
if(data[i,"Temp"]>70){
data2=data
}
subset(data2,,"Temp")
sub=subset(data,Ozone>25&Temp>70,select=Wind)
apply(sub,2,mean)
install.packages("imager")
install.packages("magick")
library(imager)
#file <- system.file('C:\\Users\\RM A-225\\Documents\\tictac.jpeg',package='imager')
#1
library(jpeg)
library(imager)
showLOCimg<-function(addr){
library(imager)
image <- load.image(addr)
plot(image)
}
showLOCimg('C:/Users/Public/Pictures/Sample Pictures/Chrysanthemum.jpg')
showURLimg<-function(addr){
library(magick)
image_read(addr)
}
showURLimg("https://upload.wikimedia.org/wikipedia/en/8/8f/MiddleEarthShadowOfWar.jpg")
#2.1
imgResize<-function(img,wid,hei,filename){
load<-load.image(img)
y <- resize(load,wid,hei)
plot(y)
x<-"C:/Users/RM A-225/Documents/augmented_images/"
savefile<-paste(x,filename)
save.image(image,savefile)
}
imgResize('C:/Users/Public/Pictures/Sample Pictures/sotrnew.jpg',25,25,"resize.jpg")
#2.2
grayimg<-function(addr,grey=TRUE){
library(imager)
image <- load.image(addr)
if(grey==TRUE){
gimage<-grayscale(image)
plot(gimage)
}
else{
plot(image)
}
}
grayimg('C:/Users/Public/Pictures/Sample Pictures/Chrysanthemum.jpg',FALSE)
grayimg('C:/Users/Public/Pictures/Sample Pictures/Chrysanthemum.jpg',TRUE)
#2.3
rotImg<-function(addr, deg){
image<-image_read(addr)
image_rotate(image,deg)
}
rotImg('C:/Users/Public/Pictures/Sample Pictures/SOTR.png',90)
#3
saveImg<-function(addr,filename){
library(imager)
image <- load.image(addr)
x<-"C:/Users/RM A-225/Documents/augmented_images/"
savefile<-paste(x,filename)
save.image(image,savefile)
}
dis_photo<-load.image('C:/Users/Public/Pictures/Sample Pictures/Chrysanthemum.jpg')
resized<-resize(dis_photo,25,25)
saveImg(resized,"try4.jpg")
install.packages("argparse")
install.packages("simplejson")
install.packages("rPyhton")
install.packages("devtools")
library(devtools)
library(argparse)
library(reticulate)
os<-import("os")
py_install("pandas")
parser <- ArgumentParser(description='Process some integers')
parser$add_argument('integers', metavar='N', type="integer", nargs='+',
help='an integer for the accumulator')
parser$add_argument('--sum', dest='accumulate', action='store_const',
const='sum', default='max',
help='sum the integers (default: find the max)')
parser$print_help()
# default args for ArgumentParser()$parse_args are commandArgs(TRUE)
# which is what you'd want for an Rscript but not for interactive use
args <- parser$parse_args(c("--sum", "1", "2", "3"))
accumulate_fn <- get(args$accumulate)
print(accumulate_fn(args$integers))
install.python(page_with_download_url = "https://www.python.org/downloads/windows/version_number = 3")
################################################################
# Copyright (c) 2012-2014 Trevor L. Davis
# Copyright (c) 2014 Paul Gilbert
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#' Tests whether the python command is sufficient
#'
#' \code{is_python_sufficient} checks whether a given python binary has all the
#' desired features (minimum and/or maximum version number and/or access to
#' certain modules).
#'
#' @param path The path to a given python binary.
#' If binary is on system path just the binary name will work.
#' @param minimum_version The minimum version of python it should be.
#' Should be a string with major and minor number separated by a '.'.
#' If left NULL won't impose such a restriction.
#' @param maximum_version The maximum version of python it should be.
#' Should be a string with major and minor number separated by a '.'.
#' If left NULL won't impose such a restriction.
#' @param required_modules Which modules should be required.
#' Can use a single "|" to represent a single either-or requirement like "json|simplejson".
#' If left NULL won't impose such a restriction.
#' @return \code{TRUE} or \code{FALSE} depending on whether the python binary met all requirements
#' @export
is_python_sufficient <- function(path, minimum_version=NULL,
maximum_version=NULL, required_modules=NULL) {
python_code <- vector("character")
if(!is.null(required_modules)) {
import_code <- .create_import_code(required_modules)
python_code <- append(python_code, import_code)
}
version_code <- .create_version_checking_code(minimum_version, maximum_version)
python_code <- append(python_code, version_code)
ok_message <- "Everything worked out okay"
python_code <- append(python_code, paste("print(", shQuote(ok_message), ")", sep=""))
tryCatch({
output <- system(path, intern=TRUE, input=python_code, ignore.stderr=TRUE)
any(grepl(ok_message, output))
},
warning = function(w) {
# qpath <- sQuote(path)
# warning(qpath,
# "Doesn't seem to have required modules or is of insufficient version")
FALSE
},
error = function(e) {
FALSE
})
}
#' Find a suitable python cmd or give error if not possible
#'
#' \code{find_python_cmd} finds a suitable python cmd or raises an error if not possible
#'
#' @inheritParams is_python_sufficient
#' @param error_message What error message the user will see if couldn't find a sufficient python binary.
#' If left NULL will print out a default message.
#' @return The path to an appropriate python binary. If such a path wasn't found then it will throw an error.
#' @examples
#' \dontrun{
#' find_python_cmd()
#' find_python_cmd(minimum_version='2.6', maximum_version='2.7')
#' find_python_cmd(required_modules = c('argparse', 'json | simplejson'))
#' }
#' @seealso \code{\link{can_find_python_cmd}} for a wrapper which doesn't throw an error
#' @export
find_python_cmd <- function(minimum_version=NULL, maximum_version=NULL,
required_modules=NULL, error_message=NULL) {
python_cmds <- c(getOption("python_cmd", ""), "python", Sys.getenv("PYTHON", ""),
sprintf("python%.1f", c(seq(4.9, 2.0, by=-0.1))),
"python4", "python3", "python2", "pypy",
Sys.getenv("PYTHON4", ""), Sys.getenv("PYTHON3", ""),
Sys.getenv("PYTHON2", ""), sprintf("C:/Python%s/python", c(49:20)))
python_cmds <- unique(python_cmds)
python_cmds <- Sys.which(python_cmds)
python_cmds <- python_cmds[which(python_cmds != "")]
for(cmd in python_cmds) {
if(is_python_sufficient(cmd, minimum_version, maximum_version, required_modules)) {
return(cmd)
}
}
if(exists('py_discover_config', where=asNamespace('reticulate'), mode='function')) { # Fall back on reticulate if can't find a suitable command
python_cmds <- reticulate::py_discover_config()$python_versions
for(cmd in python_cmds) {
if(is_python_sufficient(cmd, minimum_version, maximum_version, required_modules)) {
return(cmd)
}
}
}
if(is.null(error_message)) { error_message <- paste("Couldn't find a sufficient Python binary.",
"If you haven't installed the Python dependency yet please do so.",
"If you have but it isn't on the system path (as is default on Windows) please add it to path",
"or set options('python_cmd'='/path/to/binary') ",
"or set the PYTHON, PYTHON2, or PYTHON3 environmental variables.",
if(!is.null(minimum_version)) paste('Python must be at least version', minimum_version),
if(!is.null(maximum_version)) paste('Python must be at most version', maximum_version),
if(!is.null(required_modules)) paste('Python must have access to the modules:',
paste(required_modules, collapse=', ')))}
stop(error_message)
}
#' Determines whether or not it can find a suitable python cmd
#'
#' \code{can_find_python_cmd} runs \code{find_python_cmd} and returns whether it could find a suitable python cmd. If it was successful its output also saves the found command as an attribute.
#'
#' @inheritParams find_python_cmd
#' @param silent Passed to \code{try}, whether any error messages from \code{find_python_cmd} should be suppressed
#' @return \code{TRUE} or \code{FALSE} depending on whether \code{find_python_cmd} could find an appropriate python binary.
#' If \code{TRUE} the path to an appropriate python binary is also set as an attribute.
#' @examples
#' did_find_cmd <- can_find_python_cmd()
#' python_cmd <- attr(did_find_cmd, "python_cmd")
#' @seealso \code{\link{find_python_cmd}}
#' @export
can_find_python_cmd <- function(minimum_version = NULL,
maximum_version = NULL, required_modules = NULL,
error_message = NULL, silent = FALSE) {
python_cmd <- try(find_python_cmd(minimum_version = minimum_version,
maximum_version = maximum_version,
required_modules = required_modules,
error_message = error_message),
silent = silent)
if(inherits(python_cmd, "try-error")) {
r <- FALSE
} else {
r <- TRUE
attr(r, 'python_cmd') <- python_cmd
}
r
}
# Create appropriate module import code
.create_import_code <- function(required_modules) {
import_code <- vector("character")
for(module in required_modules) {
if(grepl("\\|", module)) {
module <- gsub(" ", "", module)
module <- strsplit(module, "\\|")[[1]]
import_code <- append(import_code,
c("try:",
paste(" import", module[1]),
"except ImportError:",
paste(" import", module[2])))
} else {
import_code <- append(import_code, paste("import", module))
}
}
import_code
}
# Create appropriate version checking code
.create_version_checking_code <- function(minimum_version = NULL, maximum_version = NULL) {
if(is.null(minimum_version) && is.null(maximum_version))
return(c())
else {
version_code <- c("import sys")
}
if(!is.null(minimum_version)) {
min_version <- strsplit(minimum_version, '\\.')[[1]]
major = min_version[1]
minor = min_version[2]
version_code <- append(version_code,
c(paste("if sys.version_info.major <", major,
": raise Exception('Major version too low')"),
paste("if sys.version_info.major ==", major,
"and sys.version_info.minor <", minor,
": raise Exception('Minor version too low')")))
}
if(!is.null(maximum_version)) {
max_version <- strsplit(maximum_version, '\\.')[[1]]
major = max_version[1]
minor = max_version[2]
version_code <- append(version_code,
c(paste("if sys.version_info.major >", major,
": raise Exception('Major version too high')"),
paste("if sys.version_info.major ==", major,
"and sys.version_info.minor >", minor,
": raise Exception('Minor version too high')")))
}
version_code
}
install.packages("conda")