diff --git a/DESCRIPTION b/DESCRIPTION index c6e4a0d..6c7c982 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: knitr Type: Package Title: A General-Purpose Package for Dynamic Report Generation in R -Version: 1.38 +Version: 1.39 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"), @@ -123,7 +123,7 @@ Encoding: UTF-8 VignetteBuilder: knitr SystemRequirements: Package vignettes based on R Markdown v2 or reStructuredText require Pandoc (http://pandoc.org). The - function rst2pdf() require rst2pdf + function rst2pdf() requires rst2pdf (https://github.com/rst2pdf/rst2pdf). Collate: 'block.R' 'cache.R' 'utils.R' 'citation.R' 'hooks-html.R' 'plot.R' 'defaults.R' 'concordance.R' 'engine.R' 'highlight.R' @@ -135,7 +135,7 @@ Collate: 'block.R' 'cache.R' 'utils.R' 'citation.R' 'hooks-html.R' 'utils-sweave.R' 'utils-upload.R' 'utils-vignettes.R' 'zzz.R' RoxygenNote: 7.1.2 NeedsCompilation: no -Packaged: 2022-03-25 12:35:53 UTC; yihui +Packaged: 2022-04-26 22:08:17 UTC; yihui Author: Yihui Xie [aut, cre] (), Abhraneel Sarma [ctb], Adam Vogt [ctb], @@ -241,4 +241,4 @@ Author: Yihui Xie [aut, cre] (), Zhian N. Kamvar [ctb] () Maintainer: Yihui Xie Repository: CRAN -Date/Publication: 2022-03-25 14:00:02 UTC +Date/Publication: 2022-04-26 23:30:05 UTC diff --git a/MD5 b/MD5 index 9d89782..bf071ce 100644 --- a/MD5 +++ b/MD5 @@ -1,4 +1,4 @@ -4c0de3016d1024fb2a37f3c903f88f36 *DESCRIPTION +3cf6c3a046834ecb0646afea8a0a46b3 *DESCRIPTION f1f4b27531a2429eb2c46381fe02df9d *NAMESPACE 0fdd11c84ce09047d9cb3addea84765d *R/block.R 50388dd9a65dbaf76e3cdd3cab397cdc *R/cache.R @@ -6,7 +6,7 @@ f1f4b27531a2429eb2c46381fe02df9d *NAMESPACE d3066cdaec8d60368dfa30093222ac88 *R/concordance.R aad10e1851ac2ada787769b3ee6e22be *R/defaults.R a7f8b20eefad1444e2a2e17b04b37dd2 *R/engine.R -035c878dfd98d5fa729d4b259fee6c4c *R/header.R +205de0c01e6e23cf8eb93d0dc279a646 *R/header.R 73691d9549452b4c91bb3b27362a74b3 *R/highlight.R ac3146904a77572f10bc89469187e5ec *R/hooks-asciidoc.R ad8783b17b5906fc6c07f09f4d296f7b *R/hooks-chunk.R @@ -17,26 +17,26 @@ d928dba427e23df36670dff7e3d3af75 *R/hooks-html.R b6ba14a52efac7c61dbe6b52292645a1 *R/hooks-rst.R 4b7cc5acf13b4964d371f09d3b01f1d2 *R/hooks-textile.R d099970247f697104c86564dafb6c54a *R/hooks.R -c30906c3ca77224df46aabd3b3615955 *R/output.R +b056a5192f2f26e842dfa1644bd7fad8 *R/output.R a4ab9f478c9d7f460c8a6b4dda698f54 *R/package.R ee33d2131b9dd8e9ceaa043d83fa7390 *R/pandoc.R 62a8271ab2bb4cb847b002d2735c5761 *R/params.R -d4dda063c1d95697b244b98a5d306a34 *R/parser.R +c06d6a9df319d255561fd36e2c0bfaab *R/parser.R e28c0a1ed473604567f7a209ef67b86a *R/pattern.R -a5b6a6fdbabfdcc2aed68829f382ab0d *R/plot.R +f20b117fcd47625e48c90d20867b6b34 *R/plot.R b1cd5daa73317f3fbdf958376e145a29 *R/rocco.R b5f7fa8f6e9846073b747b4176ee56be *R/spin.R ef24634dfd66c7fa616651d6d29e3d9e *R/table.R 5f50b6bde0f31e7f3499e10260a22635 *R/template.R -ee48d4060f9c5e7dc8b8c67f272eeebe *R/themes.R -641c81e294296b716a204c5a7d71a923 *R/utils-conversion.R +cff7000295ea6bf361f0d0f24da1e35a *R/themes.R +12b8c5b88388b945495cdc0df59f0ad2 *R/utils-conversion.R 1d4a50e3977e2f2016772f179e2de15f *R/utils-rd2html.R aeb29eb209d25167087606e2f5401cd9 *R/utils-sweave.R 6bc1fb2aafcadb9c634aa8f640e54887 *R/utils-upload.R 4059016d909ffff9d8c6c39a8cc1ebd9 *R/utils-vignettes.R fe59f6f23a32431f12de538313c728d6 *R/utils.R 64580757ed828267f6976d73231f1f8f *R/zzz.R -bf828a0d235c246446231e94fc2ea1c6 *build/vignette.rds +3328c60d9ae6041e9a9b1250b8330eaf *build/vignette.rds 5320cec79c972798fe0eb5579eb51927 *demo/00Index a0d48f6ce89f9e1d39ee7248ff8c5511 *demo/gwidgets.R a95e58617a42ea5ea95fa99a10f1c120 *demo/notebook.R @@ -54,21 +54,21 @@ b47c340c4060c759ed158eeb0c258560 *inst/doc/docco-classic.Rmd f79998a91f3a618886659010dda5d987 *inst/doc/docco-linear.html 3e4b708e32004e6634e71a946d2b7d72 *inst/doc/knit_expand.R 4774af06c0d07027763d929823c5c384 *inst/doc/knit_expand.Rmd -a7304fef2546761cfceeda4814df6a52 *inst/doc/knit_expand.html +40e69c6014378dfb3a6ebd82e6954f01 *inst/doc/knit_expand.html 7589790b62a05f2f0e47e5ee939ac6e6 *inst/doc/knit_print.R 2ee45ca25b0161540ce8df5288d8fa15 *inst/doc/knit_print.Rmd -a9b78c3eaad4cad077bdc6420387e3b9 *inst/doc/knit_print.html +9b0c67c167f3cb0e9c3ec61a89fcbe04 *inst/doc/knit_print.html 769d05c90e0867719cdee64025753b53 *inst/doc/knitr-html.R 824b923dd4a500fb460a2366aabfb19c *inst/doc/knitr-html.Rhtml ed7a436946acedc07947ed3a01974999 *inst/doc/knitr-html.html 23404d775025e833ea59a5e08270fc0e *inst/doc/knitr-intro.R 89f153ccf155fca8ecb1eb304da5c29c *inst/doc/knitr-intro.Rmd -6138eff87ffb4ca8a3c91bbd82c646f7 *inst/doc/knitr-intro.html +293696fb7aad17c34790d38d1680d0c5 *inst/doc/knitr-intro.html 3c00ba068a4599f959ba80e7beb0c758 *inst/doc/knitr-markdown.R 36707482a23e2739b09033bdd35ebbe7 *inst/doc/knitr-markdown.Rmd -be8bb342b013211346dd6d740955ef99 *inst/doc/knitr-markdown.html +8409220ab43b411e6e521ba2e77cfde5 *inst/doc/knitr-markdown.html e2713b536f8a81e1071198c985475d5d *inst/doc/knitr-refcard.Rmd -a5ce551b629f49dbbe35d5f5842127b1 *inst/doc/knitr-refcard.pdf +bd0a463602d60f0447e6bdc3a10dd95a *inst/doc/knitr-refcard.pdf f16d37ed927336b920c67ce2e958ffc5 *inst/examples/README.md 80d81165446ea535b4368b5f6375d7a1 *inst/examples/Sweavel.sty 5cc86725bad53543ab7cc881a2783269 *inst/examples/child/knitr-child-a.Rnw @@ -119,7 +119,7 @@ e185911fcf189089aa9b39216c652c9c *inst/misc/knitr-template.Rhtml 8da99e02dfa98e2b973985e2beefb518 *inst/misc/knitr-template.Rmd bdd0af9d06749374afb3d36a5916d191 *inst/misc/knitr-template.Rnw e3e31286dead44f4d14e6c6ce0b869d2 *inst/misc/knitr.css -dc39973a7d46bb9c7a1c9172f9f244bf *inst/misc/knitr.sty +9492f7e2a18eb0c1b0ba836638cc1085 *inst/misc/knitr.sty 3510b0b40df85ecc14ba83682eb215c6 *inst/misc/stitch-test.R 37b977988aade59295d956751e6a4d0a *inst/misc/tikz2pdf.tex e1bcfa4fa59e6027485d6761cef388ae *inst/misc/toggleR.js @@ -235,7 +235,7 @@ caefcb554d3691e5a7c92bca5bb9db5d *man/hook_document.Rd 71300a86c312a2949bc50393772cf1c6 *man/hook_plot.Rd 7f3b313ee6f7a592d7319198d808e857 *man/image_uri.Rd 89e254a6b0162f677a920dd6054a4ed9 *man/imgur_upload.Rd -5da5916d987fbf2d8d14817629d92a4e *man/include_graphics.Rd +7e498e79dfb695cb00582cf448bbab25 *man/include_graphics.Rd 8bdd9512d57176c507216419eab1858e *man/include_url.Rd 339b19fe49695c2640a3876542a13142 *man/inline_expr.Rd 3d8b2f2793a4869444669d2fba2fda6e *man/is_low_change.Rd @@ -306,7 +306,7 @@ c7306a791facc53dbe80cc5b6c244293 *tests/testit/test-output.R ef2beb254ea8bcf4ff1df40b47468d5d *tests/testit/test-params.R d1387ef859e6f621f77b1124403af19c *tests/testit/test-parser.R 186581c73fbcc4f99770517bf638db4b *tests/testit/test-patterns.R -b2a4f15cc8f9e70701033cb28b7e50fc *tests/testit/test-plot.R +ad2678c42641605592b9380c89692c1c *tests/testit/test-plot.R 09028e8ca1d0b3fe013d5af7ef3ab819 *tests/testit/test-spin.R 233af33c6f30c6966d2fceaf0809e22a *tests/testit/test-sql.R 070462314b8d7a331ba71acf1b6e3d66 *tests/testit/test-table.R diff --git a/R/header.R b/R/header.R index 962cc0c..2b063eb 100644 --- a/R/header.R +++ b/R/header.R @@ -13,11 +13,9 @@ insert_header = function(doc) { } # Makes latex header with macros required for highlighting, tikz and framed -make_header_latex = function() { +make_header_latex = function(doc) { h = one_string(c( - sprintf('\\usepackage[%s]{graphicx}\\usepackage[%s]{color}', - opts_knit$get('latex.options.graphicx') %n% '', - opts_knit$get('latex.options.color') %n% ''), + header_latex_packages(doc), .header.maxwidth, opts_knit$get('header'), if (getOption('OutDec') != '.') '\\usepackage{amsmath}', if (out_format('latex')) '\\usepackage{alltt}' @@ -28,6 +26,24 @@ make_header_latex = function() { } } +# if the document already contains \usepackage[options]{pkg}, use the same +# options to avoid the option clash error in LaTeX +use_package = function(pkg, doc) { + opts = sapply(pkg, function(p) { + r = sprintf('.*?\\\\usepackage\\[(.+?)]\\{%s}.*', p) + o = xfun::grep_sub(r, '\\1', doc) + if (length(o)) return(o[1]) + opts_knit$get(paste0('latex.options.', p)) %n% '' + }) + sprintf('\\usepackage[%s]{%s}', opts, pkg) +} + +# for backward-compatibility, use xcolor package unless the latex.options.color +# has been set; xcolor is preferred: https://github.com/latex3/latex2e/pull/719 +header_latex_packages = function(doc) { + paste(use_package(c('graphicx', 'xcolor'), doc), collapse = '') +} + insert_header_latex = function(doc, b) { i = grep(b, doc) if (length(i) >= 1L) { @@ -41,11 +57,11 @@ insert_header_latex = function(doc, b) { } i = i[1L]; l = stringr::str_locate(doc[i], b) tmp = stringr::str_sub(doc[i], l[, 1], l[, 2]) - stringr::str_sub(doc[i], l[,1], l[,2]) = paste0(tmp, make_header_latex()) + stringr::str_sub(doc[i], l[,1], l[,2]) = paste0(tmp, make_header_latex(doc)) } else if (parent_mode() && !child_mode()) { # in parent mode, we fill doc to be a complete document doc[1L] = one_string(c( - getOption('tikzDocumentDeclaration'), make_header_latex(), + getOption('tikzDocumentDeclaration'), make_header_latex(doc), .knitEnv$tikzPackages, '\\begin{document}', doc[1L] )) doc[length(doc)] = one_string( diff --git a/R/output.R b/R/output.R index b928bc6..efab254 100644 --- a/R/output.R +++ b/R/output.R @@ -139,11 +139,10 @@ knit = function( input = file.path(input_dir(), input) } # respect the quiet argument in child mode (#741) - optk = opts_knit$get(); on.exit(opts_knit$set(optk), add = TRUE) + optk = opts_knit$get(); on.exit(opts_knit$restore(optk), add = TRUE) opts_knit$set(progress = opts_knit$get('progress') && !quiet) quiet = !opts_knit$get('progress') } else { - opts_knit$set(output.dir = getwd()) # record working directory in 1st run knit_log$restore() on.exit(chunk_counter(reset = TRUE), add = TRUE) # restore counter adjust_opts_knit() @@ -156,8 +155,11 @@ knit = function( optc = opts_chunk$get(); on.exit(opts_chunk$restore(optc), add = TRUE) ocode = knit_code$get(); on.exit(knit_code$restore(ocode), add = TRUE) on.exit(opts_current$restore(), add = TRUE) - optk = opts_knit$get(); on.exit(opts_knit$set(optk), add = TRUE) - opts_knit$set(tangle = tangle, progress = opts_knit$get('progress') && !quiet) + optk = opts_knit$get(); on.exit(opts_knit$restore(optk), add = TRUE) + opts_knit$set( + output.dir = getwd(), # record working directory in 1st run + tangle = tangle, progress = opts_knit$get('progress') && !quiet + ) } # store the evaluation environment and restore on exit oenvir = .knitEnv$knit_global; .knitEnv$knit_global = envir diff --git a/R/parser.R b/R/parser.R index 7fcbeeb..083d303 100644 --- a/R/parser.R +++ b/R/parser.R @@ -276,7 +276,7 @@ partition_chunk = function(engine, code) { meta = substr(src, nchar(s1) + 1, nchar(src) - nchar(s2)) # see if the metadata looks like YAML or CSV if (grepl('^[^ :]+:($|\\s)', meta[1])) { - meta = yaml::yaml.load(meta, eval.expr = TRUE) + meta = yaml::yaml.load(meta, handlers = list(expr = parse_only)) if (!is.list(meta) || length(names(meta)) == 0) { warning('Invalid YAML option format in chunk: \n', one_string(meta), '\n') meta = list() diff --git a/R/plot.R b/R/plot.R index 0ae34ae..4377441 100644 --- a/R/plot.R +++ b/R/plot.R @@ -438,6 +438,10 @@ par2 = function(x) { #' inches) of the images. This will be their actual width in pixels, divided #' by \code{dpi}. If not provided, the chunk option \code{dpi} is used; if #' \code{NA}, the output width will not be calculated. +#' @param rel_path Whether to automatically convert absolute paths to relative +#' paths. If you know for sure that absolute paths work, you may set this +#' argument or the global option \code{knitr.graphics.rel_path} to +#' \code{FALSE}. #' @param error Whether to signal an error if any files specified in the #' \code{path} argument do not exist and are not web resources. #' @note This function is supposed to be used in R code chunks or inline R code @@ -455,13 +459,17 @@ par2 = function(x) { #' @export include_graphics = function( path, auto_pdf = getOption('knitr.graphics.auto_pdf', FALSE), dpi = NULL, + rel_path = getOption('knitr.graphics.rel_path', TRUE), error = getOption('knitr.graphics.error', TRUE) ) { path = native_encode(path) # https://d.cosx.org/d/420524 - if (any(i <- xfun::is_abs_path(path))) warning( - 'It is highly recommended to use relative paths for images. ', - 'You had absolute paths: ', quote_vec(path[i]) - ) + if (any(i <- xfun::is_abs_path(path)) && rel_path && !is.null(d <- opts_knit$get('output.dir'))) { + path[i] = xfun::relative_path(path[i], d, error = FALSE) + if (any(j <- xfun::is_abs_path(path[i]))) warning( + 'It is highly recommended to use relative paths for images. ', + 'You had absolute paths: ', quote_vec(path[i][j]) + ) + } path = path.expand(path) # https://github.com/rstudio/rmarkdown/issues/1053 if (auto_pdf && is_latex_output()) { path2 = with_ext(path, 'pdf') diff --git a/R/themes.R b/R/themes.R index 1c82710..ee78750 100644 --- a/R/themes.R +++ b/R/themes.R @@ -70,9 +70,6 @@ theme_to_header_latex = function(theme) { # write latex highlight header fgheader = color_def(foreground, 'fgcolor') - fgheader = c(fgheader, '\\makeatletter', sprintf( - '\\@ifundefined{AddToHook}{}{\\AddToHook{package/xcolor/after}{%s}}', fgheader - ), '\\makeatother') highlight = one_string(c(fgheader, styler_assistant_latex(css_out[-1]))) list(highlight = highlight, background = background, foreground = foreground) } diff --git a/R/utils-conversion.R b/R/utils-conversion.R index 24ac6f9..cf14165 100644 --- a/R/utils-conversion.R +++ b/R/utils-conversion.R @@ -115,8 +115,7 @@ rnw2pdf = function( # On Windows, when tweaking the content, users may forget to close the PDF # file (thus can't be written). Since knitting may take quite some time, it's # better to check the write permission of the output file in advance. - file.remove(output) - if (xfun::file_exists(output)) stop( + if (xfun::file_exists(output) && !file.remove(output)) stop( "The file '", output, "' cannot be removed (may be locked by a PDF reader)." ) old = opts_chunk$set(error = error) diff --git a/build/vignette.rds b/build/vignette.rds index c58c7e6..dc8aa52 100644 Binary files a/build/vignette.rds and b/build/vignette.rds differ diff --git a/inst/doc/knit_expand.html b/inst/doc/knit_expand.html index 20d5fb8..3345ec8 100644 --- a/inst/doc/knit_expand.html +++ b/inst/doc/knit_expand.html @@ -220,7 +220,7 @@

