From 891af5d0f1ee8d45cd4403af43293463d23ba96b Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Tue, 13 Jan 2015 14:05:31 +0100 Subject: README.rmd with example, fix reading unzipped files --- pkg/R/TOXSWA_cwa.R | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'pkg/R/TOXSWA_cwa.R') diff --git a/pkg/R/TOXSWA_cwa.R b/pkg/R/TOXSWA_cwa.R index 8ac6c2d..8c93183 100644 --- a/pkg/R/TOXSWA_cwa.R +++ b/pkg/R/TOXSWA_cwa.R @@ -152,12 +152,15 @@ TOXSWA_cwa <- R6Class("TOXSWA_cwa", cwas = NULL, windows = NULL, events = NULL, - initialize = function(filename, basedir, zipfile, segment = "last") { + initialize = function(filename, basedir, zipfile = NULL, segment = "last") { self$filename <- filename self$basedir <- basedir self$zipfile <- zipfile - if (!is.null(zipfile)) try((file_connection <- unz(zipfile, paste0(basedir, "/", filename)))) - else try((file_connection <- file(file.path(basedir, filename)))) + if (!is.null(zipfile)) { + try(file_connection <- unz(zipfile, paste0(basedir, "/", filename))) + } else { + try(file_connection <- file(file.path(basedir, filename), "rt")) + } cwa_all_segments <- try(read.table(file_connection, sep = "", skip = 40, colClasses = c("character", "numeric", @@ -165,6 +168,8 @@ TOXSWA_cwa <- R6Class("TOXSWA_cwa", col.names = c("datetime", "t", "segment", "xcd", "cwa_tot", "cwa", "Xss", "Xmp"))) + if (is.null(zipfile)) close(file_connection) # only needed for files + if (!inherits(cwa_all_segments, "try-error")) { available_segments = 1:max(cwa_all_segments$segment) if (segment == "last") segment = max(available_segments) -- cgit v1.2.1