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

Rendering error for Rmd vignettes using knitr::include_graphics #1797

Closed
3 tasks done
hpages opened this issue Jan 18, 2020 · 8 comments
Closed
3 tasks done

Rendering error for Rmd vignettes using knitr::include_graphics #1797

hpages opened this issue Jan 18, 2020 · 8 comments

Comments

@hpages
Copy link

hpages commented Jan 18, 2020

By filing an issue to this repo, I promise that

  • I have fully read the issue guide at https://yihui.name/issue/.
  • I have provided the necessary information about my issue.
    • If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    • If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included xfun::session_info('knitr'). I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: remotes::install_github('yihui/knitr').
    • If I have posted the same issue elsewhere, I have also mentioned it in this issue.
  • I have learned the Github Markdown syntax, and formatted my issue correctly.

I understand that my issue may be closed if I don't fulfill my promises.


Hi,

Starting with knitr 1.27 we started to see new vignette errors in several Bioconductor packages in release (BioC 3.10, based on R 3.6) and devel (BioC 3.11, based on R 4.0).

Here is how to reproduce:

  1. Create a minimalist Rmd vignette that uses knitr::include_graphics:

    jpeg(file="myplot.jpeg")
    plot(1:10)
    rect(1, 5, 3, 7, col = "white")
    dev.off()
    
    Test_vignette.Rmd <- c(
        "---",
        "title: \"Test vignette\"",
        "---",
        "```{r chunk1}",
        "knitr::include_graphics(\"myplot.jpeg\")",
        "```")
    cat(Test_vignette.Rmd, sep="\n", file="Test_vignette.Rmd")
    
  2. Rendering the vignette with rmarkdown::html_document works fine:

    library(rmarkdown)
    rmarkdown::render("Test_vignette.Rmd", rmarkdown::html_document())
    
  3. Rendering the vignette with rmarkdown::pdf_document triggers a non-fatal error:

    rmarkdown::render("Test_vignette.Rmd", rmarkdown::pdf_document())
    

    Output:

    processing file: Test_vignette.Rmd
      |...................................                                   |  50%
      ordinary text without R code
    
      |......................................................................| 100%
    label: chunk1
    !!! Error: Input file `Test_vignette_files/figure-latex/chunk1-1.pdf' not found!
    
    output file: Test_vignette.knit.md
    
    /usr/bin/pandoc +RTS -K512m -RTS Test_vignette.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output Test_vignette.tex --self-contained --highlight-style tango --pdf-engine pdflatex --variable graphics --lua-filter /home/biocbuild/bbs-3.10-bioc/R/library/rmarkdown/rmd/lua/pagebreak.lua --lua-filter /home/biocbuild/bbs-3.10-bioc/R/library/rmarkdown/rmd/lua/latex-div.lua --variable 'geometry:margin=1in' 
    
    Output created: Test_vignette.pdf
    
  4. Trying to render the vignette again with rmarkdown::html_document after enabling cropping now produces a fatal error:

    html_document_crop <- rmarkdown::html_document()
    html_document_crop$knitr$knit_hooks <- list(crop=knitr::hook_pdfcrop)
    html_document_crop$knitr$opts_chunk$crop <- TRUE
    rmarkdown::render("Test_vignette.Rmd", html_document_crop)
    

    Output:

    processing file: Test_vignette.Rmd
      |...................................                                   |  50%
      ordinary text without R code
    
      |......................................................................| 100%
    label: chunk1
    Quitting from lines 5-6 (Test_vignette.Rmd) 
    Error in magick_image_readpath(enc2native(path), density, depth, strip) : 
      Magick: unable to open image `Test_vignette_files/figure-html/chunk1-1.png': No such file or directory @ error/blob.c/OpenBlob/2701
    

magick is installed (see sessionInfo() below) so this is not the same as issue #1796

Note that the issue was originally reported as a BiocStyle issue but the self-contained example above seems to exonerate BiocStyle.

Thanks,
H.

sessionInfo:

R version 3.6.2 (2019-12-12)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.3 LTS

