Skip to content

Commit

Permalink
Hacky fix for inline images
Browse files Browse the repository at this point in the history
  • Loading branch information
rundel committed Apr 28, 2021
1 parent 2c759e8 commit fa19d2e
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions R/base.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,32 @@ rep_magick_image <- function(x, times){
invisible()
}



# This is registered as an S3 method in .onLoad()
"knit_print.magick-image" <- function(x, ...){
"knit_print.magick-image" <- function(x, ..., inline = FALSE){
if(!length(x))
return(invisible())
plot_counter <- utils::getFromNamespace('plot_counter', 'knitr')

in_base_dir <- utils::getFromNamespace('in_base_dir', 'knitr')
ext <- ifelse(all(tolower(image_info(x)$format) == "gif"), "gif", "png")
tmp <- knitr::fig_path(ext, number = plot_counter())

if (inline) {
fig_path <- knitr::opts_chunk$get("fig.path")
n <- knitr::opts_chunk$get("fig.inline.cur")
if (is.null(n)) {
n <- 1
}
knitr::opts_chunk$set("fig.inline.cur" = n+1)

tmp <- file.path(
fig_path,
paste0("inline-fig-", n, ".", ext)
)
} else {
plot_counter <- utils::getFromNamespace('plot_counter', 'knitr')
tmp <- knitr::fig_path(ext, number = plot_counter())
}

# save relative to 'base' directory, see discussion in #110
in_base_dir({
Expand Down

0 comments on commit fa19d2e

Please sign in to comment.