Demos of knit_expand()

knit_expand(text = 'The value of a is {{a}}, so a + 1 is {{a+1}}.', a = rnorm(1))
 
-
## [1] "The value of a is 1.14969284175351, so a + 1 is 2.14969284175351."
+
## [1] "The value of a is -0.457571911722792, so a + 1 is 0.542428088277208."
 
knit_expand(text = 'The area of a circle with radius {{r}} is {{pi*r^2}}', r = 5)
@@ -258,7 +258,7 @@ 

Demos of knit_expand()

knit_expand(text = 'you cannot see the value of x {{x=rnorm(1)}}but it is indeed created: x = {{x}}')
 
-
## [1] "you cannot see the value of x but it is indeed created: x = -0.418363940772454"
+
## [1] "you cannot see the value of x but it is indeed created: x = -0.995003890720546"
 
res = knit_expand(text = c(' x | x^2', '{{x=1:5;paste(sprintf("%2d | %3d", x, x^2), collapse = "\n")}}'))
diff --git a/inst/doc/knit_print.html b/inst/doc/knit_print.html
index 4eee14b..dd4d96b 100644
--- a/inst/doc/knit_print.html
+++ b/inst/doc/knit_print.html
@@ -12,7 +12,7 @@
 
 
 
-
+
 
 Custom Print Methods
 
