如何将 R 对象的信息导出为 png?

问题描述

我想汇总有关我的数据的信息并将其导出为 png 文件。我已经尝试过 panderflextable 等软件包,但目前无法实现我的目标。也许有一种方法可以使用 ggplot2 完成此操作,但我不知道这种情况。

示例

假设我们有 mtcars 数据,我们想提取一些关于它的信息:

  • 数据中的行数
  • mpg 的平均值
  • cyl 中可用的因子水平
  • 预测 mpg ~ cyl 的回归摘要

为此,我将计算上述每一项并将它们分配给对象。最后,我会将所有信息捆绑在一个列表对象中。

library(dplyr)
library(tibble)
library(broom)

number_of_rows <- nrow(mtcars)
mpg_mean       <- mean(mtcars$mpg)
cyl_levels     <- mtcars %>% select(cyl) %>% unique() %>% remove_rownames()
model_summary  <- lm(mpg ~ cyl,mtcars) %>% broom::tidy()

my_data_summary <- lst(number_of_rows,mpg_mean,cyl_levels,model_summary)


> my_data_summary
## $number_of_rows
## [1] 32

## $mpg_mean
## [1] 20.09062

## $cyl_levels
##   cyl
## 1   6
## 2   4
## 3   8

## $model_summary
## # A tibble: 2 x 5
##   term        estimate std.error statistic  p.value
##   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
## 1 (Intercept)    37.9      2.07      18.3  8.37e-18
## 2 cyl            -2.88     0.322     -8.92 6.11e-10

我的问题:如何将 my_data_summary 导出为 png


一些不成功的尝试。

请注意,我并不是特别想使用任何特定的包。

1) 我试过pander

library(pander)

pander(my_data_summary)



  * **number_of_rows**: _32_
  * **mpg_mean**: _20.09_
  * **cyl_levels**:

    -----
     cyl
    -----
      6

      4

      8
    -----

  * **model_summary**:

    ------------------------------------------------------------
        term       estimate   std.error   statistic    p.value
    ------------- ---------- ----------- ----------- -----------
     (Intercept)    37.88       2.074       18.27     8.369e-18

         cyl        -2.876     0.3224       -8.92     6.113e-10
    ------------------------------------------------------------


<!-- end of list -->

这确实让我走得很远,但还不够。我怎样才能从这样的降价文本输出渲染 png?


2)我也试过flextable

library(flextable)

flextable(my_data_summary)

flextable(.) 中的错误:is.data.frame(data) 不是 TRUE

好的,所以 flextable() 只接受 data.frame 类。

因此,我可以这样做:

flextable(my_data_summary$model_summary)

然后给出这个很好的输出,可以用 png 保存到 flextable::save_as_image()

model_summary

然而,flextable(my_data_summary$model_summary) 的问题在于我想将 my_data_summary 的全部内容呈现并导出到单个 png,类似于 pander() 接受完整列出对象。

还要注意我通过 Rscript 执行此代码,因此我需要一个非交互式/基于 GUI 的解决方案。


期望输出

一个 png 文件,表示 my_data_summary 对象,看起来像:

enter image description here

对此有什么想法吗?


编辑


基于@Waldi 在下面的回答,我想强调的是,我正在寻找一种可以在单个代码运行中运行的解决方案,而无需创建中间临时文件。换句话说,我试图想出一个只有一个输出代码一个 png 内容my_data_summary 文件

解决方法

您可以使用 webshot 包。
由于 webshot 依赖于 phantom.js,因此第一步是运行:

webshot::install_phantomjs()

然后创建 test.Rmd 文件:

---
title: test
output: html_document
---

`r knitr::opts_chunk$set(echo = FALSE,warning = FALSE,message = FALSE,cache = F)`

```{r,results='asis'}
library(dplyr)
library(tibble)
library(broom)

number_of_rows <- nrow(mtcars)
mpg_mean       <- mean(mtcars$mpg)
cyl_levels     <- mtcars %>% select(cyl) %>% unique() %>% remove_rownames()
model_summary  <- lm(mpg ~ cyl,mtcars) %>% broom::tidy()

my_data_summary <- lst(number_of_rows,mpg_mean,cyl_levels,model_summary)

library(pander)

pander(my_data_summary)
```

您现在可以输出png

webshot::rmdshot('test.Rmd','test.png')  

enter image description here

,

您可以使用具有连接多个html表格功能的htmlTable包

library(dplyr)
library(tibble)
library(broom)
number_of_rows <- nrow(mtcars)
mpg_mean       <- mean(mtcars$mpg)
cyl_levels     <- mtcars %>% select(cyl) %>% unique() %>% remove_rownames()
model_summary  <- lm(mpg ~ cyl,model_summary)


#Using htmlTable
library(htmlTable)

#converting each element of the list into invidual html table and concatenating 
concatHtmlTables(list(htmlTable(my_data_summary[[1]]),htmlTable(txtRound(my_data_summary[[2]],4)),htmlTable(my_data_summary[[3]]),htmlTable(txtRound(my_data_summary[[4]],4))),headers = names(my_data_summary))

      

enter image description here

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...