简体   繁体   English

如何在 R 中将 huxtable 保存为 png 文件

[英]How can I save a huxtable as png file in R

The huxtable package comes with quick_* functions to save outputs such as html (via quick_html ), pdf (via quick_pdf ) etc. huxtable包带有quick_*函数来保存输出,例如 html(通过quick_html )、pdf(通过quick_pdf )等。

However, there seems to be no option to directly save to an imgage (eg png).但是,似乎没有直接保存到图像(例如 png)的选项。

Patrick's answer is nice.帕特里克的回答很好。 Alternatively, you can convert the pdf to png with GhostScript :或者,您可以使用GhostScript将 pdf 转换为 png:

gs -sDevice=png16m -sOutputFile="hux.png" hux.pdf

I had this question a while ago and solved it on my own, but thought it may be helpful to others who run into similar challenges.不久前我有这个问题并自己解决了,但认为它可能对遇到类似挑战的其他人有所帮助。

Fortunately, the package webshot allows taking screenshots of huxtable outputs saved with quick_html .幸运的是, webshot包允许截取使用quick_html保存的huxtable输出的屏幕截图。

Here's the solution:这是解决方案:

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")

There seems to be a similar question with flextable , which was solved here .似乎有一个与flextable类似的问题,在这里得到了解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM