<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/creating-reporting-template-with-glue-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://datascienceplus.com/creating-reporting-template-with-glue-in-r/"> R Programming – DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>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 <code><a rel="nofollow" href="https://cran.r-project.org/web/packages/glue/index.html" target="_blank">glue</a></code> in R, which is a very good and competitive alternative for Reporting Template packages like <code>whisker</code> and <code>brew</code>.</p>
<p>The package can be installed directly from CRAN.</p>
<pre>
install.packages('glue')
</pre>
<p>Let us try to put together a very minimal reporting template to output basic information about the given Dataset. </p>
<pre>
library(glue)
df &lt;- mtcars
msg &lt;- 'Dataframe Info: \n\n This dataset has {nrow(df)} rows and {ncol(df)} columns. \n There {ifelse(sum(is.na(df))>0,&quot;is&quot;,&quot;are&quot;)} {sum(is.na(df))} Missing Value
glue(msg)
<em>Dataframe Info: This dataset has 32 rows and 11 columns. There are 0 Missing Values.
</em>
</pre>
<p>As in the above code, <code>glue()</code> is the primary function that takes a <code>string</code> with r expressions enclosed in curly braces <code>{}</code> whose resulting value would get concatenated with the given string. Creation of the templatised string is what we have done with <code>msg</code>. 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 <code>iris </code>dataset. Also since we are outputting the count of missing values, let’s manually assign <code>NA</code> for two instances and run the code.</p>
<pre>
df &lt;- iris
df[2,3] &lt;- NA
df[4,2] &lt;- NA
msg &lt;- 'Dataframe Info: \n\n This dataset has {nrow(df)} rows and {ncol(df)} columns. \n There {ifelse(sum(is.na(df))>0,&quot;is&quot;,&quot;are&quot;)} {sum(is.na(df))} Missing Value
glue(msg)
<em>Dataframe Info: This dataset has 150 rows and 5 columns.
There is 2 Missing Values.
</em>
</pre>
<p>That looks fine. But what if we want to report the contents of the dataframe. That’s where coupling <code>glue's glue_data()</code> function with <code>magrittr's %>% </code> operator helps.</p>
<pre>
library(magrittr)
head(mtcars) %>% glue_data(&quot;* {rownames(.)} has {cyl} cylinders and {hp} hp&quot;)
<em>* 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
</em>
</pre>
<p>This is just to introduce <code>glue</code> 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 <a rel="nofollow" href="https://github.com/amrrs/datascienceplus_codes/blob/master/glue_reporting_template.R" target="_blank">my github</a>.</p>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p></p></strong>
<ol>
<li><a rel="nofollow" href="https://datascienceplus.com/predict-employee-turnover-with-python/" title="Permanent Link toPredict Employee Turnover With Python" target="_blank">Predict Employee Turnover With Python</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/making-a-shiny-dashboard-using-highcharter-analyzing-inflation-rates/" title="Permanent Link toMaking a Shiny dashboard using ‘highcharter’ – Analyzing Inflation Rates" target="_blank">Making a Shiny dashboard using ‘highcharter’ – Analyzing Inflation Rates</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/time-series-analysis-in-r-part-2-time-series-transformations/" title="Permanent Link toTime Series Analysis in R Part 2: Time Series Transformations" target="_blank">Time Series Analysis in R Part 2: Time Series Transformations</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/time-series-analysis-in-r-part-1-the-time-series-object/" title="Permanent Link toTime Series Analysis in R Part 1: The Time Series Object" target="_blank">Time Series Analysis in R Part 1: The Time Series Object</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/parsing-text-for-emotion-terms-analysis-visualization-using-r/" title="Permanent Link toParsing Text for Emotion Terms: Analysis &amp; Visualization Using R" target="_blank">Parsing Text for Emotion Terms: Analysis &amp; Visualization Using R</a></li>
</ol>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://datascienceplus.com/creating-reporting-template-with-glue-in-r/"> R Programming – DataScience+</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/HMGBp9FzRVI" height="1"></a></div>