Search code examples
rpngknitr

Cannot use include_graphics to insert png in Rmarkdown (error: file is not in PNG format)


This error is very strange. When I knitr the doc, I got the following error:

Quitting from lines 42-43 (sigminer-doc.Rmd) 
Error in png::readPNG(path, native = TRUE, info = TRUE) : 
  file is not in PNG format

However, I can read the png by png::readPNG and it also works when I run the chunk in RStudio.

enter image description here

Use the knitr button in RStudio for index.Rmd cannot work.

enter image description here

I also reinstall the png, knitr package, the idea cannot work.


UPDATE:

I tried to debug the working directory and add the following code to the header.

knitr::opts_knit$set(root.dir = rprojroot::find_rstudio_root_file())
knitr::knit_hooks$set(debug = function(before, options, envir) {
  if (!before) {
    envir = as.list(envir)
    message("Objects: ", paste(names(envir), collapse = " "))
    for (i in names(envir)) {
      if (!startsWith(i, ".")) {
        message(
          i, " = ", envir[[i]]
        )
      }
    }
  }
})

The working directory is as expected.

Objects: workdir root.dir
workdir = /Users/wsx/Documents/GitHub/sigminer-doc
root.dir = /Users/wsx/Documents/GitHub/sigminer-doc
  |.....                                                                                                                                 |   4%
  ordinary text without R code

  |......                                                                                                                                |   5%
label: unnamed-chunk-3 (with options) 
List of 2
 $ echo   : logi FALSE
 $ fig.cap: chr "The illustration of SBS signature, fig source: https://www.nature.com/articles/nrg3729"

Quitting from lines 64-65 (sigminer-doc.Rmd) 
Error in png::readPNG(path, native = TRUE, info = TRUE) : 
  file is not in PNG format

The knitr also did not stop in code chunk

stopifnot(file.exists("fig/sbs_signature_overview_nat_review.png"))

I found that this issue comes from the figures.

> png::readPNG("fig/sbs_signature_overview.png")
 [ reached getOption("max.print") -- omitted 3 matrix slice(s) ]
Warning message:
In png::readPNG("fig/sbs_signature_overview.png") :
  libpng warning: iCCP: known incorrect sRGB profile
> png::readPNG("fig/sbs_signature_overview_nat_review.png")
Error in png::readPNG("fig/sbs_signature_overview_nat_review.png") : 
  file is not in PNG format

However, I can correctly knit this document before...

My session:

> devtools::session_info()
─ Session info ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 4.0.0 (2020-04-24)
 os       macOS High Sierra 10.13.6   
 system   x86_64, darwin17.0          
 ui       RStudio                     
 language (EN)                        
 collate  zh_CN.UTF-8                 
 ctype    zh_CN.UTF-8                 
 tz       Asia/Shanghai               
 date     2020-05-11                  

