Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

created a eng_r function which exposes the R engine #1963

Merged
merged 17 commits into from
Mar 11, 2021
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Title: A General-Purpose Package for Dynamic Report Generation in R
Version: 1.31.5
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")),
person("Abhraneel", "Sarma", role = "ctb"),
person("Adam", "Vogt", role = "ctb"),
person("Alastair", "Andrew", role = "ctb"),
person("Alex", "Zvoleff", role = "ctb"),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## NEW FEATURES

- The internal code to process R code chunks was factored out into the language engine `R`, which can be obtained via `knit_engines$get('R')` now (thanks, @abhsarma, #1963).

- Added arguments `dir` and `envir` to `load_cache()` to specify the working directory and environment into which the cached objects are loaded. By default, all cached objects are loaded into the global environment, which may not be desirable (thanks, @LTLA, #1905).

- The internal function `html_screenshot()`, which takes screenshots of HTML widgets and Shiny apps in **knitr** documents, now prefers the **webshot2** package over the **webshot** package. This is because the backend of the **webshot** package is PhantomJS, which [has been archived since 2018](https://github.com/ariya/phantomjs/issues/15344). If **webshot** is still preferred, use the chunk option `opts_chunk$set(webshot = "webshot")` (thanks, @atusy #1918, @LouisStAmour #1858).
Expand Down
49 changes: 33 additions & 16 deletions R/block.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,42 @@ cache2.opts = c('fig.keep', 'fig.path', 'fig.ext', 'dev', 'dpi', 'dev.args', 'fi
cache0.opts = c('include', 'out.width.px', 'out.height.px', 'cache.rebuild')

block_exec = function(options) {
if (options$engine == 'R') return(eng_r(options))

# when code is not R language
if (options$engine != 'R') {
res.before = run_hooks(before = TRUE, options)
engine = get_engine(options$engine)
output = in_dir(input_dir(), engine(options))
if (is.list(output)) output = unlist(output)
res.after = run_hooks(before = FALSE, options)
output = paste(c(res.before, output, res.after), collapse = '')
output = knit_hooks$get('chunk')(output, options)
if (options$cache) {
cache.exists = cache$exists(options$hash, options$cache.lazy)
if (options$cache.rebuild || !cache.exists) block_cache(options, output, switch(
options$engine,
'stan' = options$output.var, 'sql' = options$output.var, character(0)
))
}
return(if (options$include) output else '')
res.before = run_hooks(before = TRUE, options)
engine = get_engine(options$engine)
output = in_dir(input_dir(), engine(options))
if (is.list(output)) output = unlist(output)
res.after = run_hooks(before = FALSE, options)
output = paste(c(res.before, output, res.after), collapse = '')
output = knit_hooks$get('chunk')(output, options)
if (options$cache) {
cache.exists = cache$exists(options$hash, options$cache.lazy)
if (options$cache.rebuild || !cache.exists) block_cache(options, output, switch(
options$engine,
'stan' = options$output.var, 'sql' = options$output.var, character(0)
))
}
if (options$include) output else ''
}

#' Engine for R
#'
#' This function handles the execution of R code blocks (when the chunk option
#' \code{engine} is \code{'R'}) and generates the R output for each code block.
#'
#' This engine function has one argument \code{options}: the source code of the
#' current chunk is in \code{options$code}. It returns a processed output that
#' can consist of data frames (as tables), graphs, or character output. This
#' function is intended for advanced use to allow developers to extend R, and
#' customize the pipeline with which R code is executed and processed within
#' knitr.
#'
#' @param options A list of chunk options. Usually this is just the object
#' \code{options} associated with the current code chunk.
#' @noRd
eng_r = function(options) {
# eval chunks (in an empty envir if cache)
env = knit_global()
obj.before = ls(globalenv(), all.names = TRUE) # global objects before chunk
Expand Down
2 changes: 1 addition & 1 deletion R/engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ knit_engines$set(
c = eng_shlib, cc = eng_shlib, fortran = eng_shlib, fortran95 = eng_shlib, asy = eng_dot,
cat = eng_cat, asis = eng_asis, stan = eng_stan, block = eng_block,
block2 = eng_block2, js = eng_js, css = eng_css, sql = eng_sql, go = eng_go,
python = eng_python, julia = eng_julia, sass = eng_sxss, scss = eng_sxss
python = eng_python, julia = eng_julia, sass = eng_sxss, scss = eng_sxss, R = eng_r
)

cache_engines$set(python = cache_eng_python)
Expand Down
2 changes: 1 addition & 1 deletion man/knit_filter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.