R/sas_collectcode.r

Defines functions sas_collectcode

Documented in sas_collectcode

sas_collectcode <- function() {
    if (file.exists("autoexec.sas")) {
        assign("oautoexec", readLines("autoexec.sas"), pos=2)
        message("Found an existing 'autoexec.sas'.")
    } else {
        oautoexec <- NULL
    }
    
knitr::knit_hooks$set(collectcode = function(before, options, envir) {
    if (!before) {
        if (options$engine %in% c("sas", "saslog", 
                                  "sashtml", "sashtmllog", 
                                  "sashtml5", "sashtml5log")) {
            autoexec <- file("autoexec.sas", open="at")
            writeLines(options$code, autoexec)
            close(autoexec)
            #print(sys.frames())
            #print(sys.calls())
        do.call("on.exit", 
            list(quote(unlink("autoexec.sas")), add=TRUE),
            envir = sys.frame(-9)) 
        do.call("on.exit", 
           list(quote(print(sys.call())), add=TRUE),
            envir = sys.frame(-9)) 
            # sys.frame(1) or sys.frame(-10) is rmarkdown::render()
            # sys.frame(-9) is knitr::knit()
        if (!is.null(oautoexec)) {
            do.call("on.exit",
                list(quote(writeLines(oautoexec, "autoexec.sas")), add=TRUE),
                envir = sys.frame(-9))
        }
        }
    }
})
}

Try the SASmarkdown package in your browser

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

SASmarkdown documentation built on April 1, 2023, 12:15 a.m.