Detailed introduction of “myprettyreport” R package
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Package introduction: Package myprettyreport helps to export ggplot2 graphs into a good-looking PDF file in a clear and easy way with a wide range of flexibility. It has a modular structure so the report elements can be combined in many ways.
Installation: This package currently only available on Github so the proper way to install it:
Quick overview:
Functions of the package:
- start_report() is the first mandatory function to generate the report.
- add_cover_page() function generates the cover page of the report.
- add_new_page() function adds a single report page to the report.
- add_multiple_page() function adds multiple report pages to the report.
- end_report() function generates the final output and closes the process.
Quick example:
Result:
page1 | page2 |
---|---|
Basics
A minimal example which generates a PDF file but it contains just a blank page:
As we can see each elements can be added to each other with the magrittr::%>% (pipe) function.
Insert a new report page to a PDF file which creates a skeleton ggplot2 object as default.
Now let’s create our first very own ggplot2 object and export it. The report syntax is almost the same except we have to add our ggplot2 object to a report as a plot parameter value.
The result will be equivalent so far if we just use the ggplot2 built in export function:
output_ggsave output_myprettyreport
Although the result is the same it is easier to modify the report layout a little bit thanks to the package myprettyreport. We could create a footer and a header section like this:
Also it could be possible to add a logo to a top left corner. All we need is to load a logo file into R and add it as a logo parameter. Optionally the size of the logo could be specified (valid values are between 0 and 1).
Currently 2 themes have been implemented yet. Now we are changing the theme from the default to a theme called flashy. In this case we have to modify the default values of the header and footer colors also because the default color of the text is white.
We may notice that if we use this theme, the position of the logo will also change (now it is on the bottom left corner). So the size parameter of the logo will probably need to be reconfigured also.
The next step could be adding one more page to our report. In this case the preferred way is to use the add_multiple_page() function because it results more simplified syntax:
Important to note that the add_multiple_page() function was executed (not the add_new_page()) and it has a page_number parameter instead of footer_text! So in this case we have to multiply the footer text value.
Now let’s add a cover page to our report. We have to insert the add_cover_page() function after the start_report() function.
It is also possible to change the theme of the cover page if we want the same “flashy” theme everywhere:
Now let’s add multiple plots to a single page:
In this case we have to specify the layout of the plot area with the grid::grid.layout() function. Also we have to determine the position of every single plot (plot_hpos and plot_vpos). For example if we create a 2×2 plot area plot_hpos = c(1, 2) means that the first plot in the plot_list will be in the left side of the page, and second one will be on the other side.
Optionally of course we could also use other libraries to reach the same goal. My personal recommendation is the patchwork package. As I’ve mentioned already the result will be the same:
report_output report_output_patchwork
Finally create a report which has multiple pages and also multiple plots in every page:
So if we compare this syntax to the previous one we could see that the only difference is that we need to create sublists inside our list and every plot which has been defined inside a sublist will appear in the same report page.
Advanced features
Export multiple ggplot2 objects into multiple files
Customize the report with extra grid parameters
The package can allows us to pass extra grid parameters to our report to customize the final output. It can be useful if we need e.g. an additional text field. First of all we have to know the default layout structure of the report. If you haven’t heard about the basics of grid layouts it is recommended to go deeper into that topic at first. I share the structure of the layouts which was produced by the grid::grid.show.layout() function:
cover_page_theme_basic report_page_theme_basic
cover_page_theme_flashy report_page_theme_flashy
So e.g. if we want to insert an extra text field in the report page header when the theme is the basic, the syntax is the following:
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.