Skip to content

Commit

Permalink
fix: prepend output files with label
Browse files Browse the repository at this point in the history
  • Loading branch information
markvanderloo committed Jan 9, 2020
1 parent 0f2e6a4 commit 6f35eba
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 27 deletions.
2 changes: 1 addition & 1 deletion pkg/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Description: A framework that allows for easy logging of changes in data.
an existing script. Track changes in multiple datasets, using multiple
loggers. Add custom-built loggers or use loggers offered by other
packages.
Version: 1.1.2
Version: 1.1.2.1
URL: https://github.com/markvanderloo/lumberjack
BugReports: https://github.com/markvanderloo/lumberjack/issues
Imports: utils, R6
Expand Down
3 changes: 3 additions & 0 deletions pkg/NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
version 1.2.0
- bugfix: prefix label for file output was ignored since 1.1.2

version 1.1.2
- the 'file' argument in 'cellwise$new' is now called 'tempfile' to better reflect
it's purpose.
Expand Down
6 changes: 3 additions & 3 deletions pkg/R/cellwise.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ cellwise <- R6Class("cellwise"
, con = NULL
, n = NULL
, verbose = NULL
, label = NULL
, key = NULL
)
, public = list(
initialize = function(key, verbose=TRUE, tempfile=file.path(tempdir(),"cellwise.csv")){
label = NULL
, initialize = function(key, verbose=TRUE, tempfile=file.path(tempdir(),"cellwise.csv")){
if(missing(key)) stop("you must provide a key")
private$tmpfile = tempfile
private$con = file(private$tmpfile, open="wt")
Expand Down Expand Up @@ -105,7 +105,7 @@ cellwise <- R6Class("cellwise"
private$con <- iclose(private$con)
if (is.null(file)){
file <- "cellwise.csv"
if (!is.null(private$label) && private$label != "" ) file <- paste(private$label,file,sep="_")
if (!is.null(self$label) && self$label != "" ) file <- paste(self$label,file,sep="_")
}
file.copy(from=private$tmpfile, to=file, overwrite = TRUE)
if (private$verbose){
Expand Down
6 changes: 3 additions & 3 deletions pkg/R/expression_logger.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ expression_logger <- R6Class("expression_loggger"
, expression = NULL
, result = NULL
, verbose=TRUE
, label=NULL
)
, public = list(
initialize = function(..., verbose=TRUE){
label=NULL
, initialize = function(..., verbose=TRUE){
private$step <- c()
private$expression <- c()
private$verbose <- verbose
Expand All @@ -71,7 +71,7 @@ expression_logger <- R6Class("expression_loggger"
, dump = function(file=NULL,...){
if (is.null(file)){
file <- "expression.csv"
if (!is.null(private$label) && private$label != "") file <- paste(private$label, file, sep="_")
if (!is.null(self$label) && self$label != "") file <- paste(self$label, file, sep="_")
}
d <- cbind(
step = private$step
Expand Down
28 changes: 14 additions & 14 deletions pkg/R/filedump.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,24 @@ filedump <- R6Class("filedump"
, dir = NULL
, verbose = NULL
, filename = NULL
, label=NULL
)
, public = list(
initialize = function(dir = file.path(tempdir(),"filedump")
, filename="%sstep%03d.csv", verbose = TRUE){
private$n <- 0
private$dir <- dir
if (!dir.exists(dir)){
dir.create(dir,recursive = TRUE)
if (verbose){
msgf("Created %s", normalizePath(dir))
label=NULL
, initialize = function(dir = file.path(tempdir(),"filedump")
, filename="%sstep%03d.csv", verbose = TRUE){
private$n <- 0
private$dir <- dir
if (!dir.exists(dir)){
dir.create(dir,recursive = TRUE)
if (verbose){
msgf("Created %s", normalizePath(dir))
}
}
private$verbose <- verbose
private$filename <- filename
}
}
private$verbose <- verbose
private$filename <- filename
}
, add = function(meta, input, output){
prefix <- if (is.null(private$label)) "" else paste0(private$label,"_")
prefix <- if (is.null(self$label)) "" else paste0(self$label,"_")
outname <- file.path(private$dir, sprintf(private$filename, prefix, private$n))
if (private$n == 0)
write.csv(input, file=outname, row.names=FALSE)
Expand Down
12 changes: 6 additions & 6 deletions pkg/R/simple.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ simple <- R6Class("simple"
n = NULL
, store = NULL
, verbose = NULL
, label = NULL
)
, public = list(
initialize = function( verbose = TRUE){
private$n <- 0
private$store <- new.env()
private$verbose <- verbose
label = NULL
, initialize = function( verbose = TRUE){
private$n <- 0
private$store <- new.env()
private$verbose <- verbose
}
, add = function(meta, input, output){
private$n <- private$n + 1
Expand All @@ -66,7 +66,7 @@ simple <- R6Class("simple"
log_df <- do.call(rbind,mget(ls(private$store), private$store))
if (is.null(file)){
file <- "simple.csv"
if (!is.null(private$label) && private$label != "" ) file <- paste(private$label,file,sep="_")
if (!is.null(self$label) && self$label != "" ) file <- paste(self$label,file,sep="_")
}
write.csv(log_df, file=file, row.names = FALSE,...)
if (is.character(file) && private$verbose ){
Expand Down
2 changes: 2 additions & 0 deletions pkg/inst/tinytest/test_cellwise.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ d2 <- data.frame(id=c(1,1),x=1:2)
logger <- cellwise$new(key="id")
expect_warning(logger$add(meta=list(src="haha"),input=d1,output=d2))

expect_true("label" %in% ls(logger))


1 change: 1 addition & 0 deletions pkg/inst/tinytest/test_expressionlogger.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ expect_equal(lg$mw[1], mean(women$weight))
expect_equal(lg$mh[2], mean(2*women$height))
expect_equal(lg$mw[2], mean(women$weight))

expect_true("label" %in% ls(logger))


2 changes: 2 additions & 0 deletions pkg/inst/tinytest/test_filedump.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ i2 <- dump_log(i2, verbose=TRUE)

# this test crashes covr but it does pass.
#expect_equal(length(logger$logdata()) , 3)
expect_true("label" %in% ls(logger))


1 change: 1 addition & 0 deletions pkg/inst/tinytest/test_simple.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ logger <- simple$new()
iris %>>% start_log(logger) %>>% head() %>>% stop_log(dump=FALSE)
expect_equal(nrow(logger$logdata()), 1L)

expect_true("label" %in% ls(simple$new()))

0 comments on commit 6f35eba

Please sign in to comment.