@@ -372,17 +372,20 @@
 
 

Custom Print Methods

Yihui Xie

-

2022-03-25

+

2022-04-26

@@ -423,7 +426,7 @@

Customize Printing

## UseMethod("knit_print") ## } ## } -## <bytecode: 0x7fd3542685a8> +## <bytecode: 0x7fe7362ac0e0> ## <environment: namespace:knitr>
methods(knit_print)
##  [1] knit_print.css*            knit_print.data.frame*    
@@ -441,12 +444,12 @@ 

Customize Printing

## x ## else normal_print(x) ## } -## <bytecode: 0x7fd34ea68028> +## <bytecode: 0x7fe7319d6248> ## <environment: namespace:knitr>
normal_print
## function (x, ...) 
 ## if (isS4(x)) methods::show(x) else print(x)
-## <bytecode: 0x7fd3511a9eb8>
+## <bytecode: 0x7fe731741748>
 ## <environment: namespace:evaluate>

As we can see, knit_print() has a default method, which is basically print() or show(), @@ -830,7 +833,7 @@

For package authors

## { ## structure(x, class = "knit_asis", knit_meta = meta, knit_cacheable = cacheable) ## } -## <bytecode: 0x7fd350d12b30> +## <bytecode: 0x7fe7352cecd0> ## <environment: namespace:knitr>

