R/S3methods.R

Defines functions summary.CachedWorkflow print.GVcode print.CachedWorkflow plot.CachedWorkflow print.SimpleFuture print.CachedResult

#' @export
print.CachedResult <- function(x, ...) {
    `if`(x %>% containsVal,
         print(x$val, ...),
         do.(message('CachedResult\n',
                    'Its value can be extracted with function `extractVal`.')))
    invisible(x)
}

#' @export
print.SimpleFuture <- function(x, ...) {
    `if`(x %>% containsVal,
         print(x$val, ...),
         do.(message('SimpleFuture (result calculated concurrently)\n',
                    'Its value can be extracted with function `extractVal`\n',
                    'which may have to wait until the value is computed.')))
    invisible(x)
}

#' @export
plot.CachedWorkflow <- function(x, ...) {
    x %>%
        attr('GVcode') %>%
        DiagrammeR::grViz()
}

#' @export
print.CachedWorkflow <- function(x, ...) {
    message('CachedWorkflow\n',
            'It can be drawn as a diagram with function `plot`.\n',
            'The returned value:')
    `if`(x %>% inherits('CachedResult'),
         print.CachedResult(x),
         x %>%
             `class<-`(class(x) %>%
                           setdiff('CachedWorkflow')) %>%
             print(...))
    invisible(x)
}

#' @export
print.GVcode <- function(x, ...) {
    cat(x)
    invisible(x)
}

#' @export
summary.CachedWorkflow <- function(x, ...) {
    GVcode <- x %>%
        attr('GVcode', exact=TRUE)
    c(cached='_._Cached_._',
      evaluated='_._NotCached_._') %>%
        sapply(countSubstrInStr,
               str=GVcode) %>%
        c(total=sum(.['cached'],
                    .['evaluated'])) %>%
        message_(ti=FALSE,
                 'The number of function calls in the\n',
                 'CachedWorkflow made with `cachedCall`:')
}
alekrutkowski/cacheflow documentation built on Sept. 3, 2021, 7:10 p.m.