如何将 {gtsummary} 表输出为图像?

sho*_*say 4 r gtsummary

我正在尝试将 {gtsummary} 表(特别是tbl_regression()输出)输出为图像。我在用kableExtra::as_image()但结果不是很漂亮。下面是一个reprex,但我没有发布图片的声誉。

有没有办法以编程方式获取tbl_regression()输出图像?在{gtsummary} github 站点上,自述文件中有非常漂亮的表格图像,但我不确定它们是手动创建的还是使用代码创建的,因为我没有看到任何提取它们的函数。

library(gtsummary)
library(kableExtra)

m_linear <-
    lm(mpg ~ cyl, data = mtcars)

gtsummary::tbl_regression(m_linear) %>%
    gtsummary::as_kable() %>%
    kableExtra::as_image(file = "t.png")
Run Code Online (Sandbox Code Playgroud)

reprex 包(v0.3.0)于 2020 年 3 月 18 日创建

Mar*_*num 8

The images on the website were printed using the {gt} package from RStudio (which we love), and they have a function to save tables (gt::gtsave()) that works beautifully with {gtsummary} tables.

You can use the following steps to save a {gtsummary} table using {gt}:

library(gtsummary)

# make model and tbl_regression object
m_linear <- lm(mpg ~ cyl, data = mtcars)
t1 <- tbl_regression(m_linear)

# Use function from gt package to save table, after converting to 
# gt object using as_gt()
gt::gtsave(as_gt(t1), file = file.path(tempdir(), "temp.png"))
Run Code Online (Sandbox Code Playgroud)

Created on 2020-03-19 by the reprex package (v0.3.0)

Hope this works for you, good luck!

  • 这将返回“PhantomJS”未找到错误。 (2认同)
  • @Mandalorian 只需使用 `webshot::install_phantomjs()` 安装它。 (2认同)