Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

daff logging object that can be used with the 'lumberjack' pkg #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
^examples
^\.travis\.yml$
^appveyor\.yml$
R/TODO.txt
^.RData
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ LazyData: true
Imports:
V8 (>= 0.6),
jsonlite,
utils
utils,
R6
Enhances: lumberjack
URL: http://github.com/edwindj/daff
Suggests:
testthat
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ S3method(print,data_diff_summary)
S3method(summary,data_diff)
export(diff_data)
export(differs_from)
export(lbj_daff)
export(merge_data)
export(patch_data)
export(read_diff)
export(render_diff)
export(which_conflicts)
export(write_diff)
import(R6)
importFrom(V8,JS)
importFrom(V8,new_context)
importFrom(jsonlite,toJSON)
Expand Down
114 changes: 114 additions & 0 deletions R/lumberjack.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# A logger for the 'lumberjack' package.



#' Automatically derive daffs with the lumberjack package
#'
#' The lbj_daff logger creates a \code{daff} and writes it to \code{daff<n>.csv}
#' in a directory of choice.
#'
#' @import R6
#'
#' @section Creating a logger:
#'
#' \code{filedump$new(dir=file.path(tempdir(),"daff")
#' , prefix="step\%03d.csv", verbose=TRUE, ...)}
#' \tabular{ll}{
#' \code{dir}\tab Where to write the daff files.\cr
#' \code{filename}\tab filename template, used with \code{\link{sprintf}}
#' to create a file name.\cr
#' \code{verbose}\tab toggle verbosity\cr
#' \code{...}\tab Extra arguments, passed to \code{diff_data} each time.
#' }
#'
#' @section Dump options:
#'
#' \code{$dump(...)}
#' \tabular{ll}{
#' \code{...}\tab Currently unused.\cr
#' }
#'
#' @section Retrieve log data:
#'
#' \code{$logdata(simplify=TRUE)} returns a list of data frames, sorted in the
#' order returned by \code{base::dir()}
#' \code{$diffs(simplify=TRUE)} returns a list of \code{daff} objects, sorted
#' in the order returned by \code{base::dir()}
#'
#' \tabular{ll}{
#' \code{simplify}\tab Simplify lists of length 1 to data.frame?
#' }
#'
#' @section Details:
#'
#' If \code{dir} does not exist it is created. If
#'
#'
#' @docType class
#' @format An \code{R6} class object.
#'
#' @examples
#' \dontrun{
#' library(lumberjack)
#' logger <- lbj_daff$new()
#' women %>>%
#' start_log(logger) %>>%
#' head() %>>%
#' {. * 2} %>>%
#' dump_log()
#' L <- logger$logdata()
#' L[[2]]
#' L <- logger$diffs()
#' L[[1]]
#' }
#'
#' @family loggers
#' @export
lbj_daff <- R6Class("lpj_daff"
, public=list(
n = NULL
, dir = NULL
, verbose = NULL
, filename = NULL
, daff_args = NULL
, initialize = function(dir = file.path(tempdir(), "daff")
, filename="daff%03d.csv", verbose = TRUE,...){
self$n <- 0
self$dir <- dir
if (!dir.exists(dir)){
dir.create(dir,recursive = TRUE)
if (verbose){
msgf("Created %s", normalizePath(dir))
}
}
self$verbose <- verbose
self$filename <- filename
self$daff_args <- list(...)
}
, add = function(meta, input, output){
outname <- file.path(self$dir,sprintf(self$filename,self$n))
self$n <- self$n + 1
outname <- file.path(self$dir, sprintf(self$filename,self$n))
arglist <- c(list(data_ref=input, data=output), self$daff_args)
output <- do.call("diff_data", arglist)
write_diff(output, file=outname)
}
, dump = function(...){
if ( self$verbose ){
msgf("daffs were written to %s", normalizePath(self$dir))
}
}
, logdata = function(simplify=TRUE){
fl <- dir(self$dir,full.names = TRUE)
L <- lapply(fl, function(x) read_diff(x)$get_data())
if (simplify && length(L) == 1L) L[[1]] else L
}
, diffs = function(simplify=TRUE){
fl <- dir(self$dir, full.names=TRUE)
L <- lapply(fl, read_diff)
if (simplify && length(L) == 1L) L[[1]] else L
}
))

# msgf: the reasonable messenger :^)
msgf <- function(fmt,...) message(sprintf(fmt,...))
73 changes: 73 additions & 0 deletions man/lbj_daff.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions tests/testthat/test_lumberjack.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

context("lumberjack logger")

test_that("lumberjack logger",{
logger <- lbj_daff$new()
meta=list(expr=expression({.*2}), src="{\n .*2\n}")
logger$add(meta, women, 2*women)
expect_equal(logger$n,1L)
fl <- dir(logger$dir, full.names = TRUE)
expect_equal(length(fl),1L)
d <- read.csv(fl)
expect_equal(nrow(d), nrow(women)*ncol(women))
expect_equal(nrow(logger$logdata()), nrow(women)*ncol(women))
})