Commit 8876b695 authored by Enrico Schumann's avatar Enrico Schumann

Add function file_info; handle reading of empty files

parent 2ba6cc1f
2017-05-10 Enrico Schumann <es@enricoschumann.net>
* R/functions.R (read_ts_tables): safeguard for
empty files
(file_info): add function
* inst/unitTests/ut_write_read.R
(test.read_ts_tables): add test for empty files
2017-05-09 Enrico Schumann <es@enricoschumann.net>
* DESCRIPTION (Version): 0.4-5
......@@ -23,8 +32,8 @@
* DESCRIPTION (Version): 0.4-3
* R/functions.R (write_ts_table): fix handling
of argument 'overwrite': it had been ignored (reported
by Torsten von Bartenwerffer)
of argument 'overwrite': it had been
ignored (reported by Torsten von Bartenwerffer)
2017-03-16 Enrico Schumann <es@enricoschumann.net>
......
export(
as.ts_table,
file_info,
read_ts_tables,
write_ts_table,
ts_table,
as.ts_table
write_ts_table
)
importFrom("DBI",
......
......@@ -154,12 +154,16 @@ read_ts_tables <- function(file, dir, t.type = "guess",
else
stop("check lengths of file and dir")
}
samp <- readLines(dfile[[1]], n = 2L)
if (t.type == "guess" || missing(columns))
samp <- readLines(dfile[[1]], n = 2L)
if (t.type == "guess") {
tmp <- as.numeric(
strsplit(samp[[2]], ",", fixed = TRUE)[[1L]][[1L]])
t.type <- if (tmp < 30000) "Date" else "POSIXct"
if (length(samp) == 2L) {
tmp <- as.numeric(
strsplit(samp[[2]], ",", fixed = TRUE)[[1L]][[1L]])
t.type <- if (tmp < 30000) "Date" else "POSIXct"
} else
t.type <- "Date"
}
if (missing(columns)) {
tmp <- gsub("\"", "",
......@@ -281,12 +285,26 @@ file_info <- function(dir, file) {
else
file.path(dir, file)
res <- " "
class(res) <- "file_info"
nf <- length(dfile)
res <- data.frame(dir_file = dfile,
columns = character(nf),
min_timestamp = numeric(nf),
max_timestamp = numeric(nf),
stringsAsFactors = FALSE)
for (i in seq_len(nf)) {
fi <- read_ts_tables(dfile[i])
res[["min_timestamp"]][i] <- suppressWarnings(min(fi$timestamp))
res[["max_timestamp"]][i] <- suppressWarnings(max(fi$timestamp))
}
res[["min_timestamp"]][!is.finite(res[["min_timestamp"]])] <- NA
res[["max_timestamp"]][!is.finite(res[["max_timestamp"]])] <- NA
class(res) <- c("file_info", "data.frame")
res
}
print.file_info <- function(x, ...) {
print(unclass(x))
print(x, ...)
}
update_ts_table <- function(x, timestamp, type, file)
......
......@@ -58,11 +58,18 @@ test.read_ts_tables <- function() {
.Dim = c(5L, 2L)))
z1 <- ts_table(11:15, as.POSIXct("2016-1-1 10:00:00", tz = "UTC")+0:4, "close")
write_ts_table(z1, dir, "X1")
z2 <- ts_table(1:5, as.POSIXct("2016-1-1 10:00:00", tz = "UTC")+1:5, "close")
write_ts_table(z2, dir, "X2")
read_ts_tables(c("X1", "X2"), dir, columns = "close")
## z1 <- ts_table(11:15, as.POSIXct("2016-1-1 10:00:00", tz = "UTC")+0:4, "close")
## write_ts_table(z1, dir, "X1")
## z2 <- ts_table(1:5, as.POSIXct("2016-1-1 10:00:00", tz = "UTC")+1:5, "close")
## write_ts_table(z2, dir, "X2")
## read_ts_tables(c("X1", "X2"), dir, columns = "close")
## check empty file
writeLines('"timestamp","close"', file.path(dir, "empty"))
em <- read_ts_tables("empty", dir)
checkEquals(em$timestamp, structure(numeric(0), class = "Date"))
checkEquals(em$data, structure(numeric(0), .Dim = 0:1))
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment