<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/create-and-update-powerpoint-reports-using-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.displayr.com/create-and-update-powerpoint-reports-using-r/?utm_medium=Feed&utm_source=Syndication"> R – Displayr</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In my sordid past, I was a data science consultant. One thing about data science that they don’t teach you at school is that senior managers in most large companies require reports to be in PowerPoint. Yet, I like to do my more complex data science in R – PowerPoint and R are not natural allies. As a result, creating an updating PowerPoint reports using R can be painful.</p>
<p>In this post, I discuss how to make R and PowerPoint work efficiently together. The underlying assumption is that R is your computational engine and that you are trying to get outputs into PowerPoint. I compare and contrast three tools for creating and updating PowerPoint reports using R: free <span>ReporteRs</span> package with two commercial products, <a rel="nofollow" href="http://www.displayr.com/" target="_blank">Displayr </a>and <a rel="nofollow" href="http://www.q-researchsoftware.com/" target="_blank">Q</a>.</p>
<hr>
<h2><span>Option 1: ReporteRs</span></h2>
<p>The first approach to getting R and PowerPoint to work together is to use David Gohel’s <span>ReporteRs</span>. To my mind, this is the most “pure” of the approaches from an R perspective. If you are an experienced R user, this approach works in pretty much the way that you will expect it to work.</p>
<p>The code below creates 250 crosstabs, conducts significance tests, and, if the <em>p</em>-value is less than 0.05, presents a slide containing each. And, yes, I know this is <em>p-</em>hacking, but this post is about how to use PowerPoint and R, not how to do statistics…</p>
<pre> 
library(devtools)
devtools::install_github('davidgohel/ReporteRsjars')
devtools::install_github('davidgohel/ReporteRs')
install.packages(c('ReporteRs', 'haven', 'vcd', 'ggplot2', 'reshape2'))
library(ReporteRs)
library(haven)
library(vcd)
library(ggplot2)
library(reshape2)
dat = read_spss(&quot;http://wiki.q-researchsoftware.com/images/9/94/GSSforDIYsegmentation.sav&quot;)
filename = &quot;c://delete//Significant crosstabs.pptx&quot; # the document to produce
document = pptx(title = &quot;My significant crosstabs!&quot;)
alpha = 0.05 # The level at which the statistical testing is to be done.
dependent.variable.names = c(&quot;wrkstat&quot;, &quot;marital&quot;, &quot;sibs&quot;, &quot;age&quot;, &quot;educ&quot;)
all.names = names(dat)[6:55] # The first 50 variables int the file.
counter = 0
for (nm in all.names)
    for (dp in dependent.variable.names)
    {
        if (nm != dp)
        {
            v1 = dat[[nm]]
            if (is.labelled(v1))
                v1 = as_factor(v1)
            v2 = dat[[dp]]
            l1 = attr(v1, &quot;label&quot;)
            l2 = attr(v2, &quot;label&quot;)
            if (is.labelled(v2))
                v2 = as_factor(v2)
            if (length(unique(v1)) &lt;= 10 &lt;= 10) # Only performing tests if 10 or fewer rows and columns. { 
                x = xtabs(~v1 + v2) 
                x = x[rowSums(x) > 0, colSums(x) > 0]
                ch = chisq.test(x)
                p = ch$p.value
                if (!is.na(p) &amp;&amp; p &lt;= alpha)
                {
                    counter = counter + 1
                    # Creating the outputs.
                    crosstab = prop.table(x, 2) * 100
                    melted = melt(crosstab)
                    melted$position = 100 - as.numeric(apply(crosstab, 2, cumsum) - 0.5 * crosstab)
                    p = ggplot(melted, aes(x = v2, y = value,fill = v1)) + geom_bar(stat='identity')
                    p = p + geom_text(data = melted, aes(x = v2, y = position, label = paste0(round(value, 0),&quot;%&quot;)), size=4)
                    p = p + labs(x = l2, y = l1)
                    colnames(crosstab) = paste0(colnames(crosstab), &quot;%&quot;)
                    #bar = ggplot() + geom_bar(aes(y = v1, x = v2), data = data.frame(v1, v2), stat=&quot;identity&quot;)
                    # Writing them to the PowerPoint document.
                    document = addSlide(document, slide.layout = &quot;Title and Content&quot; )
                    document = addTitle(document, paste0(&quot;Standardized residuals and chart: &quot;, l1, &quot; by &quot;, l2))
                    document = addPlot(doc = document, fun = print, x = p, offx = 3, offy = 1, width = 6, height = 5 )
                    document = addFlexTable(doc = document, FlexTable(round(ch$stdres, 1), add.rownames = TRUE),offx = 8, offy = 2, width = 4.5, height = 3 )
                }
            }
            
       }
    }