Matrix products: default
BLAS:   /home/biocbuild/bbs-3.10-bioc/R/lib/libRblas.so
LAPACK: /home/biocbuild/bbs-3.10-bioc/R/lib/libRlapack.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rmarkdown_2.0

loaded via a namespace (and not attached):
 [1] compiler_3.6.2  magrittr_1.5    magick_2.2      tools_3.6.2    
 [5] htmltools_0.4.0 yaml_2.2.0      Rcpp_1.0.3      stringi_1.4.5  
 [9] jpeg_0.1-8.1    knitr_1.27      stringr_1.4.0   xfun_0.12      
[13] digest_0.6.23   rlang_0.4.2     evaluate_0.14  
@yihui
Copy link
Owner

yihui commented Jan 20, 2020

Should be fixed now. Thanks for the report! You may try the dev version for now:

remotes::install_github('yihui/knitr')

@salim-b
Copy link
Contributor

salim-b commented Jan 21, 2020

@yihui I also ran into this bug after updating to knitr 1.27. Then I updated to the dev version (1.27.2) and stumpled upon another issue I'd call a bug, too:

It seems that knitr is always overwriting the file included through knitr::include_graphics(). I don't know if this was already the case before knitr 1.27. I noticed it because I tried to build an MVE for the original bug using knitr's reference card PDF vignette as follows:

knitr::include_graphics(paste0(find.package(package = "knitr"), "/doc/knitr-refcard.pdf"))

Since I've installed knitr through Michael Rutter's PPA (Ubuntu Linux), on my system the above is equivalent to:

knitr::include_graphics("/usr/lib/R/site-library/knitr/doc/knitr-refcard.pdf")

When I try to build this, the following error is thrown:

!!! Error: Cannot move `tmp-pdfcrop-6024.pdf' to `/usr/lib/R/site-library/knitr/doc/knitr-refcard.pdf'!

This is of course because knitr isn't run as root, but the file is owned by root and knitr doesn't have write permission:

$ ls -1l /usr/lib/R/site-library/knitr/doc/knitr-refcard.pdf
-rw-r--r-- 1 root root 79531 Jan 19 04:04 /usr/lib/R/site-library/knitr/doc/knitr-refcard.pdf

I verified this with a file knitr actually has write permission to – it gets overwritten when included through knitr::include_graphics()! The documentation of knitr::include_graphics() doesn't say anything about possible file modifications of param path and I believe it was never really intended to overwrite path, right?


Addendum: I've just read about the crop option in #1796 and setting knitr::opts_chunk$set(crop = NULL) avoids knitr's attempt to overwrite the included PDF.

I think it's fine that knitr automatically crops whitespace around the files specified in knitr::include_graphics() but it should still never overwrite the original files!

And is there a specific reason why crop isn't a "proper" chunk options like e.g. fig.path?

@hpages
Copy link
Author

hpages commented Jan 21, 2020

and stumpled upon another issue

Have you considered opening a new issue for that?

@salim-b
Copy link
Contributor

salim-b commented Jan 21, 2020

Have you considered opening a new issue for that?

I thought it might make sense to report it here since it seems closely related to the original issue you reported. But of course I can open a new issue if that's preferred...

@hpages
Copy link
Author

hpages commented Jan 21, 2020

I think it's preferred, especially since the specific issue I reported here has been fixed and closed. Thanks!

@salim-b
Copy link
Contributor

salim-b commented Jan 21, 2020

I think it's preferred, especially since the specific issue I reported here has been fixed and closed. Thanks!

Ok then: #1800

@yihui
Copy link
Owner

yihui commented Jan 21, 2020

@salim-b Your issue is slightly related to @hpages's issue. If you are unsure, it is always a good idea to file a new issue (so the original poster won't receive unnecessary notifications), and as a hint to the developers, you could mention that the new issue might be related to this issue. Thanks!

And is there a specific reason why crop isn't a "proper" chunk options like e.g. fig.path?

Because cropping images require additional software packages, and I tend to let users opt-in after they know how to install these packages, instead of "officially" supporting this feature.

@github-actions
Copy link

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants