Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
fda0660
added .img.id for image id for epub to try to fix https://github.com/…
muschellij2 Aug 28, 2019
abcea3d
fixing the div to be the id not the img
muschellij2 Aug 28, 2019
f5af631
Merge remote-tracking branch 'upstream/master'
muschellij2 Nov 19, 2019
b8da157
simple path.expand for plot_crop
muschellij2 Nov 19, 2019
45277bd
backing up x in plot_crop and returning original
muschellij2 Nov 19, 2019
c1041ae
pushed
muschellij2 Jun 26, 2020
ebd35df
Merge branch 'master' of https://github.com/muschellij2/knitr into up…
muschellij2 Jun 26, 2020
9840166
split call_block into a function block_params to extract the params t…
muschellij2 Jun 26, 2020
538cd65
added a verbosity argument that you can turn off
muschellij2 Jun 26, 2020
4d64cd0
false trigger to see if fixed knitr
muschellij2 Jul 21, 2020
b9020f1
removed to merge
muschellij2 Jul 21, 2020
ab7d063
Merge remote-tracking branch 'upstream/master'
muschellij2 Jul 21, 2020
92f6e97
remove tweak
muschellij2 Jul 23, 2020
ceb32ef
fixed tweak
muschellij2 Jul 23, 2020
55195ad
reworked block so it gives params but still executes the same way
muschellij2 Jul 23, 2020
9b18fa3
passing through collapse argument
muschellij2 Aug 7, 2020
0a03b67
Merge remote-tracking branch 'upstream/master' into master
muschellij2 Sep 24, 2020
bac20ce
Merge remote-tracking branch 'upstream/master' into master
muschellij2 Oct 15, 2020
41c5322
Merge remote-tracking branch 'upstream/master' into master
muschellij2 Oct 21, 2020
3c7f4e4
updated with upstream
muschellij2 Mar 30, 2021
0f890e2
whitespace removal
muschellij2 Mar 30, 2021
9203c8d
trying again with the img.id options
muschellij2 Mar 30, 2021
2eaa84e
Merge branch 'master' of https://github.com/muschellij2/knitr into up…
muschellij2 Mar 30, 2021
f4e7a13
added collapse - still need to document
muschellij2 Mar 30, 2021
772d7e3
documented collapse
muschellij2 Mar 30, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions R/block.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ process_group.inline = function(x) {
}


call_block = function(block) {
block_params = function(block, verbose = TRUE) {
# now try eval all options except those in eval.after and their aliases
af = opts_knit$get('eval.after'); al = opts_knit$get('aliases')
if (!is.null(al) && !is.null(af)) af = c(af, names(al[af %in% al]))
Expand All @@ -28,7 +28,7 @@ call_block = function(block) {
label = ref.label = params$label
if (!is.null(params$ref.label)) ref.label = sc_split(params$ref.label)
params[["code"]] = params[["code"]] %n% unlist(knit_code$get(ref.label), use.names = FALSE)
if (opts_knit$get('progress')) print(block)
if (opts_knit$get('progress') && verbose > 0) print(block)

if (!is.null(params$child)) {
if (!is_blank(params$code)) warning(
Expand Down Expand Up @@ -74,13 +74,29 @@ call_block = function(block) {
if (opts_knit$get('verbose')) message(' loading cache from ', hash)
cache$load(hash, lazy = params$cache.lazy)
cache_engine(params)
}
} else if (label %in% names(dep_list$get()) && !isFALSE(opts_knit$get('warn.uncached.dep')))
warning2('code chunks must not depend on the uncached chunk "', label, '"')

return(params)
}

call_block = function(block, collapse = '') {
params = block_params(block, verbose = TRUE)

# this is required here because return() in the if
# so that the params are returned in block_params, but
# the same logic execution is done below
if (params$cache > 0) {
if (cache$exists(params$hash, params$cache.lazy) &&
isFALSE(params$cache.rebuild) &&
params$engine != 'Rcpp') {
if (!params$include) return('')
if (params$cache == 3) return(cache$output(hash))
if (params$cache == 3) return(cache$output(params$hash))
}
if (params$engine == 'R')
cache$library(params$cache.path, save = FALSE) # load packages
} else if (label %in% names(dep_list$get()) && !isFALSE(opts_knit$get('warn.uncached.dep')))
warning2('code chunks must not depend on the uncached chunk "', label, '"')
} # warning already triggered above

params$params.src = block$params.src
opts_current$restore(params) # save current options
Expand All @@ -90,7 +106,7 @@ call_block = function(block) {
op = options(params$R.options); on.exit(options(op), add = TRUE)
}

block_exec(params)
block_exec(params, collapse = collapse)
}

# options that should affect cache when cache level = 1,2
Expand All @@ -100,16 +116,17 @@ cache2.opts = c('fig.keep', 'fig.path', 'fig.ext', 'dev', 'dpi', 'dev.args', 'fi
# options that should not affect cache
cache0.opts = c('include', 'out.width.px', 'out.height.px', 'cache.rebuild')

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

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

# when code is not R language
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 = paste(c(res.before, output, res.after), collapse = collapse)
output = knit_hooks$get('chunk')(output, options)
if (options$cache) {
cache.exists = cache$exists(options$hash, options$cache.lazy)
Expand All @@ -135,8 +152,9 @@ block_exec = function(options) {
#'
#' @param options A list of chunk options. Usually this is just the object
#' \code{options} associated with the current code chunk.
#' @param collapse string to collapse output on, passed to \code{\link{paste}}
#' @noRd
eng_r = function(options) {
eng_r = function(options, collapse = '') {
# eval chunks (in an empty envir if cache)
env = knit_global()
obj.before = ls(globalenv(), all.names = TRUE) # global objects before chunk
Expand Down Expand Up @@ -265,7 +283,7 @@ eng_r = function(options) {
output = unlist(wrap(res, options)) # wrap all results together
res.after = run_hooks(before = FALSE, options, env) # run 'after' hooks

output = paste(c(res.before, output, res.after), collapse = '') # insert hook results
output = paste(c(res.before, output, res.after), collapse = collapse) # insert hook results
output = knit_hooks$get('chunk')(output, options)

if (options$cache > 0) {
Expand Down
10 changes: 10 additions & 0 deletions R/hooks-html.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ hook_animation = function(options) {
), cap)
}

.img.id = function(options) {
sprintf(
'id="%s"',
paste0(
options$fig.lp, options$label,
if (options$fig.num > 1L && options$fig.show == 'asis') options$fig.cur
)
)
}

# a wrapper to upload an image and return the URL
.upload.url = function(x) {
opts_knit$get('upload.fun')(x)
Expand Down
3 changes: 2 additions & 1 deletion R/hooks-md.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ hook_plot_md_base = function(x, options) {
}
# use HTML syntax <img src=...>
if (pandoc_html && !isTRUE(grepl('-implicit_figures', from))) {
d1 = if (plot1) sprintf('<div class="figure"%s>\n', css_text_align(a))
d1 = if (plot1) sprintf('<div class="figure"%s %s>\n', css_text_align(a),
.img.id(options))
d2 = sprintf('<p class="caption">%s</p>', cap)
img = sprintf(
'<img src="%s" alt="%s" %s />',
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.