R/save.R

Defines functions saveConstraint saveDVH saveMetric.list saveMetric.data.frame saveMetric

Documented in saveConstraint saveDVH saveMetric saveMetric.data.frame saveMetric.list

saveMetric <-
function(x, file="", ...) {
    UseMethod("saveMetric")
}

saveMetric.data.frame <-
function(x, file="", ...) {
    write.table(x, file=file, row.names=FALSE, ...)
}

saveMetric.list <-
function(x, file="", ...) {
    addArgs <- list(...)

    ## determine sensible file names
    pat1st <- "([[:alnum:]]+[[:alnum:][:blank:][:punct:]]*)\\.[[:alnum:]]+$"
    pat2nd <-  "[[:alnum:]]+[[:alnum:][:blank:][:punct:]]*(\\.[[:alnum:]]+$)"

    first <- if(grepl(pat1st, file)) {
        first <- paste0(sub(pat1st, "\\1", file), "_", collapse="")
    } else {
        "metric_"
    }

    last <- if(grepl(pat2nd, file)) {
        sub(pat2nd, "\\1", file)
    } else {
        ".txt"
    }

    comps  <- gsub("\\.", "_", names(x))
    fNames <- paste0(first, comps, last)

    if(length(addArgs) > 0L) {
        Map(saveMetric.data.frame, x, file=fNames, ...)
    } else {
        Map(saveMetric.data.frame, x, file=fNames)
    }

    return(invisible(NULL))
}

saveDVH <-
function(x, file="", ...) {
    addArgs <- list(...)

    ## determine sensible file names
    pat1st <- "([[:alnum:]]+[[:alnum:][:blank:][:punct:]]*)\\.[[:alnum:]]+$"
    pat2nd <-  "[[:alnum:]]+[[:alnum:][:blank:][:punct:]]*(\\.[[:alnum:]]+$)"

    first <- if(grepl(pat1st, file)) {
        first <- paste0(sub(pat1st, "\\1", file), "_", collapse="")
    } else {
        "dvh_"
    }

    last <- if(grepl(pat2nd, file)) {
        sub(pat2nd, "\\1", file)
    } else {
        ".pdf"
    }

    comps <- gsub("\\.", "_", names(x))
    file  <- paste0(first, comps, last)

    ### ... my be NULL -> cannot use Map()
    if(length(addArgs) > 0L) {
        Map(ggsave, x, filename=file, ...)
    } else {
        Map(ggsave, x, filename=file)
    }

    return(invisible(NULL))
}

saveConstraint <-
function(x, ...) {
    write.table(x, row.names=FALSE, ...)
}

Try the DVHmetrics package in your browser

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

DVHmetrics documentation built on March 23, 2022, 5:08 p.m.