─ Packages ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 package      * version date       lib source        
 assertthat     0.2.1   2019-03-21 [1] CRAN (R 4.0.0)
 backports      1.1.6   2020-04-05 [1] CRAN (R 4.0.0)
 bibtex         0.4.2.2 2020-01-02 [1] CRAN (R 4.0.0)
 Biobase      * 2.48.0  2020-04-27 [1] Bioconductor  
 BiocGenerics * 0.34.0  2020-04-27 [1] Bioconductor  
 bookdown       0.18    2020-03-05 [1] CRAN (R 4.0.0)
 callr          3.4.3   2020-03-28 [1] CRAN (R 4.0.0)
 cli            2.0.2   2020-02-28 [1] CRAN (R 4.0.0)
 cluster      * 2.1.0   2019-06-19 [2] CRAN (R 4.0.0)
 codetools      0.2-16  2018-12-24 [2] CRAN (R 4.0.0)
 colorspace     1.4-1   2019-03-18 [1] CRAN (R 4.0.0)
 crayon         1.3.4   2017-09-16 [1] CRAN (R 4.0.0)
 data.table     1.12.8  2019-12-09 [1] CRAN (R 4.0.0)
 desc           1.2.0   2018-05-01 [1] CRAN (R 4.0.0)
 devtools       2.3.0   2020-04-10 [1] CRAN (R 4.0.0)
 digest         0.6.25  2020-02-23 [1] CRAN (R 4.0.0)
 doParallel     1.0.15  2019-08-02 [1] CRAN (R 4.0.0)
 dplyr          0.8.5   2020-03-07 [1] CRAN (R 4.0.0)
 ellipsis       0.3.0   2019-09-20 [1] CRAN (R 4.0.0)
 evaluate       0.14    2019-05-28 [1] CRAN (R 4.0.0)
 fansi          0.4.1   2020-01-08 [1] CRAN (R 4.0.0)
 foreach        1.5.0   2020-03-30 [1] CRAN (R 4.0.0)
 fs             1.4.1   2020-04-04 [1] CRAN (R 4.0.0)
 furrr          0.1.0   2018-05-16 [1] CRAN (R 4.0.0)
 future         1.17.0  2020-04-18 [1] CRAN (R 4.0.0)
 ggplot2        3.3.0   2020-03-05 [1] CRAN (R 4.0.0)
 globals        0.12.5  2019-12-07 [1] CRAN (R 4.0.0)
 glue           1.4.0   2020-04-03 [1] CRAN (R 4.0.0)
 gridBase       0.4-7   2014-02-24 [1] CRAN (R 4.0.0)
 gtable         0.3.0   2019-03-25 [1] CRAN (R 4.0.0)
 htmltools      0.4.0   2019-10-04 [1] CRAN (R 4.0.0)
 iterators      1.0.12  2019-07-26 [1] CRAN (R 4.0.0)
 knitr          1.28    2020-02-06 [1] CRAN (R 4.0.0)
 lifecycle      0.2.0   2020-03-06 [1] CRAN (R 4.0.0)
 listenv        0.8.0   2019-12-05 [1] CRAN (R 4.0.0)
 magrittr       1.5     2014-11-22 [1] CRAN (R 4.0.0)
 memoise        1.1.0   2017-04-21 [1] CRAN (R 4.0.0)
 munsell        0.5.0   2018-06-12 [1] CRAN (R 4.0.0)
 NMF          * 0.22.0  2020-02-12 [1] CRAN (R 4.0.0)
 pacman       * 0.5.1   2019-03-11 [1] CRAN (R 4.0.0)
 pheatmap       1.0.12  2019-01-04 [1] CRAN (R 4.0.0)
 pillar         1.4.4   2020-05-05 [1] CRAN (R 4.0.0)
 pkgbuild       1.0.8   2020-05-07 [1] CRAN (R 4.0.0)
 pkgconfig      2.0.3   2019-09-22 [1] CRAN (R 4.0.0)
 pkgload        1.0.2   2018-10-29 [1] CRAN (R 4.0.0)
 pkgmaker     * 0.31.1  2020-03-19 [1] CRAN (R 4.0.0)
 plyr           1.8.6   2020-03-03 [1] CRAN (R 4.0.0)
 png            0.1-7   2013-12-03 [1] CRAN (R 4.0.0)
 prettyunits    1.1.1   2020-01-24 [1] CRAN (R 4.0.0)
 processx       3.4.2   2020-02-09 [1] CRAN (R 4.0.0)
 ps             1.3.2   2020-02-13 [1] CRAN (R 4.0.0)
 purrr          0.3.4   2020-04-17 [1] CRAN (R 4.0.0)
 R.cache        0.14.0  2019-12-06 [1] CRAN (R 4.0.0)
 R.methodsS3    1.8.0   2020-02-14 [1] CRAN (R 4.0.0)
 R.oo           1.23.0  2019-11-03 [1] CRAN (R 4.0.0)
 R.utils        2.9.2   2019-12-08 [1] CRAN (R 4.0.0)
 R6             2.4.1   2019-11-12 [1] CRAN (R 4.0.0)
 RColorBrewer   1.1-2   2014-12-07 [1] CRAN (R 4.0.0)
 Rcpp           1.0.4.6 2020-04-09 [1] CRAN (R 4.0.0)
 registry     * 0.5-1   2019-03-05 [1] CRAN (R 4.0.0)
 remotes        2.1.1   2020-02-15 [1] CRAN (R 4.0.0)
 reshape2       1.4.4   2020-04-09 [1] CRAN (R 4.0.0)
 rlang          0.4.6   2020-05-02 [1] CRAN (R 4.0.0)
 rmarkdown      2.1     2020-01-20 [1] CRAN (R 4.0.0)
 rngtools     * 1.5     2020-01-23 [1] CRAN (R 4.0.0)
 rprojroot      1.3-2   2018-01-03 [1] CRAN (R 4.0.0)
 rstudioapi     0.11    2020-02-07 [1] CRAN (R 4.0.0)
 scales         1.1.0   2019-11-18 [1] CRAN (R 4.0.0)
 sessioninfo    1.1.1   2018-11-05 [1] CRAN (R 4.0.0)
 sigminer     * 1.0.5   2020-05-09 [1] local         
 stringi        1.4.6   2020-02-17 [1] CRAN (R 4.0.0)
 stringr        1.4.0   2019-02-10 [1] CRAN (R 4.0.0)
 styler         1.3.2   2020-02-23 [1] CRAN (R 4.0.0)
 testthat       2.3.2   2020-03-02 [1] CRAN (R 4.0.0)
 tibble         3.0.1   2020-04-20 [1] CRAN (R 4.0.0)
 tidyselect     1.0.0   2020-01-27 [1] CRAN (R 4.0.0)
 usethis        1.6.1   2020-04-29 [1] CRAN (R 4.0.0)
 vctrs          0.2.4   2020-03-10 [1] CRAN (R 4.0.0)
 withr          2.2.0   2020-04-20 [1] CRAN (R 4.0.0)
 xfun           0.13    2020-04-13 [1] CRAN (R 4.0.0)
 xtable         1.8-4   2019-04-21 [1] CRAN (R 4.0.0)
 yaml           2.2.1   2020-02-01 [1] CRAN (R 4.0.0)

[1] /Users/wsx/R_library
[2] /Library/Frameworks/R.framework/Versions/4.0/Resources/library

Solution

  • The problem is caused by the wrong file format. Although I save the error file with '.png' file extension before, it is not in png format. I still don't know why this file can be read by png package sometimes.

    I correct the format and the issue is gone.

    > error_file = magick::image_read("fig/sbs_signature_overview_nat_review.png")
    > print(error_file)
      format width height colorspace matte filesize density
    1   WEBP   685    521       sRGB FALSE    18390   72x72
    > right_png <- magick::image_convert(error_file, "png")
    > right_png
      format width height colorspace matte filesize density
    1    PNG   685    521       sRGB FALSE        0   72x72
    > magick::image_write(right_png, path = "fig/sbs_signature_overview_nat_review2.png", format = "png")
    

    I learn a little magick and debugging knitr here. Thanks for the authors.