Note that you can actually put knitr in the Suggests field in DESCRIPTION, and use diff --git a/inst/doc/knitr-intro.html b/inst/doc/knitr-intro.html index afd5429..ce2c940 100644 --- a/inst/doc/knitr-intro.html +++ b/inst/doc/knitr-intro.html @@ -12,7 +12,7 @@ - + Not An Introduction to knitr @@ -394,7 +394,7 @@

Not An Introduction to knitr

Yihui Xie

-

2022-03-25

+

2022-04-26

@@ -410,9 +410,9 @@

2022-03-25

non-Sweave vignettes:

options(digits = 4)
 rnorm(20)
-
##  [1] -0.73974 -1.14359 -0.23914  0.05636 -0.75331 -0.47152 -0.50625 -0.10183
-##  [9]  0.43507 -1.93467  0.79913  1.85079 -1.70558  0.15422  0.03602  0.33003
-## [17]  0.74745 -0.71951  0.95464  0.63750
+
##  [1] -0.432492 -1.934594  0.090109 -0.537050  0.349098  0.156540 -0.202615
+##  [8] -2.024916 -0.940910  0.602132  0.624322 -0.422067 -0.706236 -1.168691
+## [15] -0.481862  0.272402 -0.528331 -0.497320 -0.008859 -1.556787
fit = lm(dist ~ speed, data = cars)
 b = coef(fit)
