This object controls how to execute the code from languages other than R
(when the chunk option engine
is not 'R'
). Each component in
this object is a function that takes a list of current chunk options
(including the source code) and returns a character string to be written into
the output.
knit_engines
An object of class list
of length 7.
The engine function has one argument options
: the source code of the
current chunk is in options$code
. Usually we can call external
programs to run the code via system2
. Other chunk options are
also contained in this argument, e.g. options$echo
and
options$eval
, etc.
In most cases, options$engine
can be directly used in command line to
execute the code, e.g. python
or ruby
, but sometimes we may
want to specify the path of the engine program, in which case we can pass it
through the engine.path
option. For example, engine='ruby',
engine.path='/usr/bin/ruby1.9.1'
. Additional command line arguments can be
passed through options$engine.opts
, e.g. engine='ruby',
engine.opts='-v'
.
See str(knitr::knit_engines$get())
for a list of built-in language
engines.
The Leiningen engine lein
requires lein-exec plugin; see
https://github.com/yihui/knitr/issues/1176 for details.
Usage: https://yihui.org/knitr/objects/; examples: https://yihui.org/knitr/demo/engines/
knit_engines$get("python")
#> function (options)
#> {
#> if (isFALSE(options$python.reticulate)) {
#> eng_interpreted(options)
#> }
#> else {
#> if (!loadable("reticulate"))
#> warning2("The 'python' engine in knitr requires the reticulate package. ",
#> "If you do not want to use the reticulate package, set the chunk option ",
#> "python.reticulate = FALSE.")
#> reticulate::eng_python(options)
#> }
#> }
#> <environment: namespace:knitr>
knit_engines$get("awk")
#> function (options)
#> {
#> engine = options$engine
#> code = if (engine %in% c("highlight", "Rscript", "sas", "haskell",
#> "stata")) {
#> f = wd_tempfile(engine, switch(engine, sas = ".sas",
#> Rscript = ".R", stata = ".do", ".txt"))
#> write_utf8(c(switch(engine, sas = "OPTIONS NONUMBER NODATE PAGESIZE = MAX FORMCHAR = '|----|+|---+=|-/<>*' FORMDLIM=' ';title;",
#> NULL), options$code), f)
#> on.exit(unlink(f), add = TRUE)
#> switch(engine, haskell = paste("-e", shQuote(paste(":script",
#> f))), sas = {
#> logf = sub("[.]sas$", ".lst", f)
#> on.exit(unlink(c(logf, sub("[.]sas$", ".log", f))),
#> add = TRUE)
#> f
#> }, stata = {
#> logf = sub("[.]do$", ".log", f)
#> on.exit(unlink(c(logf)), add = TRUE)
#> sprintf(switch(Sys.info()[["sysname"]], Windows = "/q /e do %s",
#> Darwin = paste("-q < %s >", shQuote(xfun::normalize_path(logf))),
#> Linux = "-q -e do %s", "-q -b do %s"), shQuote(normalizePath(f)))
#> }, f)
#> }
#> else paste(switch(engine, bash = "-c", coffee = "-e", groovy = "-e",
#> lein = "exec -ep", mysql = "-e", node = "-e", octave = "--eval",
#> perl = "-E", php = "-r", psql = "-c", python = "-c",
#> ruby = "-e", scala = "-e", sh = "-c", zsh = "-c", NULL),
#> shQuote(one_string(options$code)))
#> opts = get_engine_opts(options$engine.opts, engine)
#> code = if (engine %in% c("awk", "gawk", "sed", "sas", "psql",
#> "mysql"))
#> paste(code, opts)
#> else paste(opts, code)
#> cmd = get_engine_path(options$engine.path, engine)
#> out = if (options$eval) {
#> if (options$message)
#> message("running: ", cmd, " ", code)
#> tryCatch(system2(cmd, code, stdout = TRUE, stderr = TRUE,
#> env = options$engine.env), error = function(e) {
#> if (!options$error)
#> stop(e)
#> paste("Error in running command", cmd)
#> })
#> }
#> else ""
#> if (!options$error && !is.null(attr(out, "status")))
#> stop(one_string(out))
#> if (options$eval && engine %in% c("sas", "stata") && file.exists(logf))
#> out = c(read_utf8(logf), out)
#> engine_output(options, options$code, out)
#> }
#> <environment: namespace:knitr>
names(knit_engines$get())
#> [1] "awk" "bash" "coffee" "gawk" "groovy" "haskell" "lein"
#> [8] "mysql" "node" "octave" "perl" "php" "psql" "Rscript"
#> [15] "ruby" "sas" "scala" "sed" "sh" "stata" "zsh"
#> [22] "asis" "asy" "block" "block2" "bslib" "c" "cat"
#> [29] "cc" "comment" "css" "ditaa" "dot" "embed" "eviews"
#> [36] "exec" "fortran" "fortran95" "go" "highlight" "js" "julia"
#> [43] "python" "R" "Rcpp" "sass" "scss" "sql" "stan"
#> [50] "targets" "tikz" "verbatim" "glue" "glue_sql" "gluesql"