Home > Software engineering >  Not able to save kable tables in R
Not able to save kable tables in R

Time:10-08

I'm trying to save a kableExtra table, but I only managed to do so by clicking on the "save" button above the image (png or jpeg), which is not ideal...this is my case

  • Ideally, I'd like to export it as a Word file (note that knitting to Word in r-markdown is NOT working)
  • I'd also like to be able to export to .png, but this option to save as pdf or png is working

If I try the same thing, I get:

packageVersion("kableExtra") 
[1] ‘1.3.4’

> library(webshot)
Warning message:
package ‘webshot’ was built under R version 4.1.3 

> dt <- mtcars[1:5, 1:6]
>  
> kableExtra::kbl(dt, caption="Table") %>%
  kableExtra::kable_classic(full_width = F, html_font = "Cambria") %>% 
  kableExtra::save_kable("my_latex_table.png") # altern
Could not load  c:\Users\laris\AppData\Local\Temp\Rtmpm0UVE0\my_latex_table524420567fce.html
Error in webshot::webshot(file_temp_html, file, ...) : 
  webshot.js returned failure value: 1

Any ideas? Ideally, I'd like to be able to export to both .doc and .png

CodePudding user response:

Update

It seems updating the {webshot} package solve this issue. After installing the {webshot} package again, Running the following code simply,

library(kableExtra)

dt <- mtcars[1:5, 1:6]

kbl(dt, caption="Table") %>%
kable_classic(full_width = F, html_font = "Cambria") %>% 
save_kable("my_latex_table.png")

indeed creates the my_latex_table.png in the working directory.

packageVersion("webshot")
#> [1] '0.5.4'

Old answer

It seems you are not alone facing this issue. There's an exactly same issue #707 regarding this erroneous behavior of {webshot} (Although the issue is currently closed with a suggested workaround).

I am just describing the suggested workaround from this issue #707 comment.

At first, save the table in a html file using save_kable which will create a html file in your working directory and then pass that html file in webshot::webshot() to get a screenshot of the table from that html file, which will create the png file in your working directory.

library(kableExtra)

dt <- mtcars[1:5, 1:6]

kbl(dt, caption="Table") %>%
kable_classic(full_width = F, html_font = "Cambria") %>% 
save_kable("my_latex_table.png")


webshot::webshot("my_latex_table.html", file = "my_latex_table.png",
                 selector = ".lightable-classic", expand = 20)
  • Related