diff --git a/inst/doc/knitr-markdown.html b/inst/doc/knitr-markdown.html index bc2170a..4d838b2 100644 --- a/inst/doc/knitr-markdown.html +++ b/inst/doc/knitr-markdown.html @@ -250,7 +250,7 @@

Examples

10:1 ## [1] 10 9 8 7 6 5 4 3 2 1 rnorm(5)^2 -## [1] 0.022662 0.079285 0.061534 1.583053 0.003948 +## [1] 1.65565 5.03669 0.01342 0.13085 0.41971 strsplit('hello, markdown vignettes', '') ## [[1]] ## [1] "h" "e" "l" "l" "o" "," " " "m" "a" "r" "k" "d" "o" "w" "n" " " "v" "i" "g" diff --git a/inst/doc/knitr-refcard.pdf b/inst/doc/knitr-refcard.pdf index 52c0f86..dc2c326 100644 Binary files a/inst/doc/knitr-refcard.pdf and b/inst/doc/knitr-refcard.pdf differ diff --git a/inst/misc/knitr.sty b/inst/misc/knitr.sty index 9a9370e..17fbc21 100644 --- a/inst/misc/knitr.sty +++ b/inst/misc/knitr.sty @@ -21,13 +21,5 @@ \definecolor{messagecolor}{rgb}{0, 0, 0} \definecolor{warningcolor}{rgb}{1, 0, 1} \definecolor{errorcolor}{rgb}{1, 0, 0} -\makeatletter -\@ifundefined{AddToHook}{}{\AddToHook{package/xcolor/after}{ -\definecolor{shadecolor}{rgb}{.97, .97, .97} -\definecolor{messagecolor}{rgb}{0, 0, 0} -\definecolor{warningcolor}{rgb}{1, 0, 1} -\definecolor{errorcolor}{rgb}{1, 0, 0} -}} -\makeatother \newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX diff --git a/man/include_graphics.Rd b/man/include_graphics.Rd index 98d4fb7..f396049 100644 --- a/man/include_graphics.Rd +++ b/man/include_graphics.Rd @@ -8,6 +8,7 @@ include_graphics( path, auto_pdf = getOption("knitr.graphics.auto_pdf", FALSE), dpi = NULL, + rel_path = getOption("knitr.graphics.rel_path", TRUE), error = getOption("knitr.graphics.error", TRUE) ) } @@ -27,6 +28,11 @@ inches) of the images. This will be their actual width in pixels, divided by \code{dpi}. If not provided, the chunk option \code{dpi} is used; if \code{NA}, the output width will not be calculated.} +\item{rel_path}{Whether to automatically convert absolute paths to relative +paths. If you know for sure that absolute paths work, you may set this +argument or the global option \code{knitr.graphics.rel_path} to +\code{FALSE}.} + \item{error}{Whether to signal an error if any files specified in the \code{path} argument do not exist and are not web resources.} } diff --git a/tests/testit/test-plot.R b/tests/testit/test-plot.R index 2236700..e750146 100644 --- a/tests/testit/test-plot.R +++ b/tests/testit/test-plot.R @@ -139,9 +139,8 @@ if (xfun::loadable('tikzDevice') && # https://github.com/yihui/knitr/issues/1166 knit(text = "\\Sexpr{include_graphics('myfigure.pdf', error = FALSE)}", quiet = TRUE) -assert('include_graphics() warns on absolute paths and expands ~', { +assert('include_graphics() expands ~', { path1 = "~/test.png" - (has_warning(include_graphics(path1, error = FALSE))) (!has_warning(include_graphics("img/test.png", error = FALSE))) (unclass(suppressWarnings(include_graphics(path1, error = FALSE))) %==% path.expand(path1)) })