Creating Reporting Template with Glue in R

November 8, 2017

(This article was first published on R Programming – DataScience+, and kindly contributed to R-bloggers)

Report Generation is a very important part in any Organization’s Business Intelligence and Analytics Division. The ability to create automated reports out of the given data is one of the most desirable things, that any innovative team would thrive for. And that is one area where SAS is considered to be more matured than R – not because R does not have those features – but primarily because R practitioners are not familiar with those. That’s the same feeling I came across today when I stumbled upon this package glue in R, which is a very good and competitive alternative for Reporting Template packages like whisker and brew.

The package can be installed directly from CRAN.


Let us try to put together a very minimal reporting template to output basic information about the given Dataset.

df <- mtcars
msg <- 'Dataframe Info: \n\n This dataset has {nrow(df)} rows and {ncol(df)} columns. \n There {ifelse(sum(>0,"is","are")} {sum(} Missing Value
Dataframe Info: 
This dataset has 32 rows and 11 columns. 
There are 0 Missing Values.

As in the above code, glue() is the primary function that takes a string with r expressions enclosed in curly braces {} whose resulting value would get concatenated with the given string. Creation of the templatised string is what we have done with msg. This whole exercise wouldn’t make much of a sense if it’s required for only one dataset, rather it serves its purpose when the same code is used for different datasets with no code change. Hence let us try running this on a different dataset – R’s inbuilt iris dataset. Also since we are outputting the count of missing values, let’s manually assign NA for two instances and run the code.

df <- iris
df[2,3] <- NA
df[4,2] <- NA
msg <- 'Dataframe Info: \n\n This dataset has {nrow(df)} rows and {ncol(df)} columns. \n There {ifelse(sum(>0,"is","are")} {sum(} Missing Value
Dataframe Info: 
This dataset has 150 rows and 5 columns.
There is 2 Missing Values.

That looks fine. But what if we want to report the contents of the dataframe. That’s where coupling glue's glue_data() function with magrittr's %>% operator helps.

head(mtcars) %>% glue_data("* {rownames(.)} has {cyl} cylinders and {hp} hp")
* Mazda RX4 has 6 cylinders and 110 hp
* Mazda RX4 Wag has 6 cylinders and 110 hp
* Datsun 710 has 4 cylinders and 93 hp
* Hornet 4 Drive has 6 cylinders and 110 hp
* Hornet Sportabout has 8 cylinders and 175 hp
* Valiant has 6 cylinders and 105 hp

This is just to introduce glue and its possibilities. This could potentially help in automating a lot of Reports and also to start with Exception-based Reporting. The code used in the article can be found on my github.

    Related Post

    1. Predict Employee Turnover With Python
    2. Making a Shiny dashboard using ‘highcharter’ – Analyzing Inflation Rates
    3. Time Series Analysis in R Part 2: Time Series Transformations
    4. Time Series Analysis in R Part 1: The Time Series Object
    5. Parsing Text for Emotion Terms: Analysis & Visualization Using R

    To leave a comment for the author, please follow the link and comment on their blog: R Programming – DataScience+. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

    If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

    Comments are closed.

    Search R-bloggers


    Never miss an update!
    Subscribe to R-bloggers to receive
    e-mails with the latest R posts.
    (You will not see this message again.)

    Click here to close (This popup will not appear again)