如何将R对象中的信息导出为png? [英] How to export information from R objects as png?
问题描述
我想总结有关我的数据的信息,并将其导出为 png
文件.我已经尝试过诸如 pander
和 flextable
之类的软件包,但是目前无法实现我的目标.也许有一种方法可以使用 ggplot2
完成此操作,但我并不知道.
I want to summarize information about my data and export it as a png
file. I've experimented with packages such as pander
and flextable
, but currently can't achieve my goal. Maybe there's a way to get this done using ggplot2
, but I'm unaware of such.
假设我们有 mtcars
数据,并且我们想要提取一些有关它的信息:
Let's say that we have the mtcars
data, and we want to extract some information about it:
- 数据中的行数
-
mpg
的平均值 -
cyl
中可用的因子水平 - 用于预测
mpg〜cyl
的回归摘要
- Number of rows in the data
- Average of
mpg
- The factor levels available in
cyl
- Regression summary for predicting
mpg ~ cyl
为此,我将计算以上各项并将它们分配给对象.最后,我将所有信息捆绑在一个列表对象中.
To this end, I'll compute each of the above and assign them to objects. Finally, I'll bundle all the info in a list object.
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
?
一些尝试失败.
请注意,我并没有特别动机去使用任何特定的程序包.
To note, I'm not particularly motivated to use any specific package.
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 -->
这确实使我走得很远,但是还不够.如何从这样的markdown文本输出转换为渲染的 png?
This indeed gets me pretty far, but insufficient. How can I get from such markdown textual output to a rendered png?
2)我也尝试过 flextable
.
library(flextable)
flextable(my_data_summary)
flextable(.)中的错误:is.data.frame(data)不为真
Error in flextable(.) : is.data.frame(data) is not TRUE
好的,所以 flextable()
仅接受 data.frame
类.
因此,我可以做类似的事情:
I therefore could have done something like:
flextable(my_data_summary$model_summary)
然后给出哪个很好的输出,可以使用 flextable :: save_as_image()
保存到 png
:
Which then gives this nice output that can be saved to png
with flextable::save_as_image()
:
但是, flextable(my_data_summary $ model_summary)
的问题是我想获取 my_data_summary
的全部内容,并将其导出到该单个 png
,类似于 pander()
完全接受列表对象的方式.
However, the problem with flextable(my_data_summary$model_summary)
is that I want to get the entire contents of my_data_summary
rendered and exported to that single png
, similar to how pander()
accepts the list object in its entirety.
还要注意,我需要通过 Rscript
执行此代码,因此我需要一个非交互式/基于GUI的解决方案.
Also important to note that I execute this code via Rscript
, so I need a solution that is not interactive/GUI-based.
代表 my_data_summary
对象的 png
文件,其外观类似于:
A png
file that represents my_data_summary
object and looks something like:
对此有何想法?
编辑
基于下面的@Waldi的回答,我想强调一点,我正在寻找一种可以在单个代码运行中运行而不创建中间临时文件的解决方案.换句话说,我试图提供一个具有单个输出的代码:一个包含 my_data_summary
内容的 png
文件.
Based on @Waldi's answer below, I'd like to emphasize that I'm looking for a solution that could be run in a single code run, without creating intermediate temporary files. In other words, I'm trying to come up with a code that has a single output: a png
file of my_data_summary
contents.
推荐答案
您可以使用 webshot
包.
由于 webshot
依赖于 phantom.js
,第一步是运行:
You could use the webshot
package.
As webshot
relies on phantom.js
the first step is to run:
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')
这篇关于如何将R对象中的信息导出为png?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!