Skip to content

Commit

Permalink
close rstudio/bookdown#118: fig.cap works for HTML widgets now
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed Oct 1, 2016
1 parent 2eb9edd commit 8f7b48a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Suggests:
rgl (>= 0.95.1201),
codetools,
rmarkdown,
htmlwidgets,
htmlwidgets (>= 0.7),
webshot,
tikzDevice (>= 0.10),
png,
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

- added a `block2` engine for R Markdown documents as an alternative to the `block` engine; it should be faster and supports arbitrary Pandoc's Markdown syntax, but it is essentially a hack; note when the output format is LaTeX/PDF, you have to define `\let\BeginKnitrBlock\begin \let\EndKnitrBlock\end` in the LaTeX preamble

- figure captions specified in the chunk option `fig.cap` are also applied to HTML widgets (thanks, @byzheng, https://github.com/rstudio/bookdown/issues/118)

- added a `width` argument to `write_bib()` so long lines in bib entries can be wrapped

- the inline syntax `` `r#code` `` is also supported besides `` `r code` ``; this can make sure the inline expression is not split when the line is wrapped (thanks, Dave Jarvis)
Expand Down
16 changes: 12 additions & 4 deletions R/output.R
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,11 @@ wrap.knit_asis = function(x, options, inline = FALSE) {
# store metadata in an object named of the form .hash_meta when cache=TRUE
if (length(m) && options$cache == 3)
assign(cache_meta_name(options$hash), m, envir = knit_global())
if (inherits(x, 'knit_asis_htmlwidget')) {
options$fig.cur = plot_counter()
options = reduce_plot_opts(options)
return(add_html_caption(options, x))
}
}
x = as.character(x)
if (!out_format('latex') || inline) return(x)
Expand Down Expand Up @@ -595,15 +600,18 @@ wrap.html_screenshot = function(x, options = opts_chunk$get(), inline = FALSE) {
wrap.knit_embed_url = function(x, options = opts_chunk$get(), inline = FALSE) {
options$fig.cur = plot_counter()
options = reduce_plot_opts(options)
iframe = sprintf(
add_html_caption(options, sprintf(
'<iframe src="%s" width="%s" height="%s"></iframe>',
escape_html(x$url), options$out.width %n% '100%', x$height %n% '400px'
)
))
}

add_html_caption = function(options, code) {
cap = .img.cap(options)
if (cap == '') return(iframe)
if (cap == '') return(code)
sprintf(
'<div class="figure"%s>\n%s\n<p class="caption">%s</p>\n</div>',
css_text_align(options$fig.align), iframe, cap
css_text_align(options$fig.align), code, cap
)
}

Expand Down
8 changes: 7 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,13 @@ current_input = function(dir = FALSE) {
default_handlers = evaluate:::default_output_handler
# change the value handler in evaluate default handlers
knit_handlers = function(fun, options) {
if (!is.function(fun)) fun = knit_print
if (!is.function(fun)) fun = function(x, ...) {
res = knit_print(x, ...)
# indicate the htmlwidget result with a special class so we can attach
# the figure caption to it later in wrap.knit_asis
if (inherits(x, 'htmlwidget')) class(res) = c(class(res), 'knit_asis_htmlwidget')
res
}
if (length(formals(fun)) < 2)
stop("the chunk option 'render' must be a function of the form ",
"function(x, options) or function(x, ...)")
Expand Down

0 comments on commit 8f7b48a

Please sign in to comment.