writeDoc(document, file = filename )        
cat(paste0(counter, &quot; tables and charges exported to &quot;, filename, &quot;.&quot;))
</pre>
<p>Below we see one of the admittedly ugly slides created using this code. With more time and expertise, I am sure I could have done something prettier. A cool aspect of the <span>ReporteRs</span> package is that you can then edit the file in PowerPoint. You can then get R to update any charts and other outputs originally created in R.</p>
<p> </p>
<p><img sizes="(max-width: 780px) 100vw, 780px" src="https://i0.wp.com/www.displayr.com/wp-content/uploads/2017/07/RPowerPoint.png?w=450&ssl=1" alt="Chart created in PowerPoint using R" class="wp-image-2438" data-recalc-dims="1"></p>
<p> </p>
<hr>
<h2><span>Option 2: Displayr</span></h2>
<p>A completely different approach is to author the report in <a rel="nofollow" href="http://www.displayr.com/" target="_blank">Displayr</a>, and then export the resulting report from Displayr to PowerPoint.</p>
<p>This has advantages and disadvantages relative to using ReporteRs. First, I will start with the big disadvantage, in the hope of persuading you of my objectivity (disclaimer: I have no objectivity, I work at Displayr).</p>
<p>Each page of a Displayr report is created interactively, using a mouse and clicking and dragging things. In my earlier example using ReporteRs, I only created pages where there was a statistically significant association. Currently, there is no way of doing such a thing in Displayr.</p>
<p>The flipside of using the graphical user interface like Displayr is that it is a lot easier to <a rel="nofollow" href="https://www.displayr.com/layered-visualization/" target="_blank">create attractive visualizations</a>. As a result, the user has much greater control over the look and feel of the report. For example, the screenshot below shows a PowerPoint document created by Displayr. All but one of the charts has been created using R, and the first two are based on a moderately complicated statistical model (latent class rank-ordered logit model).</p>
<p>You can access the document used to create the <a rel="nofollow" href="https://app.displayr.com/Try/Presidential%20Max-Diff" target="_blank">PowerPoint report with R here</a> (just sign in to Displayr first) – you can poke around and see how it all works.</p>
<p>A benefit of authoring a report using Displayr is that the user can access the report online, interact with it (e.g., filter the data), and then export precisely what they want. You can see this document as it is viewed by a user of the <a rel="nofollow" href="https://app.displayr.com/Dashboard?id=c1506180-fe64-4941-8d24-9ec4a54439af#page=60e0ae01-0f29-463f-b66f-967b05a407bf" target="_blank">online report</a> here.</p>
<p> </p>
<p><img sizes="(max-width: 780px) 100vw, 780px" src="https://i1.wp.com/www.displayr.com/wp-content/uploads/2017/08/DisplayrPowerPoint.png?w=450&ssl=1" alt="Demographics dashboard example from Displayr" class="wp-image-2860" data-recalc-dims="1"></p>
<p> </p>
<hr>
<h2><span>Option 3: Q</span></h2>
<p><img sizes="(max-width: 450px) 100vw, 450px" src="https://i0.wp.com/www.displayr.com/wp-content/uploads/2017/08/ReportTree2.png?resize=450%2C176&ssl=1" alt="" class="wp-image-2873" data-recalc-dims="1"></p>
<p>A third approach for authoring and updating PowerPoint reports using R is to use <a rel="nofollow" href="https://www.q-researchsoftware.com/" target="_blank">Q</a>, which is a Windows program designed for survey reporting (same disclaimer as with Displayr). It works by exporting and updating results to a PowerPoint document. Q has two different mechanisms for exporting R analyses to PowerPoint. First, you can export R outputs, including HTMLwidgets, created in Q directly to PowerPoint as images. Second, you can create tables using R and then have these exported as native PowerPoint objects, such as Excel charts and PowerPoint tables.</p>
<p>Q has two different mechanisms for exporting R analyses to PowerPoint. First, you can export R outputs, including HTMLwidgets, created in Q directly to PowerPoint as images. Second, you can create tables using R and then have these exported as native PowerPoint objects, such as Excel charts and PowerPoint tables.</p>
<p>In Q, a <em>Report </em>contains a series of analyses. Analyses can either be created using R, or, using Q’s own internal calculation engine, which is designed for producing tables from survey data.</p>
<p>The map above (in the Displayr report) is an <em>HTMLwidget </em>created using the <span>plotly</span> R package. It draws data from a table called <strong>Region</strong>, which would also be shown in the report. (The same R code in the Displayr example can be used in an R object within Q). So when exported into PowerPoint, it creates a page, using the PowerPoint template, where the title is <strong>Responses by region </strong>and the map appears in the middle of the page.</p>
<p>The screenshot below is showing another R chart created in PowerPoint. The data has been <a rel="nofollow" href="https://www.displayr.com/extracting-google-trends-data-in-r/" target="_blank">extracted from Google Trends using the <span>gtrendsR</span> R package</a>. However, the chart itself is a standard Excel chart, attached to a spreadsheet containing the data. These slides can then be customized using all the normal PowerPoint tools and can be <a rel="nofollow" href="https://www.displayr.com/automatically-update-powerpoint/" target="_blank">automatically updated when the data is revised</a>.</p>
<h2></h2>
<p><img sizes="(max-width: 780px) 100vw, 780px" src="https://i2.wp.com/www.displayr.com/wp-content/uploads/2017/08/PowerPoint-chart-1024x481.png?w=450&ssl=1" alt="" class="wp-image-2874" data-recalc-dims="1"></p>
<p> </p>
<hr>
<h2><span>Explore the Displayr example</span></h2>
<p>You can access the Displayr document used to create and update the <a rel="nofollow" href="https://app.displayr.com/Try/Presidential%20Max-Diff" target="_blank">PowerPoint report with R here</a> (just sign in to Displayr first). Here, you can poke around and see how it all works or create your own document.</p>
<h2></h2>
	<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://www.displayr.com/create-and-update-powerpoint-reports-using-r/?utm_medium=Feed&utm_source=Syndication"> R – Displayr</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/KVt4kdA2TDw" height="1"></a></div>