R/output.R

Defines functions gdiffGenerateOutput generateOutput

Documented in gdiffGenerateOutput

## Functions for generating graphical output
generateOutput <- function(session, codeFun, dir, device, clean, ncpu) {
    UseMethod("generateOutput")
}

## Useful function for generateOutput() methods
gdiffGenerateOutput <- function(codeFun, dir, device, clean, ncpu) {
    ## 'codeFun' can be NULL
    ## (in which case, do nothing)
    if (!is.null(codeFun)) {
        if (length(dir) > 1) {
            dir <- dir[1]
            warning("Multiple output directories specified: only using the first")
        }
        createDir(dir, clean)
        sessionFile <- file.path(dir, gdiffSessionFile)
        if (file.exists(sessionFile)) {
            stop("Directory already contains 'gdiff' output")
        }
        info <- sessionInfo()
        ## Use version 2 for maximum compatiblity across R sessions
        saveRDS(info, file=sessionFile, version=2)
        codeList <- codeFun()
        f <- function(fun, name) {
            ## Allow function to be generated as NULL
            ## (in which case, do nothing)
            if (!is.null(fun)) {
                for (d in device) {
                    filename <- file.path(dir, name) 
                    d$open(filename)
                    fun()
                    d$close(dir, name)
                }
            }
        }
        if (length(codeList)) {
            if (ncpu > 1) {
                cl <- parallel::makePSOCKcluster(ncpu)
                parallel::clusterMap(cl, f, codeList, names(codeList))
                parallel::stopCluster(cl)
            } else {
                invisible(mapply(f, codeList, names(codeList)))
            }
        }
    }
}

Try the gdiff package in your browser

Any scripts or data that you put into this service are public.

gdiff documentation built on April 1, 2023, 12:11 a.m.