Search code examples
rrstudiokableextra

Error using save_kable when using library kableextra to create a table in R Quarto


I would like to save a table generated with kableExtra as a PNG file in a quarto report. I'm using the example provided in the help file of save_kable

kable(mtcars[1:5, ], "html") %>%
  kable_styling("striped") %>%
  row_spec(1, color = "red") %>%
  save_kable('test.png')

And receive the following error

Error: rsession-utf8.exe: GeometryDoesNotContainImage `C:\test.png' @ warning/attribute.c/GetImageBoundingBox/534

I'm using R version 4.4.0 (2024-04-24 ucrt) in RStudio with the following libraries:

kableextra 1.4
chromote 0.3.1
webshot 0.5.5
webshot2 0.1.1

Solution

  • An alternative is to first save as an html file and then use webshot2 to convert to png:

    kable(mtcars[1:5, ], "html") %>%
      kable_styling("striped") %>%
      row_spec(1, color = "red") %>%
      save_kable('test.html')
    
    webshot2::webshot("test.html", "test.png")