Reputation: 4201
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.
Let's say that we have the mtcars
data, and we want to extract some information about it:
mpg
cyl
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.
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.
Upvotes: 1
Views: 1154
Reputation: 41260
You could use the webshot
package.
As webshot
relies on phantom.js
the first step is to run:
webshot::install_phantomjs()
Then create test.Rmd
file :
---
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)
```
You are now ready to output png
:
webshot::rmdshot('test.Rmd','test.png')
Upvotes: 3
Reputation: 2949
You can use htmlTable package which has the function to concatenate multiple html tables
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)
#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))
Upvotes: 0