【问题标题】:How can I save a huxtable as png file in R如何在 R 中将 huxtable 保存为 png 文件
【发布时间】:2022-06-10 20:22:09
【问题描述】:

huxtable 包带有quick_* 函数来保存输出,例如html(通过quick_html)、pdf(通过quick_pdf)等。

但是,似乎没有直接保存到图像(例如 png)的选项。

【问题讨论】:

    标签: r huxtable webshot


    【解决方案1】:

    我前段时间有这个问题并自己解决了,但认为它可能对遇到类似挑战的其他人有所帮助。

    幸运的是,webshot 包允许对使用quick_html 保存的huxtable 输出进行截图。

    解决办法如下:

    library(magrittr)
    library(huxtable)
    set.seed(1337)
    
    data <- matrix(rnorm(25), 5, 5)
    my_hux <- as_hux(data) %>%
      set_outer_borders(0.4) %>%
      map_background_color(by_rows("grey95", "white")) %>%
      map_text_color(by_quantiles(c(0.1, 0.9), c("red", "black", "green3")))
    
    quick_html(my_hux, file = "ahuxtable.html", open = FALSE)
    # higher zoom increases resolution and image size
    # you may need to run "webshot::install_phantomjs()" for this to work
    webshot::webshot(url = "ahuxtable.html", file = "ahuxtable.png",
                     zoom = 5, selector = "table")
    

    flextable 似乎也有类似的问题,here 已解决。

    【讨论】: