R/option.R

Defines functions is_harvestr_frame is_top_harvestr_call test_is_top_harvestr_call dflt_harvestr_parallel test_dflt_harvestr_parallel dflt_harvestr_progress test_dflt_harvestr_progress dflt_harvestr_time dflt_harvestr_cache dflt_harvestr_cache_dir dflt_harvestr_use_try

is_harvestr_frame <- function(env){
    par <- parent.env(env)
    if(!exists(".packageName", envir=par, inherits=FALSE)) return(FALSE)
    par$.packageName == "harvestr"
}

is_top_harvestr_call <-
function(n=0){
    frames <- head(sys.frames(), -1-n)
    sum(sapply(frames, is_harvestr_frame))==1
}
test_is_top_harvestr_call <- function(...)is_top_harvestr_call(...)


#' @importFrom foreach getDoParRegistered
dflt_harvestr_parallel <-
function(){
    if(is_top_harvestr_call())
        stop("dflt_harvestr_parallel should not be called directly.")
    if(is_top_harvestr_call(1))
        return(getDoParRegistered())
    frames <- sys.frames()
    harvestr.frames <- Filter(is_harvestr_frame, frames)
    has.parallel <- sapply(harvestr.frames, exists, x=".parallel", where=-1, inherits=FALSE)
    if(!any(has.parallel)) return(FALSE)
    return(max(get(envir=harvestr.frames[has.parallel][[1]], ".parallel") - 1, 0))
}
test_dflt_harvestr_parallel <- function(.parallel=0, nest=.parallel){
    if(nest>0) return(Recall(.parallel=.parallel, nest= nest-1))
    dflt_harvestr_parallel()
}


dflt_harvestr_progress <-
function( is.interactive = Interactive()
        , OS             = .Platform$OS.type
        , is.top.call    = is_top_harvestr_call(1)
        ){
    if( is.interactive && is.top.call){
        if( OS == "windows")
            return("win")
        else return("time")
    }
    return("none")
}
test_dflt_harvestr_progress <- function(...){dflt_harvestr_progress(...)}

dflt_harvestr_time      <- function(){ FALSE }
dflt_harvestr_cache     <- function(){ FALSE }
dflt_harvestr_cache_dir <- function(){"harvestr-cache"}
dflt_harvestr_use_try   <- function(){!interactive()}

defaults <-
list( parallel  = dflt_harvestr_parallel
    , progress  = dflt_harvestr_progress
    , time      = dflt_harvestr_time
    , cache     = dflt_harvestr_cache
    , cache.dir = dflt_harvestr_cache_dir
    , use.try   = dflt_harvestr_use_try
    )
halpo/harvestr documentation built on April 2, 2020, 8:43 p.m.