forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot1.R
More file actions
49 lines (45 loc) · 1.49 KB
/
plot1.R
File metadata and controls
49 lines (45 loc) · 1.49 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
## Function to get user confirmation
## Only quit with "y" or "n"
readconfirm <- function() {
n <- readline(prompt="Do you want to continue? (y/n): ")
if(n=="y" || n=="n") {
return(n)
} else {
return(readconfirm())
}
}
readData <- function() {
file <- "./data/household_power_consumption.txt"
library(data.table)
## Estimating table size
top.size <- object.size(fread(file, nrow=1000))
lines <- as.numeric(gsub("[^0-9]", "", system(paste("wc -l", file, sep = " "), intern=T)))
size.estimate <- lines / 1000 * top.size
## Asking user if he want to continue
print(paste("You will manipulate a table of size: ", size.estimate, " bytes"))
confirm <- readconfirm()
if (confirm=="n")
return()
## Loading data of days "1/2/2007", "2/2/2007"
findRows<-fread(file, header = TRUE, select = 1)
all<-(which(findRows$Date %in% c("1/2/2007", "2/2/2007")) )
skipLines<- min(all)
keepRows<- length(all)
data <- fread(file, skip = (skipLines) , nrows = keepRows, header = FALSE)
rm(findRows)
dataNames<- names(fread(file, nrow = 1))
setnames(data, dataNames)
return(data)
}
plot1 <- function(){
data <- readData()
if (!is.null(data)){
png("plot1.png")
hist(data$Global_active_power, col = "red",
main = "Global Active Power", xlab = "Global Active Power (kilowatts)")
dev.off()
print("plot1.pgn is done!")
} else {
print("See you!")
}
}