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.
Example
Let's say that we have the mtcars data, and we want to extract some information about it:
- 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 question: how can I export my_data_summary as a png?
Some unsuccessful attempts.
To note, I'm not particularly motivated to use any specific package.
1) I've tried 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 -->
This indeed gets me pretty far, but insufficient. How can I get from such markdown textual output to a rendered png?
2) I've also tried flextable.
library(flextable)
flextable(my_data_summary)
Error in flextable(.) : is.data.frame(data) is not TRUE
OK, so flextable() accepts only data.frame class.
I therefore could have done something like:
flextable(my_data_summary$model_summary)
Which then gives this nice output that can be saved to png with flextable::save_as_image():

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.
Also important to note that I execute this code via Rscript, so I need a solution that is not interactive/GUI-based.
Desired Output
A png file that represents my_data_summary object and looks something like:
Any ideas on this?
EDIT
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.


You can use htmlTable package which has the function to concatenate multiple html tables