forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot2.R
More file actions
51 lines (47 loc) · 1.61 KB
/
plot2.R
File metadata and controls
51 lines (47 loc) · 1.61 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
## 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)
}
plot2 <- function(){
data <- readData()
if (!is.null(data)){
png("plot2.png")
Sys.setlocale("LC_TIME", "en_US.UTF-8")
plot(strptime(paste(data$Date, data$Time ,sep = " "), format = "%d/%m/%Y %H:%M:%S"),
data$Global_active_power, type = "l", ylab = "Global Active Power (kilowatts)",
xlab = "")
dev.off()
print("plot2.pgn is done!")
} else {
print("See you!")
}
}