forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot1.R
69 lines (55 loc) · 2.2 KB
/
plot1.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
# Use the following command to construct Plot 1:
# > ConstructPlot1()
ReadData <- function(from.cache) {
# This function returns the dataset needed to make the plots
# and also caches the data in csv format for faster future retrieval.
# Returns:
# A date frame containing only the required subset of the raw data.
# The minimum required memory is about 2,075,259 * 9 * 10 = 180 MB,
# if the entire raw data is loaded into memory.
# However, a better alternative is to read only the potion that's needed.
datasource <- "household_power_consumption.txt"
datacache <- "household_power_consumption_cache.csv"
# Use the cache if it's there and you want to use it.
if(from.cache && file.exists(datacache)) {
result <- read.csv(datacache, header = TRUE)
return(result)
}
con <- file(datasource, open = "r")
single.line <- readLines(con, n = 1, warn = FALSE)
name.list <- unlist(strsplit(single.line, ";"))
# Get every column in string format, in order to ensure portability
result <- data.frame(matrix(ncol = 9, nrow = 0), stringsAsFactors = FALSE)
# Keep the original column names
names(result) <- name.list
i <- 1
while (length(single.line <- readLines(con, n = 1, warn = FALSE)) > 0) {
data.list <- unlist(strsplit(single.line, ";"))
if (data.list[1] == "1/2/2007" | data.list[1] == "2/2/2007") {
# Here is the best place to detect and correct missing values, coded as "?".
# However, missing values are actually missing in the final dataset.
result[i, ] <- data.list
i <- i + 1
}
}
close(con)
# Save the cache for future re-use.
if(file.exists(datacache)) {
unlink(datacache, force = TRUE)
}
write.csv(result, file = datacache, row.names = FALSE)
result
}
ConstructPlot1 <- function(from.cache = FALSE) {
library(datasets)
ds <- ReadData(from.cache)
png("plot1.png", width = 480, height = 480, units = "px")
hist(as.numeric(ds$Global_active_power),
xlab = "Global Active Power (kilowatts)",
col = "red",
main = "Global Active Power")
# The following code will also work:
# dev.copy(png, file = "plot1.png")
# The default size of the image is just 480 x 480 pixels
dev.off()
}