<?xml version="1.0" encoding="UTF-8" ?>
<!-- RSS generated by PHPBoost on Sun, 24 May 2026 15:33:03 +0200 -->

<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Easy Guides]]></title>
		<atom:link href="https://www.sthda.com/english/syndication/rss/wiki/39" rel="self" type="application/rss+xml"/>
		<link>https://www.sthda.com</link>
		<description><![CDATA[Last articles of the category: Exporting Data From R]]></description>
		<copyright>(C) 2005-2026 PHPBoost</copyright>
		<language>en</language>
		<generator>PHPBoost</generator>
		
		
		<item>
			<title><![CDATA[Exporting Data From R]]></title>
			<link>https://www.sthda.com/english/wiki/exporting-data-from-r</link>
			<guid>https://www.sthda.com/english/wiki/exporting-data-from-r</guid>
			<description><![CDATA[<!-- START HTML -->

            
  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">


<p><br/></p>
<style>#rdoc .course_material a{font-size:1.5em;} #rdoc .readmore a{font-size:1em;}</style>
<p>In the previous chapters we described the <a href="https://www.sthda.com/english/english/wiki/r-basics-quick-and-easy">essentials of R programming</a> as well as how to <a href="https://www.sthda.com/english/english/wiki/importing-data-into-r">import data into R</a>. Here, you’ll learn how to <strong>export data</strong> from <strong>R</strong> to <strong>txt</strong>, <strong>csv</strong>, <strong>Excel</strong> (xls, xlsx) and R data file formats. Additionally, we’ll describe how to create and format <strong>Word</strong> and <strong>PowerPoint</strong> documents from R.</p>
<br/>
<div class="block">
<ol style="list-style-type: decimal">
<li><strong>Export Data From R to txt|csv|Excel files</strong>
<ul>
<li><a href="https://www.sthda.com/english/english/wiki/writing-data-from-r-to-txt-csv-files-r-base-functions">Writing data from R to a txt|csv file: R base functions</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/fast-writing-of-data-from-r-to-txt-csv-files-readr-package">Fast Writing of Data From R to txt|csv Files: readr package</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/writing-data-from-r-to-excel-files-xls-xlsx">Writing data from R to Excel files (xls|xlsx)</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/saving-data-into-r-data-format-rds-and-rdata">Saving data into R data format: RDATA and RDS</a> 
</li>
</ul></li>
<li><strong>Create and format word and powerpoint documents using R and ReporteRs package</strong>:
<ul>
<li><a href="https://www.sthda.com/english/english/wiki/create-and-format-word-documents-using-r-software-and-reporters-package">Create and format Word documents</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/create-a-word-document-from-a-template-file-using-r-software-and-reporters-package">Create a Word document from a template file</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/add-a-table-into-a-word-document-using-r-software-and-reporters-package">Add a table into a Word document</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/create-and-format-powerpoint-documents-from-r-software">Create and format PowerPoint documents</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/create-an-editable-graph-from-r-software">Create an editable graph from R software to PowerPoint</a></li>
</ul></li>
</ol>
</div>
<p><br/></p>
<img src="https://www.sthda.com/english/sthda/RDoc/images/exporting-data-from-r.png" alt="Exporting data from R" /> <br/>
<hr/>
<p><br/></p>
<br/>
<div class="course_material">
<ol style="list-style-type: decimal">
<li><a href="https://www.sthda.com/english/english/wiki/writing-data-from-r-to-txt-csv-files-r-base-functions"><i class="fa fa-folder-open"></i> <strong>Writing data from R to a txt|csv file: R base functions</strong></a> <br/></li>
</ol>
<ul>
<li>R base functions for writing data: <strong>write.table</strong>(), <strong>write.csv</strong>(), <strong>write.csv2</strong>()</li>
<li>Writing data to a file</li>
</ul>
<p><br/></p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/export-txt-csv-from-r-base-functions.png" alt="Writing Data From R to txt|csv Files: R Base Functions" /> <br/></p>
<pre class="r"><code># Loading mtcars data
data("mtcars")

# Write data to txt file: tab separated values
# sep = "\t"
write.table(mtcars, file = "mtcars.txt", sep = "\t",
            row.names = TRUE, col.names = NA)

# Write data to csv files:  
# decimal point = "." and value separators = comma (",")
write.csv(mtcars, file = "mtcars.csv")

# Write data to csv files: 
# decimal point = comma (",") and value separators = semicolon (";")
write.csv2(mtcars, file = "mtcars.csv")</code></pre>
<p><span class="success readmore">Read more: <a href="https://www.sthda.com/english/english/wiki/writing-data-from-r-to-txt-csv-files-r-base-functions"><i class="fa fa-play"></i> Writing data from R to a txt|csv file: R base functions</a></span> 
</p>
<ol start="2" style="list-style-type: decimal">
<li><a href="https://www.sthda.com/english/english/wiki/fast-writing-of-data-from-r-to-txt-csv-files-readr-package"><i class="fa fa-folder-open"></i> <strong>Fast writing of Data From R to txt|csv Files: readr package</strong></a> <br/></li>
</ol>
<ul>
<li>Installing and loading readr: <strong>install.packages</strong>(“readr”)</li>
<li>readr functions for writing data: <strong>write_tsv</strong>(), <strong>write_csv</strong>()</li>
<li>Writing data to a file</li>
</ul>
<p><br/></p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/export-txt-csv-from-r-readr-package.png" alt="Fast Writing of Data From R to txt|csv Files: readr package" /> <br/></p>
<pre class="r"><code># Loading mtcars data
data("mtcars")

library("readr")
# Writing mtcars data to a tsv file
write_tsv(mtcars, path = "mtcars.txt")

# Writing mtcars data to a csv file
write_csv(mtcars, path = "mtcars.csv")</code></pre>
<p><span class="success readmore">Read more: <a href="https://www.sthda.com/english/english/wiki/fast-writing-of-data-from-r-to-txt-csv-files-readr-package"><i class="fa fa-play"></i> Fast writing of Data From R to txt|csv Files: readr package</a></span> 
</p>
<ol start="3" style="list-style-type: decimal">
<li><a href="https://www.sthda.com/english/english/wiki/writing-data-from-r-to-excel-files-xls-xlsx"><i class="fa fa-folder-open"></i> <strong>Writing data from R to Excel files (xls|xlsx)</strong></a> <br/></li>
</ol>
<ul>
<li>Installing xlsx package: <strong>install.packages</strong>(“xlsx”)</li>
<li>Using xlsx package: <strong>write.xlsx</strong>()</li>
</ul>
<p><br/></p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/export-excel-files.png" alt="Writing Data From R to Excel Files (xls|xlsx)" /> <br/></p>
<pre class="r"><code>library("xlsx")

# Write the first data set in a new workbook
write.xlsx(USArrests, file = "myworkbook.xlsx",
      sheetName = "USA-ARRESTS", append = FALSE)

# Add a second data set in a new worksheet
write.xlsx(mtcars, file = "myworkbook.xlsx", 
           sheetName="MTCARS", append=TRUE)</code></pre>
<p><span class="success readmore">Read more: <a href="https://www.sthda.com/english/english/wiki/writing-data-from-r-to-excel-files-xls-xlsx"><i class="fa fa-play"></i> Writing data from R to Excel files (xls|xlsx)</a></span> 
</p>
<ol start="4" style="list-style-type: decimal">
<li><a href="https://www.sthda.com/english/english/wiki/saving-data-into-r-data-format-rds-and-rdata"><i class="fa fa-folder-open"></i> <strong>Saving data into R data format: RDATA and RDS</strong></a> <br/></li>
</ol>
<ul>
<li>Save one object to a file: <strong>saveRDS</strong>(object, file), <strong>readRDS</strong>(file)</li>
<li>Save multiple objects to a file: <strong>save</strong>(data1, data2, file), <strong>load</strong>(file)</li>
<li>Save your entire workspace: <strong>save.image</strong>(), <strong>load</strong>()</li>
</ul>
<p><br/></p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/save-data-into-r-data-formats.png" alt="Save data into R data formats" /> <br/></p>
<ol style="list-style-type: decimal">
<li>Saving and restoring one single R object:</li>
</ol>
<pre class="r"><code># Save a single object to a file
saveRDS(mtcars, "mtcars.rds")

# Restore it under a different name
my_data <- readRDS("mtcars.rds")</code></pre>
<ol start="2" style="list-style-type: decimal">
<li>Saving and restoring one or more R objects:</li>
</ol>
<pre class="r"><code># Save multiple objects
save(data1, data2, file = "data.RData")

# To load the data again
load("data.RData")</code></pre>
<ol start="3" style="list-style-type: decimal">
<li>Saving and restoring your entire <a href="https://www.sthda.com/english/wiki/(unning-rstudio-and-setting-up-your-working-directory-easy-r-programming#use-r-inside-rstudio)">workspace</a>:</li>
</ol>
<pre class="r"><code># Save your workspace
save.image(file = "my_work_space.RData")

# Load the workspace again
load("my_work_space.RData")</code></pre>
<p><span class="success readmore">Read more: <a href="https://www.sthda.com/english/english/wiki/saving-data-into-r-data-format-rds-and-rdata"><i class="fa fa-play"></i> Saving data into R data format: RDATA and RDS</a></span> 
</p>
<ol start="5" style="list-style-type: decimal">
<li><a href="https://www.sthda.com/english/english/wiki/create-and-format-word-documents-using-r-software-and-reporters-package"><i class="fa fa-folder-open"></i> <strong>Create and format Word documents with R and ReporteRs package</strong></a> <br/></li>
</ol>
<p><strong>ReporteRs</strong> package, by David Gohel, provides easy to use functions to <strong>write</strong> and <strong>format</strong> <strong>Word documents</strong>. It can be also used to generate <strong>Word document from a template file</strong> with logos, fonts, etc. ReporteRs is Java-based solution, so it works on Windows, Linux and Mac OS systems.</p>
<ul>
<li>Install and load the ReporteRs R package</li>
<li>Create a simple Word document
<ul>
<li>Add texts : title and paragraphs of texts</li>
<li>Format the text of a Word document using R software</li>
<li>Add plots and images</li>
<li>Add a table</li>
<li>Add lists : ordered and unordered lists</li>
<li>Add a footnote to a Word document</li>
<li>Add R scripts</li>
</ul></li>
<li>Add a table of contents into a Word document</li>
</ul>
<p><br/></p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-word-document-image.png" alt="Write a Word document using R software and ReporteRs package" />
<br/></p>
<p><span class="success readmore">Read more: <a href="https://www.sthda.com/english/english/wiki/create-and-format-word-documents-using-r-software-and-reporters-package"><i class="fa fa-play"></i> Create and format Word documents with R and ReporteRs package</a></span> 
</p>
<ol start="6" style="list-style-type: decimal">
<li><a href="https://www.sthda.com/english/english/wiki/create-a-word-document-from-a-template-file-using-r-software-and-reporters-package"><i class="fa fa-folder-open"></i> <strong>Create a Word document from a template file with R and ReporteRs package</strong></a> <br/></li>
</ol>
<ul>
<li>Quick introduction to ReporteRs package</li>
<li>Create a Word document using a template file</li>
</ul>
<p><br/></p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-word-document-template-image.png" alt="Read and write a Word document from a template using R software and ReporteRs package" />
<br/></p>
<p><span class="success readmore">Read more: <a href="https://www.sthda.com/english/english/wiki/create-a-word-document-from-a-template-file-using-r-software-and-reporters-package"><i class="fa fa-play"></i> Create a Word document from a template file with R and ReporteRs package</a></span> 
</p>
<ol start="7" style="list-style-type: decimal">
<li><a href="https://www.sthda.com/english/english/wiki/add-a-table-into-a-word-document-using-r-software-and-reporters-package"><i class="fa fa-folder-open"></i> <strong>Add a table into a Word document with R and ReporteRs package</strong></a> <br/></li>
</ol>
<ul>
<li>Add a simple table</li>
<li>Add a formatted table
<ul>
<li>Change the background colors of rows and columns</li>
<li>Change cell background and text colors</li>
<li>Insert content into a table : header and footer rows</li>
</ul></li>
<li>Analyze, format and export a correlation matrix into a Word document</li>
<li>Powerpoint</li>
</ul>
<p><br/></p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-word-document-formatted-table1.png" alt="Add a table into a Word document with R and ReporteRs package" /> <br/></p>
<p><span class="success readmore">Read more: <a href="https://www.sthda.com/english/english/wiki/add-a-table-into-a-word-document-using-r-software-and-reporters-package"><i class="fa fa-play"></i> Add a table into a Word document with R and ReporteRs package</a></span> 
</p>
<ol start="8" style="list-style-type: decimal">
<li><a href="https://www.sthda.com/english/english/wiki/create-and-format-powerpoint-documents-from-r-software"><i class="fa fa-folder-open"></i> <strong>Create and format PowerPoint documents with R and ReporteRs</strong></a> <br/></li>
</ol>
<ul>
<li>Why is it important to be able to generate a PowerPoint report from R ?
<ul>
<li>Reason I : Many collaborators works with Microsoft office tools</li>
<li>Reason II : keeping beautiful R graphs beautiful for publications</li>
</ul></li>
<li>Install and load the ReporteRs package</li>
<li>Create a simple PowerPoint document
<ul>
<li>Slide layout</li>
<li>Generate a simple PowerPoint document from R software</li>
<li>Format the text of a PowerPoint document</li>
<li>Add plots and images</li>
<li>Add a table</li>
<li>Add ordered and unordered lists</li>
</ul></li>
<li>Create a PowerPoint document from a template file</li>
</ul>
<p><br/></p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-powerpoint-document-image.png" alt="Write a PowerPoint document using R software and ReporteRs package" /><br/></p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-powerpoint.png" alt="Write a PowerPoint document using R software and ReporteRs package" /> <br/></p>
<p><span class="success readmore">Read more: <a href="https://www.sthda.com/english/english/wiki/create-and-format-powerpoint-documents-from-r-software"><i class="fa fa-play"></i> Create and format PowerPoint documents with R and ReporteRs</a></span> 
</p>
<ol start="9" style="list-style-type: decimal">
<li><a href="https://www.sthda.com/english/english/wiki/create-an-editable-graph-from-r-software"><i class="fa fa-folder-open"></i> <strong>Create an editable graph from R to PowerPoint</strong></a> <br/></li>
</ol>
<ul>
<li>Case of base graphs</li>
<li>Case of graphs generated using ggplot2</li>
</ul>
<p><br/></p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-editable-ggplot2.png" alt="Editable plot from R software using ReporteRs package" />
<br/></p>
<p><span class="success readmore">Read more: <a href="https://www.sthda.com/english/english/wiki/create-an-editable-graph-from-r-software"><i class="fa fa-play"></i>Create an editable graph from R to PowerPoint</a></span> 
</p>
</div>
<p><br/></p>

<script>jQuery(document).ready(function () {
    jQuery('h1').addClass('wiki_paragraph1');
    jQuery('h2').addClass('wiki_paragraph2');
    jQuery('h3').addClass('wiki_paragraph3');
    jQuery('h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->


<!-- END HTML -->]]></description>
			<pubDate>Sun, 10 Apr 2016 18:01:37 +0200</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Create and format PowerPoint documents from R software]]></title>
			<link>https://www.sthda.com/english/wiki/create-and-format-powerpoint-documents-from-r-software</link>
			<guid>https://www.sthda.com/english/wiki/create-and-format-powerpoint-documents-from-r-software</guid>
			<description><![CDATA[<!-- START HTML -->

  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">

<div id="TOC">
<ul>
<li><a href="#why-is-it-important-to-be-able-to-generate-a-powerpoint-report-from-r">Why is it important to be able to generate a PowerPoint report from R ?</a><ul>
<li><a href="#reason-i-many-collaborators-works-with-microsoft-office-tools">Reason I : Many collaborators works with Microsoft office tools</a></li>
<li><a href="#reason-ii-keeping-beautiful-r-graphs-beautifull-for-publications">Reason II : keeping beautiful R graphs beautiful for publications</a></li>
</ul></li>
<li><a href="#but-wasnt-this-problem-solved-already">But wasn’t this problem solved already?</a></li>
<li><a href="#objective">Objective</a></li>
<li><a href="#install-and-load-the-reporters-package">Install and load the ReporteRs package</a></li>
<li><a href="#create-a-simple-powerpoint-document">Create a simple PowerPoint document</a><ul>
<li><a href="#slide-layout">Slide layout</a></li>
<li><a href="#generate-a-simple-powerpoint-document-from-r-software">Generate a simple PowerPoint document from R software</a></li>
<li><a href="#format-the-text-of-a-powerpoint-document">Format the text of a PowerPoint document</a><ul>
<li><a href="#text-properties-font-color-and-size">Text properties : font, color and size</a></li>
<li><a href="#change-the-appearance-of-a-piece-of-text-pot">Change the appearance of a “Piece Of Text” (pot)</a></li>
</ul></li>
<li><a href="#add-plots-and-images">Add plots and images</a></li>
<li><a href="#add-a-table">Add a table</a></li>
<li><a href="#add-ordered-and-unordered-lists">Add ordered and unordered lists</a></li>
</ul></li>
<li><a href="#create-a-powerpoint-document-from-a-template-file">Create a PowerPoint document from a template file</a><ul>
<li><a href="#download-a-template-file">Download a template file</a></li>
<li><a href="#slide-layouts-available-in-the-template-file">Slide layouts available in the template file</a></li>
<li><a href="#create-a-powerpoint-document-from-the-template-file">Create a PowerPoint document from the template file</a></li>
</ul></li>
<li><a href="#infos">Infos</a></li>
</ul>
</div>

<p><br/></p>
<div id="why-is-it-important-to-be-able-to-generate-a-powerpoint-report-from-r" class="section level1">
<h1>Why is it important to be able to generate a PowerPoint report from R ?</h1>
<p>There are at least, two reasons for this, as described in the next sections.</p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-powerpoint.png" alt="Write a PowerPoint document using R software and ReporteRs package" /></p>
<div id="reason-i-many-collaborators-works-with-microsoft-office-tools" class="section level2">
<h2>Reason I : Many collaborators works with Microsoft office tools</h2>
<p>About 1 billion people worldwide use Microsoft Office (1 in 7 people on the planet; <a href="http://news.microsoft.com/bythenumbers/index.html" rel = "nofollow" target="_blank">source: Microsoft</a>).</p>
<p>Furthermore, many collaborators still working with MS Office software (Word, PowerPoint, Excel) for :</p>
<ul>
<li>editing their text and tracking changes</li>
<li>copy-pasting texts, images and tables from multiple sources</li>
<li>saving and analyzing their data</li>
</ul>
<p>In this context, a report generated as a PDF or HTMl files is less useful with some collaborators.</p>
</div>
<div id="reason-ii-keeping-beautiful-r-graphs-beautifull-for-publications" class="section level2">
<h2>Reason II : keeping beautiful R graphs beautifull for publications</h2>
<p>R plots can be customized to be as beautiful as your imagination can make them. Unfortunately, preserving this beauty is not always an easy task when you want to publish these graphs or show them in a professional presentations.</p>
<p>Yes, this problem can be solved using knitr/rmarkdown/Latex/Beamer/Slidify. However, it would be very difficult to remake the whole presentation in a different format. Furthermore, many journals don’t accept Latex documents.</p>
<p><a href="https://assessingpsyche.wordpress.com/2013/07/27/keeping-beautiful-r-graphs-beautiful-in-powerpoint/" rel="nofollow" target="_blank">(source)</a>.</p>
</div>
</div>
<div id="but-wasnt-this-problem-solved-already" class="section level1">
<h1>But wasn’t this problem solved already?</h1>
<p>The answer of this question is yes and no. There have been several attempts to solve this problem, but many of them are not easy to use.</p>
<p>One of the previous solutions is <strong>R2PPT</strong> package. Unfortunately <strong>R2PPT</strong> is available for Windows OS only and it depends on rcom or RDCOMClient for generating Microsoft PowerPoint presentations.</p>
</div>
<div id="objective" class="section level1">
<h1>Objective</h1>
<p>The goal of this <strong>R tutorial</strong> is to show you how to easily and quickly, format and export R outputs (including data tables, plots, paragraphs of text and R scripts) from <strong>R statistical software</strong> to a Microsoft <strong>PowerPoint document</strong> (<strong>.pptx</strong> file format) using <strong>ReporteRs</strong> package.</p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-powerpoint-document-image.png" alt="Write a PowerPoint document using R software and ReporteRs package" /></p>
<p>ReporteRs is a Java-based solution, so it works on Windows, Mac and Linux.</p>
</div>
<div id="install-and-load-the-reporters-package" class="section level1">
<h1>Install and load the ReporteRs package</h1>
<p>Use the <strong>R code</strong> below :</p>
<pre class="r"><code>install.packages(&amp;#39;ReporteRs&amp;#39;) # Install
library(&amp;#39;ReporteRs&amp;#39;) # Load</code></pre>
<p><span class="warning">Note that ReporteRs relies on Java (>= 1.6) ; make sure you have an installed JRE</span></p>
<p>The version of Java installed on your computer, can be checked as follow :</p>
<pre class="r"><code>system("java -version")</code></pre>
</div>
<div id="create-a-simple-powerpoint-document" class="section level1">
<h1>Create a simple PowerPoint document</h1>
<p>Four simple steps are required :</p>
<ol style="list-style-type: decimal">
<li>Use the <strong>pptx()</strong> function to create a PowerPoint object.</li>
<li>Use the <strong>addSlide()</strong> function to add a slide into the PowerPoint document.</li>
<li>Add contents into the created slide using the functions below :
<ul>
<li><strong>addTitle</strong>: Add a title</li>
<li><strong>addParagraph</strong>: Add paragraphs of text</li>
<li><strong>addFlexTable</strong>: Add a table</li>
<li><strong>addPlot</strong>: Add a plot generated in R</li>
<li><strong>addImage</strong>: Add external images</li>
<li><strong>addRScript</strong>: Highlight and add R code</li>
<li><strong>addDate</strong> : Add a date</li>
<li><strong>addPageNumber</strong> : Add a page number</li>
<li><strong>AddFooter</strong> : Add a footer</li>
</ul></li>
<li>Write the document into a .pptx file using <strong>writeDoc()</strong> function</li>
</ol>
<div id="slide-layout" class="section level2">
<h2>Slide layout</h2>
<p>Before showing you an example of how to create and format PowerPoint from R Software, let’s first discuss about <strong>slide layout</strong>. This is very important to understand the examples provided in this tutorial.</p>
<p>When creating a new slide, you should specify the layout of the slide. The available layouts in the “MS Office PowerPoint” (default template, on my computer) are illustrated in the figure below :</p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-powerpoint-slide-layout.png" alt="Write a PowerPoint document using R software and ReporteRs package" /></p>
<p><strong>addSlide()</strong> function can be used to add a new slide into a PowerPoint document from <strong>R software</strong>. A simplified format of the function is :</p>
<pre class="r"><code>doc <- addSlide(doc, slide.layout)</code></pre>
<br/>
<div class="block">
<ul>
<li><em>doc</em> : a <strong>pptx</strong> object where slide has to be added</li>
<li><em>slide.layout</em> : the layout to use for the slide.</li>
</ul>
</div>
<p><br/></p>
<p>Among the possible values for the argument <em>slide.layout</em>, there are : “Title Slide”, “Title and Content”, “Two Content”, “Section Header”, “Content with Caption”, “Title Only”, “Comparison”.</p>
<p><span class="notice">However, you should use only the available slide layouts in your computer. </span></p>
<br/>
<div class="success">
<p>To view the slide layouts available in your computer, use the R code below :</p>
<pre class="r"><code>library(ReporteRs)
doc = pptx()
slide.layouts(doc)</code></pre>
<pre><code> [1] "Title Slide"             "Title and Vertical Text" "Title and Content"       "Two Content"            
 [5] "Section Header"          "Vertical Title and Text" "Content with Caption"    "Title Only"             
 [9] "Comparison"              "Blank"                  </code></pre>
</div>
<p><br/></p>
<p>These layouts are illustrated below :</p>
<pre class="r"><code>doc <- pptx()
layouts <-slide.layouts(doc) # All available layout
#  plot each slide style
for(i in layouts ){
  par(mar=c(0.5,0.5,2,0.5), cex=0.7)
  slide.layouts(doc, i )
    title(main = paste0("&amp;#39;", i, "&amp;#39;" ))
  if(interactive()) readline(prompt = "Show next slide layout")
}</code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/figure/import-export-data/r-reporters-powerpoint-r-reporters-powerpoint-slide-layout-1.png" title="plot of chunk r-reporters-powerpoint-slide-layout-" alt="plot of chunk r-reporters-powerpoint-slide-layout-" width="268.8" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/import-export-data/r-reporters-powerpoint-r-reporters-powerpoint-slide-layout-2.png" title="plot of chunk r-reporters-powerpoint-slide-layout-" alt="plot of chunk r-reporters-powerpoint-slide-layout-" width="268.8" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/import-export-data/r-reporters-powerpoint-r-reporters-powerpoint-slide-layout-3.png" title="plot of chunk r-reporters-powerpoint-slide-layout-" alt="plot of chunk r-reporters-powerpoint-slide-layout-" width="268.8" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/import-export-data/r-reporters-powerpoint-r-reporters-powerpoint-slide-layout-4.png" title="plot of chunk r-reporters-powerpoint-slide-layout-" alt="plot of chunk r-reporters-powerpoint-slide-layout-" width="268.8" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/import-export-data/r-reporters-powerpoint-r-reporters-powerpoint-slide-layout-5.png" title="plot of chunk r-reporters-powerpoint-slide-layout-" alt="plot of chunk r-reporters-powerpoint-slide-layout-" width="268.8" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/import-export-data/r-reporters-powerpoint-r-reporters-powerpoint-slide-layout-6.png" title="plot of chunk r-reporters-powerpoint-slide-layout-" alt="plot of chunk r-reporters-powerpoint-slide-layout-" width="268.8" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/import-export-data/r-reporters-powerpoint-r-reporters-powerpoint-slide-layout-7.png" title="plot of chunk r-reporters-powerpoint-slide-layout-" alt="plot of chunk r-reporters-powerpoint-slide-layout-" width="268.8" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/import-export-data/r-reporters-powerpoint-r-reporters-powerpoint-slide-layout-8.png" title="plot of chunk r-reporters-powerpoint-slide-layout-" alt="plot of chunk r-reporters-powerpoint-slide-layout-" width="268.8" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/import-export-data/r-reporters-powerpoint-r-reporters-powerpoint-slide-layout-9.png" title="plot of chunk r-reporters-powerpoint-slide-layout-" alt="plot of chunk r-reporters-powerpoint-slide-layout-" width="268.8" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/import-export-data/r-reporters-powerpoint-r-reporters-powerpoint-slide-layout-10.png" title="plot of chunk r-reporters-powerpoint-slide-layout-" alt="plot of chunk r-reporters-powerpoint-slide-layout-" width="268.8" style="margin-bottom:10px;" /></p>
<div class="warning">
<p>Note that, the selected layout determines the contents you can add into the slide (See the figure above).</p>
<ol style="list-style-type: decimal">
<li>Example 1 - If you choose <strong>‘Title and Content’</strong> as a slide layout, you can add only :
<ul>
<li>a <strong>title</strong></li>
<li>and <strong>one content</strong> which can be texts, plots, images, tables or R code</li>
</ul></li>
<li>Example 2 - If you choose <em>‘Two Content’</em> as a slide layout, you can add :
<ul>
<li>a <strong>title</strong></li>
<li>and two contents : For example, you can add a <em>table</em> in the left panel and a <em>paragraph of texts</em> in the right panel.</li>
</ul></li>
<li>Example 3 - If you choose <strong>‘Comparison’</strong>, you can add a title and four contents (plots, tables, paragraphs, images)</li>
</ol>
</div>
<p><span class="success">Whatever the slide layout chosen, you can use the functions <strong>addDate()</strong>, <strong>addFooter()</strong> and <strong>addPageNumber()</strong> to add date, footer and slide number, respectively. </span></p>
</div>
<div id="generate-a-simple-powerpoint-document-from-r-software" class="section level2">
<h2>Generate a simple PowerPoint document from R software</h2>
<p>The R code below creates a PowerPoint document with a title slide, plots, tables, and an R script :</p>
<pre class="r"><code>library( ReporteRs )

# Create a PowerPoint document
doc = pptx( )

# Slide 1 : Title slide
#+++++++++++++++++++++++
doc <- addSlide(doc, "Title Slide")
doc <- addTitle(doc,"Create a PowerPoint document from R software")
doc <- addSubtitle(doc, "R and ReporteRs package")
doc <- addDate(doc)
doc <- addFooter(doc, "Isaac Newton")
doc <- addPageNumber(doc, "1/4")

# Slide 2 : Add plot
#+++++++++++++++++++++++
doc <- addSlide(doc, "Title and Content")
doc <- addTitle(doc, "Bar plot")
plotFunc<- function(){
  barplot(VADeaths, beside = TRUE,
          col = c("lightblue", "mistyrose", "lightcyan",
                  "lavender", "cornsilk"),
  legend = rownames(VADeaths), ylim = c(0, 100))
  title(main = "Death Rates in Virginia", font.main = 4)
}
doc <- addPlot(doc, plotFunc )
doc <- addPageNumber(doc, "2/4")

# Slide 3 : Add table 
#+++++++++++++++++++++++
doc <- addSlide(doc, "Two Content")
doc <- addTitle(doc,"iris data sets")
doc <- addFlexTable(doc, FlexTable(iris[1:10,] ))
doc <- addParagraph(doc, "iris data set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are Iris setosa, versicolor, and virginica.")
doc <- addPageNumber(doc, "3/4")

# Silde 4 : Add R script
#+++++++++++++++++++++
doc <- addSlide(doc, "Content with Caption")
doc <- addTitle(doc, "R Script for histogram plot")
doc <- addPlot(doc, function() hist(iris$Sepal.Width, col=4))
r_code ="data(iris)
hist(iris$Sepal.Width, col = 4)"
doc <- addRScript(doc, text=r_code)

# write the document 
writeDoc(doc, "r-reporters-powerpoint.pptx" )</code></pre>
<p><span class="success">The PowerPoint document created by the <strong>R</strong> code above is available here : <a href="https://www.sthda.com/english/sthda/RDoc/example-files/r-reporters-powerpoint.docx">R software and ReporteRs package - Example of creating a PowerPoint document</a></span></p>
<iframe src="http://www.slideshare.net/slideshow/embed_code/43182752" width="510" height="420" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;" allowfullscreen> </iframe>  
   



<div class="block">
<p>Note that, you can use <strong>addPageNumber()</strong> function without specifying the value of the slide number, as follow :</p>
<pre class="r"><code>doc <- addPageNumber(doc)</code></pre>
<p>In this case slide number is added using the default setting (e.g : 1 for slide1, 2 for slide 2).</p>
<p>If you want to customize the numbering, use the function as follow :</p>
<pre class="r"><code>doc <- addPageNumber(doc, "1/2") # Example 1
doc <- addPageNumber(doc, "I") # Example 2</code></pre>
</div>
<p><br/></p>
</div>
<div id="format-the-text-of-a-powerpoint-document" class="section level2">
<h2>Format the text of a PowerPoint document</h2>
<div id="text-properties-font-color-and-size" class="section level3">
<h3>Text properties : font, color and size</h3>
<p>As illustrated in the figure below, text properties include :</p>
<ul>
<li><strong>Font</strong> : family (e.g : “Arial”), size (e.g : 11), style (e.g : “italic”)</li>
<li><strong>Underlined</strong> text</li>
<li><strong>Color</strong> (e.g : “blue”)</li>
<li><strong>Vertical align</strong> (superscript, subscript)</li>
</ul>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-powerpoint-text-properties.png" alt="Write a PowerPoint document using R software and ReporteRs package" /></p>
<p>The default <strong>font size</strong> and <strong>font family</strong> of the PowerPoint can be modified as follow :</p>
<pre class="r"><code>options( "ReporteRs-fontsize" = 18, 
         "ReporteRs-default-font" = "Arial")</code></pre>
</div>
<div id="change-the-appearance-of-a-piece-of-text-pot" class="section level3">
<h3>Change the appearance of a “Piece Of Text” (pot)</h3>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-word-document-text-appearance.png" alt="Write a PowerPoint document using R software and ReporteRs package, format the text" /></p>
<p><br/> The function <strong>pot()</strong> [Pieces Of Text] is used to modify the appearance of a text. It can be used also to create a <strong>hyperlink</strong>. The format is :</p>
<pre class="r"><code>pot(value="", format = textProperties())</code></pre>
<br/>
<div class="block">
<ul>
<li><strong>value</strong> : the text to be formatted</li>
<li><strong>format</strong> : the properties to use for formatting the text</li>
</ul>
</div>
<p><br/></p>
<p>The allowed values for the argument <code>format</code> are the following functions :</p>
<ul>
<li><em>textProperties()</em> : the text formatting properties</li>
<li><em>textBold()</em>, <em>textItalic()</em>, <em>textBoldItalic()</em> and <em>textNormal()</em> which are shortcuts for <em>bold</em>, <em>italic</em>, <em>bold-italic</em> and <em>normal</em> text, respectively.</li>
</ul>
<p>These functions can take the arguments below :</p>
<br/>
<div class="block">
<ul>
<li>color : font color; e.g : color=“#000000” or color = “black”.</li>
<li>font.size : a integer indicating the <em>font size</em>.</li>
<li>font.weight : the <em>font weight</em>. Possible values are “normal” or “bold”.</li>
<li>font.style : the <em>font style</em>. Possible values are “normal” or “italic”.</li>
<li>underlined : a logical value specifying if the text should be <em>underlined</em>.</li>
<li>font.family : the <em>font family</em>; e.g : “Arial”.</li>
<li>vertical.align : a character indicating font <em>vertical alignments</em>. Expected values are “baseline”" or “subscript” or “superscript”. Default value is <em>baseline</em>.</li>
<li>shading.color : background color of the text (e.g “#000000” or “black”)</li>
</ul>
</div>
<p><br/></p>
<p>The <strong>R</strong> code below creates a PowerPoint document containing a formatted text and a hyperlink :</p>
<pre class="r"><code>library( ReporteRs )

# Change the default font size and font family
options(&amp;#39;ReporteRs-fontsize&amp;#39;= 18, &amp;#39;ReporteRs-default-font&amp;#39;=&amp;#39;Arial&amp;#39;)

doc = pptx( )

doc <- addSlide(doc, "Two Content")
doc <- addTitle(doc,"Document with formatted texts")
doc <- addFlexTable(doc, FlexTable(iris[1:10,] ))

my_text <- pot("iris data set", textBold(color = "blue"))+
          " contains the measurements of " + 
          pot("sepal length", textBold(color="red"))+ 
        " and width and petal length and width"
  
my_link <- pot(&amp;#39;Click here to visit STHDA web site!&amp;#39;, 
    hyperlink = &amp;#39;https://www.sthda.com/english&amp;#39;,
    format=textBoldItalic(color = &amp;#39;blue&amp;#39;, underline = TRUE ))

doc <- addParagraph(doc, 
      value = set_of_paragraphs(my_text, " ",  my_link),
     par.properties=parProperties(text.align="justify")
    )

writeDoc(doc, "r-reporters-powerpoint-formatted.pptx" )</code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-powerpoint-formatted-text.png" alt="Write a PowerPoint document using R software and ReporteRs package, format the text" /></p>
</div>
</div>
<div id="add-plots-and-images" class="section level2">
<h2>Add plots and images</h2>
<p>The functions <strong>addPlot()</strong> and <strong>addImage()</strong> can be used for adding a plot or an external image to the document. <em>addPlot()</em> works with all R plots (base graphics, lattice, ggplot2 and grid).</p>
<p>These two functions can be used as follow :</p>
<pre class="r"><code># Add plots
# fun : R plotting function
# ... : other arguments to pass to the plotting function
addPlot(doc, fun, ...)

# Add images
# filename : path to the external image
addImage(doc, filename)</code></pre>
<p>The R code below creates a PowerPoint document containing a histogram and an image (downloaded from R website) :</p>
<pre class="r"><code>library( ReporteRs )

doc = pptx()

# Slide 1 : Title slide
doc <- addSlide(doc, "Title Slide")
doc <- addTitle(doc,"Document containing plots and images")
doc <- addSubtitle(doc, "R and ReporteRs package")

# Slide 2 : Add plot
doc <- addSlide(doc, "Two Content")
doc <- addTitle(doc,"Histogram plot")
doc <- addPlot(doc, function() hist(iris$Sepal.Width, col="lightblue"))
doc <- addParagraph(doc, "This histogram is generated using iris data sets")

# Slide 3 : Add  an image
# download an image from R website
download.file(url="http://www.r-project.org/hpgraphic.png",
              destfile="r-home-image.png", quiet=TRUE)
doc <- addSlide(doc, "Two Content")
doc <- addTitle(doc,"Image from R website")
doc <- addImage(doc, "r-home-image.png")
doc <- addParagraph(doc, "This image has been downloaded from R website")
 
writeDoc(doc, "r-reporters-powerpoint-plot-image.pptx")</code></pre>
<p><span class="success">The PowerPoint document created by the <strong>R</strong> code above is available here : <a href="https://www.sthda.com/english/sthda/RDoc/example-files/r-reporters-powerpoint-plot-image.pptx">R software and ReporteRs package - PowerPoint document containing plots and images</a></span></p>
<iframe src="http://www.slideshare.net/slideshow/embed_code/43187047" width="510" height="420" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;" allowfullscreen> </iframe>
    



<div class="block">
<ol style="list-style-type: decimal">
<li><p>Note that, <strong>addPlot()</strong> function can take other arguments such as <em>pointsize</em> to change the size of plotted texts (default value is 12; in pixels)</p></li>
<li>For <em>addImage()</em> function, the allowed file formats are PNG, WMF, JPEG and GIF images..</li>
</ol>
</div>
<p><br/></p>
</div>
<div id="add-a-table" class="section level2">
<h2>Add a table</h2>
<p><strong>addFlexTable()</strong> function is used to format and add a table into the PowerPoint.</p>
<p>It can be used as follow :</p>
<ul>
<li>STEP 1 : Create a table using <em>FlexTable()</em> or <em>vanilla.table()</em> function. These two functions generate a ‘flexible’ table which can be easily formatted before adding into the slide.</li>
<li>STEP 2 : Add the create table into the document using <em>addFlexTable()</em> function as follow :</li>
</ul>
<pre class="r"><code># doc : pptx object
# flextable : FlexTable object

# Example 1
doc <- addFlexTable(doc, flextable = FlexTable(data))

# Example 2 
doc <- addFlexTable(doc, flextable = vanilla.table(data))</code></pre>
<p><span class="success"><strong>setZebraStyle()</strong> function can be used to color odd and even rows differently; for example, <em>odd rows</em> in <em>gray</em> color and <em>even rows</em> in <em>white</em> color.</span></p>
<p>The example below creates a PowerPoint document with 3 slides containing a simple table (slide 1), vanilla table (slide 2) and a zebra striped table (slide 3) :</p>
<pre class="r"><code>doc = pptx()

data<-iris[1:5, ]

# Slide 1 : Simple table
doc <- addSlide(doc, "Title and Content")
doc <- addTitle(doc,"Simple table")
doc <- addFlexTable(doc, FlexTable(data))

# Slide 2 : vanilla table
doc <- addSlide(doc, "Title and Content")
doc <- addTitle(doc,"Vanilla table")
doc <- addFlexTable(doc, vanilla.table(data))

# Slide 3 : Zebra striped table
doc <- addSlide(doc, "Title and Content")
doc <- addTitle(doc,"Zebra striped table")
MyFTable <- vanilla.table(data)
MyFTable <- setZebraStyle(MyFTable, odd = &amp;#39;#eeeeee&amp;#39;, even = &amp;#39;white&amp;#39;)
doc <- addFlexTable( doc, MyFTable)

writeDoc(doc, "r-reporters-powerpoint-add-table.pptx")</code></pre>
<p><span class="success">The PowerPoint document created by the <strong>R</strong> code above is available here : <a href="https://www.sthda.com/english/sthda/RDoc/example-files/r-reporters-powerpoint-add-table.pptx">R software and ReporteRs package - PowerPoint document containing tables</a></span></p>
<iframe src="http://www.slideshare.net/slideshow/embed_code/43188796" width="510" height="420" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;" allowfullscreen> </iframe>
      


</div>
<div id="add-ordered-and-unordered-lists" class="section level2">
<h2>Add ordered and unordered lists</h2>
<p>Lists can be added using <strong>addParagraph()</strong> function as follow :</p>
<pre class="r"><code>doc  <- addParagraph(doc, 
  value = c(&amp;#39;Item 1&amp;#39;, "Item 2", "Item 3")
  par.properties = parProperties(list.style = &amp;#39;ordered&amp;#39;, level = 1 )</code></pre>
<br/>
<div class="block">
<ul>
<li><strong>value</strong> : a set of items to be added as a list</li>
<li><strong>par.properties</strong> : the paragraph formatting properties. It takes <em>list.style</em> and <em>level</em> as arguments :
<ul>
<li><em>list.style</em> : possible values are ‘unordered’ and ‘ordered’</li>
<li><em>level</em> : a numeric value indicating the level of the item to be added in the list</li>
</ul></li>
</ul>
</div>
<p><br/></p>
<p>The example below generates a one-slide PowerPoint document containing an ordered and unordered lists :</p>
<pre class="r"><code>doc <- pptx()

doc <- addSlide(doc, "Two Content")
doc <- addTitle(doc, "Ordered and unordored lists")

# 1. Ordered list
doc <- addParagraph(doc, value= c("Item 1", "Item 2", "Item 3"),
          par.properties =  parProperties(list.style = &amp;#39;ordered&amp;#39;))

# 2. Unordered list
doc <- addParagraph(doc, value= c("Item 1", "Item 2", "Item 3"),
          par.properties =  parProperties(list.style = &amp;#39;unordered&amp;#39;))

writeDoc(doc, file = "r-reporters-powerpoint-lists.pptx")</code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-word-document-simple-list.png" alt="R software and Reporters package, add lists to a Powerpoint document" /></p>
</div>
</div>
<div id="create-a-powerpoint-document-from-a-template-file" class="section level1">
<h1>Create a PowerPoint document from a template file</h1>
<p>This approach is useful in many situations :</p>
<ul>
<li>If you work in a corporate environment and you want to generate a PowerPoint document based on a template with specific fonts, color, logos, etc.</li>
<li>If you want to modify and insert new contents into an existing PowerPoint document.</li>
<li>If you want to use text formatting styles and slide layouts from a given template file.</li>
</ul>
<p><span class="warning">Note that, if you use a template file to create a PowerPoint document, slide layouts are those available in the template.</span></p>
<p>A template file can be specified to the <strong>pptx()</strong> function as follow :</p>
<pre class="r"><code># Create a PowerPoint document
doc <- pptx(template="path/to/your/powerpoint/template/file.pptx")

# ...............
# Add contents
# ...............

# Write the PowerPoint document to a file 
writeDoc(doc, file = "output-file.pptx")</code></pre>
<p>In the next section We’ll :</p>
<ul>
<li>download a PowerPoint template file from STHDA website</li>
<li>Check the available slide layouts in the template file</li>
<li>Create a PowerPoint document based on the template</li>
</ul>
<div id="download-a-template-file" class="section level2">
<h2>Download a template file</h2>
<pre class="r"><code># Download a PowerPoint template file from STHDA website
download.file(url="https://www.sthda.com/sthda/RDoc/example-files/r-reporters-powerpoint-template.pptx",
    destfile="r-reporters-powerpoint-template.pptx", quiet=TRUE)</code></pre>
</div>
<div id="slide-layouts-available-in-the-template-file" class="section level2">
<h2>Slide layouts available in the template file</h2>
<p>You can use one of the layout below when adding a new slide into the PowerPoint :</p>
<pre class="r"><code>doc <- pptx(template="r-reporters-powerpoint-template.pptx")
layouts <-slide.layouts(doc) # All available layout
# Plot the layouts
for(i in layouts ){
  par(mar=c(0.5,0.5,2,0.5), cex=0.7)
  slide.layouts(doc, i )
  title(main = paste0("&amp;#39;", i, "&amp;#39;" ))
  if(interactive()) readline(prompt = "Show next slide layout")
}</code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/figure/import-export-data/r-reporters-powerpoint-r-reporters-powerPoint-template-layout1.png" title="plot of chunk r-reporters-powerPoint-template-layout" alt="plot of chunk r-reporters-powerPoint-template-layout" width="268.8" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/import-export-data/r-reporters-powerpoint-r-reporters-powerPoint-template-layout2.png" title="plot of chunk r-reporters-powerPoint-template-layout" alt="plot of chunk r-reporters-powerPoint-template-layout" width="268.8" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/import-export-data/r-reporters-powerpoint-r-reporters-powerPoint-template-layout3.png" title="plot of chunk r-reporters-powerPoint-template-layout" alt="plot of chunk r-reporters-powerPoint-template-layout" width="268.8" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/import-export-data/r-reporters-powerpoint-r-reporters-powerPoint-template-layout4.png" title="plot of chunk r-reporters-powerPoint-template-layout" alt="plot of chunk r-reporters-powerPoint-template-layout" width="268.8" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/import-export-data/r-reporters-powerpoint-r-reporters-powerPoint-template-layout5.png" title="plot of chunk r-reporters-powerPoint-template-layout" alt="plot of chunk r-reporters-powerPoint-template-layout" width="268.8" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/import-export-data/r-reporters-powerpoint-r-reporters-powerPoint-template-layout6.png" title="plot of chunk r-reporters-powerPoint-template-layout" alt="plot of chunk r-reporters-powerPoint-template-layout" width="268.8" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/import-export-data/r-reporters-powerpoint-r-reporters-powerPoint-template-layout7.png" title="plot of chunk r-reporters-powerPoint-template-layout" alt="plot of chunk r-reporters-powerPoint-template-layout" width="268.8" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/import-export-data/r-reporters-powerpoint-r-reporters-powerPoint-template-layout8.png" title="plot of chunk r-reporters-powerPoint-template-layout" alt="plot of chunk r-reporters-powerPoint-template-layout" width="268.8" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/import-export-data/r-reporters-powerpoint-r-reporters-powerPoint-template-layout9.png" title="plot of chunk r-reporters-powerPoint-template-layout" alt="plot of chunk r-reporters-powerPoint-template-layout" width="268.8" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/import-export-data/r-reporters-powerpoint-r-reporters-powerPoint-template-layout10.png" title="plot of chunk r-reporters-powerPoint-template-layout" alt="plot of chunk r-reporters-powerPoint-template-layout" width="268.8" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/import-export-data/r-reporters-powerpoint-r-reporters-powerPoint-template-layout11.png" title="plot of chunk r-reporters-powerPoint-template-layout" alt="plot of chunk r-reporters-powerPoint-template-layout" width="268.8" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/import-export-data/r-reporters-powerpoint-r-reporters-powerPoint-template-layout12.png" title="plot of chunk r-reporters-powerPoint-template-layout" alt="plot of chunk r-reporters-powerPoint-template-layout" width="268.8" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/import-export-data/r-reporters-powerpoint-r-reporters-powerPoint-template-layout13.png" title="plot of chunk r-reporters-powerPoint-template-layout" alt="plot of chunk r-reporters-powerPoint-template-layout" width="268.8" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/import-export-data/r-reporters-powerpoint-r-reporters-powerPoint-template-layout14.png" title="plot of chunk r-reporters-powerPoint-template-layout" alt="plot of chunk r-reporters-powerPoint-template-layout" width="268.8" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/import-export-data/r-reporters-powerpoint-r-reporters-powerPoint-template-layout15.png" title="plot of chunk r-reporters-powerPoint-template-layout" alt="plot of chunk r-reporters-powerPoint-template-layout" width="268.8" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/import-export-data/r-reporters-powerpoint-r-reporters-powerPoint-template-layout16.png" title="plot of chunk r-reporters-powerPoint-template-layout" alt="plot of chunk r-reporters-powerPoint-template-layout" width="268.8" style="margin-bottom:10px;" /></p>
</div>
<div id="create-a-powerpoint-document-from-the-template-file" class="section level2">
<h2>Create a PowerPoint document from the template file</h2>
<p><span class="notice"> Note that, the template file contains already one empty slide which can be removed manually.</span></p>
<pre class="r"><code>library( ReporteRs )

# Download a PowerPoint template file from STHDA website
download.file(url="https://www.sthda.com/sthda/RDoc/example-files/r-reporters-powerpoint-template.pptx",
    destfile="r-reporters-powerpoint-template.pptx", quiet=TRUE)

options(&amp;#39;ReporteRs-fontsize&amp;#39;= 18, &amp;#39;ReporteRs-default-font&amp;#39;=&amp;#39;Arial&amp;#39;)

doc <- pptx(template="r-reporters-powerpoint-template.pptx" )

# Slide 1 : Title slide
#+++++++++++++++++++++++
doc <- addSlide(doc, "Title Slide")
doc <- addTitle(doc,"Create a PowerPoint from template using R software")
doc <- addSubtitle(doc, "R and ReporteRs package")
doc <- addDate(doc)
doc <- addFooter(doc, "Isaac Newton")
doc <- addPageNumber(doc, "1/4")

# Slide 2 : Add plot
#+++++++++++++++++++++++
doc <- addSlide(doc, "Title and Content")
doc <- addTitle(doc, "Bar plot")
plotFunc<- function(){
  barplot(VADeaths, beside = TRUE,
          col = c("lightblue", "mistyrose", "lightcyan",
                  "lavender", "cornsilk"),
  legend = rownames(VADeaths), ylim = c(0, 100))
  title(main = "Death Rates in Virginia", font.main = 4)
}
doc <- addPlot(doc, plotFunc )
doc <- addPageNumber(doc, "2/4")

# Slide 3 : Add table 
#+++++++++++++++++++++++
doc <- addSlide(doc, "Two Content")
doc <- addTitle(doc,"iris data sets")
doc <- addFlexTable(doc, FlexTable(iris[1:4,] ))
doc <- addParagraph(doc, "iris data set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are Iris setosa, versicolor, and virginica.")
doc <- addPageNumber(doc, "3/4")

# Silde 4 : Add R script
#+++++++++++++++++++++
doc <- addSlide(doc, "Content with Caption")
doc <- addTitle(doc, "R Script for histogram plot")
doc <- addPlot(doc, function() hist(iris$Sepal.Width, col=4))
r_code ="data(iris)
hist(iris$Sepal.Width, col = 4)"
doc <- addRScript(doc, text=r_code)

# write the document 
writeDoc(doc, "r-reporters-powerpoint-from-template.pptx" )</code></pre>
<p><span class="success">The PowerPoint document created by the <strong>R</strong> code above is available here : <a href="https://www.sthda.com/english/sthda/RDoc/example-files/r-reporters-powerpoint-from-template.pptx">R software and ReporteRs package - PowerPoint document from template</a></span></p>
<iframe src="http://www.slideshare.net/slideshow/embed_code/43190417" width="510" height="420" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;" allowfullscreen> </iframe>



</div>
</div>
<div id="infos" class="section level1">
<h1>Infos</h1>
<p><span class="warning"> This analysis has been performed using R (ver. 3.1.0). </span></p>
<p>You can read more about ReporteRs and download the source code at the following link :</p>
<p>GitHub (David Gohel): <a href="https://github.com/davidgohel/ReporteRs" rel="nofollow" target="_blank">ReporteRs</a></p>
</div>

<script>jQuery(document).ready(function () {
    jQuery('h1').addClass('wiki_paragraph1');
    jQuery('h2').addClass('wiki_paragraph2');
    jQuery('h3').addClass('wiki_paragraph3');
    jQuery('h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->

<!-- END HTML -->]]></description>
			<pubDate>Sat, 09 Apr 2016 10:28:43 +0200</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Create an editable graph from R software]]></title>
			<link>https://www.sthda.com/english/wiki/create-an-editable-graph-from-r-software</link>
			<guid>https://www.sthda.com/english/wiki/create-an-editable-graph-from-r-software</guid>
			<description><![CDATA[<!-- START HTML -->

            
  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">

<div id="TOC">
<ul>
<li><a href="#who-is-this-article-for">Who is this article for ?</a></li>
<li><a href="#what-r-package-to-use">What R package to use ?</a></li>
<li><a href="#create-editable-plots">Create editable plots</a><ul>
<li><a href="#case-of-base-graphs">Case of base graphs</a></li>
<li><a href="#case-of-graphes-generated-using-ggplot2">Case of graphes generated using ggplot2</a></li>
</ul></li>
<li><a href="#infos">Infos</a></li>
</ul>
</div>

<p><br/></p>
<p>In this article you’ll learn how to create an <strong>editable vector</strong> graphics from <strong>R software</strong>. This could be interesting in some cases and gives you the ability to edit your graphics from PowerPoint. You can change line types, colors, point shapes, etc, …</p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/plot-pencil.jpg" alt="Editable plot from R software" /></p>
<div id="who-is-this-article-for" class="section level1">
<h1>Who is this article for ?</h1>
<p>If you want to export your plot from R to PowerPoint automatically, this is for you.</p>
<p>If you want to bring your <strong>ggplot2 charts to PowerPoint</strong>, then this guide is for you.</p>
<p>If you’re looking for an exact package to create an <strong>editable plot</strong> and to save it as a PowerPoint document, then you’ll love this tutorial.</p>
<p>If you’re a beginner in <strong>R programming</strong>, you’ll definitely learn something new that you can use if needed.</p>
</div>
<div id="what-r-package-to-use" class="section level1">
<h1>What R package to use ?</h1>
<p>Editable vector graphics can be created and saved in a PowerPoint document using <strong>ReporteRs</strong> package.</p>
<p>Install and load the package as follow :</p>
<pre class="r"><code>install.packages(&amp;#39;ReporteRs&amp;#39;) # Install
library(&amp;#39;ReporteRs&amp;#39;) # Load</code></pre>
</div>
<div id="create-editable-plots" class="section level1">
<h1>Create editable plots</h1>
<div id="case-of-base-graphs" class="section level2">
<h2>Case of base graphs</h2>
<p>The example below creates a PowerPoint document in your current working directory. The document contains one slide with 2 panels :</p>
<ul>
<li>The first panel contains an editable box plot</li>
<li>The second panel contains a raster format</li>
</ul>
<p><span class="success">The PowerPoint document created by the <strong>R</strong> code below is available here : <a href="https://www.sthda.com/english/sthda/RDoc/example-files/editable-graph.pptx">R software and ReporteRs package - create an editable base graph</a></span></p>
<pre class="r"><code>library(&amp;#39;ReporteRs&amp;#39;)
# Create a new powerpoint document
doc <- pptx()

# Add a new slide into the ppt document 
doc <- addSlide(doc, "Two Content" )

# add a slide title
doc<- addTitle(doc, "Editable vector graphics format versus raster format" )

# A function for creating a box plot
boxplotFunc<- function(){
      boxplot(len ~ dose, data = ToothGrowth, 
        col=2:4,main = "Guinea Pigs&amp;#39; Tooth Growth",
        xlab = "Vitamin C dose mg",
        ylab = "tooth length")
      }

# Add an editable box plot
doc <- addPlot(doc, boxplotFunc, vector.graphic = TRUE )

# Add a raster box plot
doc <- addPlot(doc, boxplotFunc, vector.graphic = FALSE )

# write the document to a file
writeDoc(doc, file = "editable-graph.pptx")</code></pre>
<p><span class="success">Open the created PowerPoint and try to edit the first box plot by changing the fill colors, line types, etc…</span></p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-editable-graph.png" alt="Editable plot from R software using ReporteRs package" /></p>
</div>
<div id="case-of-graphes-generated-using-ggplot2" class="section level2">
<h2>Case of graphs generated using ggplot2</h2>
<p>ggplot2 is a powerful R package, implemented by Hadley Wickham, for producing a visually appealing charts.</p>
<p>Install and load it as follow :</p>
<pre class="r"><code>install.packages(&amp;#39;ggplot2&amp;#39;) # Install
library(&amp;#39;ggplot2&amp;#39;) # Load</code></pre>
<p><strong>Create an editable plot with ggplot2</strong> :</p>
<pre class="r"><code>library(&amp;#39;ReporteRs&amp;#39;)
library(ggplot2)
# Create a new powerpoint document
doc <- pptx()

# Add a new slide into the ppt document 
doc <- addSlide(doc, "Two Content" )

# add a slide title
doc<- addTitle(doc, "Editable vector graphics format versus raster format" )

# A function for creating a box plot
bp <- ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group))+
        geom_boxplot()

# Add an editable box plot
doc <- addPlot(doc, function() print(bp), vector.graphic = TRUE )

# Add a raster box plot
doc <- addPlot(doc, function() print(bp), vector.graphic = FALSE )

# write the document to a file
writeDoc(doc, file = "editable-ggplot2.pptx")</code></pre>
<p><span class="success">The PowerPoint document created by the <strong>R</strong> code above is available here : <a href="https://www.sthda.com/english/sthda/RDoc/example-files/editable-ggplot2.pptx">R software and ReporteRs package - create an editable ggplot2</a></span></p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-editable-ggplot2.png" alt="Editable plot from R software using ReporteRs package" /></p>
</div>
</div>
<div id="infos" class="section level1">
<h1>Infos</h1>
<p><span class="warning"> This analysis has been performed using R (ver. 3.1.0). </span></p>
<p>You can read more about ReporteRs and download the source code at the following link :</p>
<p>GitHub (David Gohel): <a href="https://github.com/davidgohel/ReporteRs" rel="nofollow" target="_blank">ReporteRs</a></p>
</div>

<script>jQuery(document).ready(function () {
    jQuery('h1').addClass('wiki_paragraph1');
    jQuery('h2').addClass('wiki_paragraph2');
    jQuery('h3').addClass('wiki_paragraph3');
    jQuery('h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->

<!-- END HTML -->]]></description>
			<pubDate>Sat, 09 Apr 2016 10:27:57 +0200</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Add a table into a Word document using R software and ReporteRs package]]></title>
			<link>https://www.sthda.com/english/wiki/add-a-table-into-a-word-document-using-r-software-and-reporters-package</link>
			<guid>https://www.sthda.com/english/wiki/add-a-table-into-a-word-document-using-r-software-and-reporters-package</guid>
			<description><![CDATA[<!-- START HTML -->

  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">


<div id="TOC">
<ul>
<li><a href="#add-a-simple-table">Add a simple table</a></li>
<li><a href="#add-a-formatted-table">Add a formatted table</a><ul>
<li><a href="#change-the-background-colors-of-rows-and-columns">Change the background colors of rows and columns</a></li>
<li><a href="#change-cell-background-and-text-colors">Change cell background and text colors</a></li>
<li><a href="#insert-content-into-a-table-header-and-footer-rows">Insert content into a table : header and footer rows</a></li>
</ul></li>
<li><a href="#analyze-format-and-export-a-correlation-matrix-into-a-word-document">Analyze, format and export a correlation matrix into a Word document</a></li>
<li><a href="#powerpoint">Powerpoint</a></li>
<li><a href="#infos">Infos</a></li>
</ul>
</div>

<p><br/></p>
<p>The <strong>ReporteRs</strong> package is used to create a <strong>Word document</strong> from <strong>R software</strong>. The function <em>addFlexTable()</em> can be used to add a simple or customized table into the document.</p>
<ol style="list-style-type: decimal">
<li>The first step is to create a table using one of the functions below :</li>
</ol>
<ul>
<li><em>FlexTable()</em> to create a ‘flexible’ table which can be easily formatted</li>
<li><em>vanilla.table()</em> which is shortcut to quickly produce a nice <em>FlexTable</em></li>
</ul>
<ol start="2" style="list-style-type: decimal">
<li>The second step is to add the created table into the Word document using <em>addFlexTable()</em> function as follow :</li>
</ol>
<pre class="r"><code># doc : docx object
# flextable : FlexTable object
addFlexTable(doc, flextable)</code></pre>
<p><span class="success">The aim of this <strong>R tutorial</strong> is to show you step by step, how to add simple and formatted table into a Word document.</span></p>
<p>In the following examples, we’ll add the first 5 rows of <em>iris</em> data sets into the Word document.</p>
<pre class="r"><code>data<-iris[1:5, ]
data</code></pre>
<pre><code>  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa</code></pre>
<div id="add-a-simple-table" class="section level1">
<h1>Add a simple table</h1>
<pre class="r"><code>library(ReporteRs)

doc <- docx()
data<-iris[1:5, ]

# Add a first table : Default table
doc <- addTitle(doc, "Default table")
doc <- addFlexTable( doc, FlexTable(data))

doc <- addParagraph(doc, c("", "")) # 2 line breaks

# Add a second table, theme : vanilla table
doc <- addTitle(doc, "Vanilla table")
doc <- addFlexTable( doc, vanilla.table(data))

writeDoc(doc, file = "r-reporters-word-document-add-table.docx")</code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-word-document-add-simple-table.png" alt="R software and Reporters package, add table to a Word document" /></p>


<div class="warning">
<p>An optional argument of <em>addFlextable()</em> function is <em>par.properties</em> which value can be <em>parRight()</em>, <em>parLeft()</em>, <em>parJustify()</em> for the table alignments. It can be used as follow :</p>
<pre class="r"><code>doc <- addFlexTable( doc, vanilla.table(data),
                     par.properties = parCenter())</code></pre>
</div>
<p><br/></p>
<div class="success">
<p>Note also that, row names are not shown by default when <em>FlexTable()</em> function is used to create a table. To make row names visible use the function as follow :</p>
<pre class="r"><code>doc <- addFlexTable( doc, FlexTable(data, row.names=TRUE))</code></pre>
</div>
<p><br/></p>
</div>
<div id="add-a-formatted-table" class="section level1">
<h1>Add a formatted table</h1>
<div id="change-the-background-colors-of-rows-and-columns" class="section level2">
<h2>Change the background colors of rows and columns</h2>
<p>You should know three functions to change the appearance of table rows and columns :</p>
<ul>
<li><strong>setZebraStyle()</strong> : to color odd and even rows differently; for example, <em>odd rows</em> in <em>gray</em> color and <em>even rows</em> in <em>white</em> color.</li>
<li><strong>setRowsColors()</strong> : to change color of a particular table row</li>
<li><strong>setColumnsColors</strong> : to change the color of particular table columns</li>
</ul>
<p>These functions can be used as follow :</p>
<pre class="r"><code>library(ReporteRs)

doc <- docx()

data<-iris[1:5, ]

# Zebra striped tables
doc <- addTitle(doc, "Zebra striped tables")
MyFTable <- vanilla.table(data)
MyFTable <- setZebraStyle(MyFTable, odd = &amp;#39;#eeeeee&amp;#39;, even = &amp;#39;white&amp;#39;)
doc <- addFlexTable( doc, MyFTable)

# Change columns and rows background colors
doc <- addTitle(doc, "Change columns and rows background colors")
MyFTable = FlexTable(data = data )
# i : row index; j : column index
MyFTable = setRowsColors(MyFTable, i=2:3, colors = &amp;#39;lightblue&amp;#39;)
MyFTable = setColumnsColors(MyFTable, j=3, colors = &amp;#39;pink&amp;#39; )
doc <- addFlexTable(doc, MyFTable)

writeDoc(doc, file = "r-reporters-word-document-formatted-table1.docx")</code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-word-document-formatted-table1.png" alt="R software and Reporters package, add table to a Word document" /></p>
<p><span class="warning">Note that, i and j are, respectively, the index of rows and column to change</span></p>
</div>
<div id="change-cell-background-and-text-colors" class="section level2">
<h2>Change cell background and text colors</h2>
<p>We can change the background colors of some cells according to their values using the function <strong>setFlexTableBackgroundColors()</strong>.</p>
<p>As an example, We’ll set up the background color of column 2 according to the value of the <em>Sepal.Width</em> variable (iris data sets) :</p>
<ul>
<li>Cells with Sepal.Width < 3.2 are colored in gray (“#DDDDDD”)</li>
<li>Cells with Sepal.Width > = 3.2 are colored in “orange”</li>
</ul>
<p>The text values of the table cells can be also customized as demonstrated in the example below :</p>
<pre class="r"><code>library(ReporteRs)

doc <- docx()

data<-iris[1:5, ]

# Change the background colors of column 2 according to Sepal.Width
#++++++++++++++++++++++++++++
doc <- addTitle(doc, "Change the background color of cells")
MyFTable <- FlexTable(data)
MyFTable <- setFlexTableBackgroundColors(MyFTable, j = 2,
  colors = ifelse(data$Sepal.Width < 3.2, &amp;#39;#DDDDDD&amp;#39;, &amp;#39;orange&amp;#39;))

doc <- addFlexTable( doc, MyFTable)

# Format the text of some cells (column 3:4)
#++++++++++++++++++++++++++++
doc <- addTitle(doc, "Format cell text values")
MyFTable = FlexTable(data)
MyFTable[, 3:4] = textProperties(color = &amp;#39;blue&amp;#39;)
doc <- addFlexTable( doc, MyFTable)

writeDoc(doc, file = "r-reporters-word-document-format-cells.docx")</code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-word-document-format-cells.png" alt="R software and Reporters package, add table to a Word document" /></p>
</div>
<div id="insert-content-into-a-table-header-and-footer-rows" class="section level2">
<h2>Insert content into a table : header and footer rows</h2>
<p>Additional rows, such as header and footer, can be easily added into a table as illustrated in the example hereafter. Contents can be also added in a particular cell.</p>
<p>The functions <strong>addHeaderRow()</strong> and <strong>addFooterRow()</strong> are used to add a header and a footer.</p>
<p>A simplified format of these functions are :</p>
<pre class="r"><code>addHeaderRow(x, value, colspan, text.properties)

addFooterRow(x, value, colspan, text.properties)</code></pre>
<br/>
<div class="block">
<ul>
<li><strong>x</strong> : a FlexTable object</li>
<li><strong>value</strong> : character vector to add as a header or a footer</li>
<li><strong>colspan</strong> : a numeric vector (optional) specifying the number of columns to span for each corresponding value</li>
<li><strong>text.properties</strong> : the text properties to apply to each cell (optional).</li>
</ul>
</div>
<p><br/></p>
<pre class="r"><code>library(ReporteRs)
doc <- docx()

data<-iris[1:5, ]

# Insert a content into a table
doc <- addTitle(doc, "Insert a content")
MyFTable = FlexTable(data, header.columns= FALSE )

# Add first header row
MyFTable <- addHeaderRow( MyFTable, 
            value = c(&amp;#39;Sepal&amp;#39;, &amp;#39;Petal&amp;#39;, &amp;#39;Species&amp;#39;), 
            text.properties = textBold(color="orange"), 
            colspan = c( 2, 2, 1))

# Add second header row
MyFTable = addHeaderRow(MyFTable, value = names(data),
  text.properties = textBold())
     
# Add footer row
MyFTable <- addFooterRow(MyFTable, 
              value = &amp;#39;This data is from iris data sets&amp;#39;,
              colspan = 5, 
              text.properties = textBoldItalic(color ="blue"))
# Add symbol
MyFTable[data$Petal.Length!=1.4, &amp;#39;Species&amp;#39;,
  text.properties = textBold(vertical.align = &amp;#39;superscript&amp;#39;,
                             color = "red")] = &amp;#39;(1)&amp;#39;
doc <- addFlexTable( doc, MyFTable)

writeDoc(doc, file = "r-reporters-word-document-add-header-footer.docx")</code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-word-document-add-footer.png" alt="R software and Reporters package, add table to a Word document" /></p>
</div>
</div>
<div id="analyze-format-and-export-a-correlation-matrix-into-a-word-document" class="section level1">
<h1>Analyze, format and export a correlation matrix into a Word document</h1>
<pre class="r"><code>library(ReporteRs)
doc <- docx()

data( mtcars )
cormatrix = cor(mtcars)

col =c("#B2182B", "#D6604D", "#F4A582", "#FDDBC7",
       "#D1E5F0", "#92C5DE", "#4393C3", "#2166AC")

mycut = cut(cormatrix,
        breaks = c(-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1),
        include.lowest = TRUE, label = FALSE )

color_palettes = col[mycut]

corrFT = FlexTable( round(cormatrix, 2), add.rownames = TRUE )

corrFT = setFlexTableBackgroundColors(corrFT,
        j = seq_len(ncol(cormatrix)) + 1,
        colors = color_palettes )


corrFT = setFlexTableBorders( corrFT
        , inner.vertical = borderProperties( style = "dashed", color = "white" )
        , inner.horizontal = borderProperties( style = "dashed", color = "white"  )
        , outer.vertical = borderProperties( width = 2, color = "white"  )
        , outer.horizontal = borderProperties( width = 2, color = "white"  )
)

doc <- addFlexTable( doc, corrFT)

writeDoc(doc, file = "r-reporters-word-document-correlation.docx")</code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-word-document-correlation.png" alt="R software and Reporters package, add table to a Word document" /></p>
</div>
<div id="powerpoint" class="section level1">
<h1>Powerpoint</h1>
<p>A pptx object works the same but does not require any parProperties</p>
</div>
<div id="infos" class="section level1">
<h1>Infos</h1>
<p><span class="warning"> This analysis has been performed using R (ver. 3.2.3). </span></p>
</div>

<script>jQuery(document).ready(function () {
    jQuery('h1').addClass('wiki_paragraph1');
    jQuery('h2').addClass('wiki_paragraph2');
    jQuery('h3').addClass('wiki_paragraph3');
    jQuery('h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->



<!-- END HTML -->]]></description>
			<pubDate>Sat, 09 Apr 2016 10:00:57 +0200</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Saving Data into R Data Format: RDS and RDATA]]></title>
			<link>https://www.sthda.com/english/wiki/saving-data-into-r-data-format-rds-and-rdata</link>
			<guid>https://www.sthda.com/english/wiki/saving-data-into-r-data-format-rds-and-rdata</guid>
			<description><![CDATA[<!-- START HTML -->

            
  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">

<div id="TOC">
<ul>
<li><a href="#preleminary-tasks">Preleminary tasks</a></li>
<li><a href="#save-one-object-to-a-file">Save one object to a file</a></li>
<li><a href="#save-multiple-objects-to-a-file">Save multiple objects to a file</a></li>
<li><a href="#save-your-entire-workspace">Save your entire workspace</a></li>
<li><a href="#summary">Summary</a></li>
<li><a href="#related-articles">Related articles</a></li>
<li><a href="#infos">Infos</a></li>
</ul>
</div>

<p><br/></p>
<p>In previous articles, we described the <a href="https://www.sthda.com/english/english/wiki/r-basics-quick-and-easy">essentials of R programming</a> and provided quick start guides for <a href="https://www.sthda.com/english/english/wiki/reading-data-from-txt-csv-files-r-base-functions">reading</a> and <a href="https://www.sthda.com/english/english/wiki/writing-data-from-r-to-txt-csv-files-r-base-functions">writing</a> txt and csv files using <strong>R</strong> base functions as well as using a most modern <strong>R</strong> package named <a href="https://www.sthda.com/english/english/wiki/fast-reading-of-data-from-txt-csv-files-into-r-readr-package">readr</a>, which is faster (X10) than <strong>R</strong> base functions. We also described different ways for <a href="https://www.sthda.com/english/english/wiki/reading-data-from-excel-files-xls-xlsx-into-r">reading</a> and <a href="https://www.sthda.com/english/english/wiki/writing-data-from-r-to-excel-files-xls-xlsx">writing</a> Excel files in <strong>R</strong>.</p>
<p><strong>Writing data</strong>, in txt, csv or Excel file formats, is the best solution if you want to open these files with other analysis software, such as Excel. However this solution doesn’t preserve data structures, such as column data types (numeric, character or factor). In order to do that, the data should be written out in <strong>R data format</strong>.</p>
<br/>
<div class="block">
Here, you’ll learn how to <strong>save</strong> i) a single R object, ii) multiple R objects or iii) your entire <strong>workspace</strong> in a specified file.
</div>
<p><br/></p>
<p><span class="success"><strong>Saving data</strong> into R data formats can reduce considerably the size of large files by compression.</span></p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/save-data-into-r-data-formats.png" alt="Save data into R data formats" /> <br/></p>
<div id="preleminary-tasks" class="section level1">
<h1>Preleminary tasks</h1>
<p><strong>Launch RStudio</strong> as described here: <a href="https://www.sthda.com/english/english/wiki/running-rstudio-and-setting-up-your-working-directory-easy-r-programming">Running RStudio and setting up your working directory</a></p>
</div>
<div id="save-one-object-to-a-file" class="section level1">
<h1>Save one object to a file</h1>
<p>It’s possible to use the function <strong>saveRDS</strong>() to write a single R object to a specified file (in <strong>rds</strong> file format). The object can be restored back using the function <strong>readRDS</strong>().</p>
<p><span class="success">Note that, it’s possible to restore the object under a different name </span></p>
<p>The simplified syntax for saving and restoring is as follow:</p>
<pre class="r"><code># Save an object to a file
saveRDS(object, file = "my_data.rds")

# Restore the object
readRDS(file = "my_data.rds")</code></pre>
<ul>
<li><strong>object</strong>: An R object to save</li>
<li><strong>file</strong>: the name of the file where the R object is saved to or read from</li>
</ul>
<p>In the R code below, we’ll save the mtcars data set and restore it under different name:</p>
<pre class="r"><code># Save a single object to a file
saveRDS(mtcars, "mtcars.rds")

# Restore it under a different name
my_data <- readRDS("mtcars.rds")</code></pre>
</div>
<div id="save-multiple-objects-to-a-file" class="section level1">
<h1>Save multiple objects to a file</h1>
<p>The function <strong>save</strong>() can be used to save one or more R objects to a specified file (in <strong>.RData</strong> or <strong>.rda</strong> file formats). The function can be read back from the file using the function <strong>load</strong>().</p>
<p><span class="warning">Note that if you save your data with <strong>save</strong>(), it cannot be restored under different name. The original object names are automatically used.</span></p>
<pre class="r"><code># Saving on object in RData format
save(data1, file = "data.RData")

# Save multiple objects
save(data1, data2, file = "data.RData")

# To load the data again
load("data.RData")</code></pre>
</div>
<div id="save-your-entire-workspace" class="section level1">
<h1>Save your entire workspace</h1>
<p>It’s a good idea to save your <a href="https://www.sthda.com/english/english/wiki/unning-rstudio-and-setting-up-your-working-directory-easy-r-programming#use-r-inside-rstudio"><strong>workspace</strong></a> image when your work sessions are long.</p>
<p>This can be done at any time using the function <strong>save.image</strong>()</p>
<pre class="r"><code>save.image() </code></pre>
<p>That stores your workspace to a file named <strong>.RData</strong> by default. This will ensure you don’t lose all your work in the event of system reboot, for instance.</p>
<p>When you <a href="https://www.sthda.com/english/english/wiki/running-rstudio-and-setting-up-your-working-directory-easy-r-programming#close-your-rrstudio-session">close R/RStudio</a>, it asks if you want to save your workspace. If you say yes, the next time you start R that workspace will be loaded. That saved file will be named .RData as well.</p>
<p>It’s also possible to specify the file name for saving your work space:</p>
<pre class="r"><code>save.image(file = "my_work_space.RData")</code></pre>
<p>To restore your workspace, type this:</p>
<pre class="r"><code>load("my_work_space.RData")</code></pre>
</div>
<div id="summary" class="section level1">
<h1>Summary</h1>
<br/>
<div class="block">
<ul>
<li><p>Save and restore one single R object: <strong>saveRDS</strong>(object, file), my_data <- <strong>readRDS</strong>(file)</p></li>
<li><p>Save and restore multiple R objects: <strong>save</strong>(data1, data2, file = “my_data.RData”), <strong>load</strong>(“my_data.RData”)</p></li>
<li>Save and restore your entire workspace: <strong>save.image</strong>(file = “my_work_space.RData”), <strong>load</strong>(“my_work_space.RData”)</li>
</ul>
</div>
<p><br/></p>
</div>
<div id="related-articles" class="section level1">
<h1>Related articles</h1>
<ul>
<li><a href="https://www.sthda.com/english/english/wiki/r-basics-quick-and-easy">R programming basics</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/importing-data-into-r">Importing data into R</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/writing-data-from-r-to-txt-csv-files-r-base-functions">Writing data from R to a txt|csv file: R base functions</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/fast-writing-of-data-from-r-to-txt-csv-files-readr-package">Fast Writing of Data From R to txt|csv Files: readr package</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/writing-data-from-r-to-excel-files-xls-xlsx">Writing data from R to Excel files (xls|xlsx)</a></li>
</ul>
</div>
<div id="infos" class="section level1">
<h1>Infos</h1>
<p><span class="warning"> This analysis has been performed using R (ver. 3.2.3). </span></p>
</div>

<script>jQuery(document).ready(function () {
    jQuery('h1').addClass('wiki_paragraph1');
    jQuery('h2').addClass('wiki_paragraph2');
    jQuery('h3').addClass('wiki_paragraph3');
    jQuery('h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->



<!-- END HTML -->]]></description>
			<pubDate>Sat, 09 Apr 2016 09:25:52 +0200</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Writing Data From R to Excel Files (xls|xlsx)]]></title>
			<link>https://www.sthda.com/english/wiki/writing-data-from-r-to-excel-files-xls-xlsx</link>
			<guid>https://www.sthda.com/english/wiki/writing-data-from-r-to-excel-files-xls-xlsx</guid>
			<description><![CDATA[<!-- START HTML -->

  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">


<div id="TOC">
<ul>
<li><a href="#preleminary-tasks">Preleminary tasks</a></li>
<li><a href="#writing-excel-files-using-xlsx-package">Writing Excel files using xlsx package</a><ul>
<li><a href="#installing-and-loading-xlsx-package">Installing and loading xlsx package</a></li>
<li><a href="#using-xlsx-package">Using xlsx package</a></li>
<li><a href="#read-more">Read more</a></li>
</ul></li>
<li><a href="#summary">Summary</a></li>
<li><a href="#related-articles">Related articles</a></li>
<li><a href="#infos">Infos</a></li>
</ul>
</div>

<p><br/></p>
<p>Previously, we described the <a href="https://www.sthda.com/english/english/wiki/r-basics-quick-and-easy">essentials of R programming</a> and provided quick start guides for <a href="https://www.sthda.com/english/english/wiki/reading-data-from-txt-csv-files-r-base-functions">reading</a> and <a href="https://www.sthda.com/english/english/wiki/writing-data-from-r-to-txt-csv-files-r-base-functions">writing</a> txt and csv files using <strong>R</strong> base functions as well as using a most modern R package named <a href="https://www.sthda.com/english/english/wiki/fast-reading-of-data-from-txt-csv-files-into-r-readr-package">readr</a>, which is faster (X10) than R base functions. We also described different ways for <a href="https://www.sthda.com/english/english/wiki/reading-data-from-excel-files-xls-xlsx-into-r">reading data from Excel files into R</a>.</p>
<br/>
<div class="block">
Here, you’ll learn how to <strong>export data</strong> from <strong>R</strong> to <strong>Excel files</strong> (<strong>xls</strong> or <strong>xlsx</strong> file formats). We’ll use the <strong>xlsx</strong> R package.
</div>
<p><br/></p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/export-excel-files.png" alt="Writing Data From R to Excel Files (xls|xlsx)" /> <br/></p>
<div id="preleminary-tasks" class="section level1">
<h1>Preleminary tasks</h1>
<p><strong>Launch RStudio</strong> as described here: <a href="https://www.sthda.com/english/english/wiki/running-rstudio-and-setting-up-your-working-directory-easy-r-programming">Running RStudio and setting up your working directory</a></p>
</div>
<div id="writing-excel-files-using-xlsx-package" class="section level1">
<h1>Writing Excel files using xlsx package</h1>
<p>The <strong>xlsx</strong> package, a java-based solution, is one of the powerful R packages to <strong>read</strong>, <strong>write</strong> and <strong>format</strong> <strong>Excel files</strong>.</p>
<div id="installing-and-loading-xlsx-package" class="section level2">
<h2>Installing and loading xlsx package</h2>
<ul>
<li>Install</li>
</ul>
<pre class="r"><code>install.packages("xlsx")</code></pre>
<ul>
<li>Load</li>
</ul>
<pre class="r"><code>library("xlsx")</code></pre>
</div>
<div id="using-xlsx-package" class="section level2">
<h2>Using xlsx package</h2>
<p>There are two main functions in <strong>xlsx</strong> package for writing both xls and xlsx Excel files: <strong>write.xlsx</strong>() and <strong>write.xlsx2</strong>() [faster on big files compared to write.xlsx function].</p>
<p>The simplified formats are:</p>
<pre class="r"><code>write.xlsx(x, file, sheetName = "Sheet1", 
  col.names = TRUE, row.names = TRUE, append = FALSE)

write.xlsx2(x, file, sheetName = "Sheet1",
  col.names = TRUE, row.names = TRUE, append = FALSE)</code></pre>
<br/>
<div class="block">
<ul>
<li><strong>x</strong>: a data.frame to be written into the workbook</li>
<li><strong>file</strong>: the path to the output file</li>
<li><strong>sheetName</strong>: a character string to use for the sheet name.</li>
<li><strong>col.names, row.names</strong>: a logical value specifying whether the column names/row names of x are to be written to the file</li>
<li><strong>append</strong>: a logical value indicating if x should be appended to an existing file.</li>
</ul>
</div>
<p><br/></p>
<p>Example of usage: the following R code will write the <a href="https://www.sthda.com/english/english/wiki/r-built-in-data-sets">R built-in data sets</a> - USArrests, mtcars and iris - into the same Excel file:</p>
<pre class="r"><code>library("xlsx")

# Write the first data set in a new workbook
write.xlsx(USArrests, file = "myworkbook.xlsx",
      sheetName = "USA-ARRESTS", append = FALSE)

# Add a second data set in a new worksheet
write.xlsx(mtcars, file = "myworkbook.xlsx", 
           sheetName="MTCARS", append=TRUE)

# Add a third data set
write.xlsx(iris, file = "myworkbook.xlsx",
           sheetName="IRIS", append=TRUE)</code></pre>
</div>
<div id="read-more" class="section level2">
<h2>Read more</h2>
<p>Read more about for reading, writing and formatting Excel files:</p>
<ul>
<li><a href="https://www.sthda.com/english/english/wiki/r-xlsx-package-a-quick-start-guide-to-manipulate-excel-files-in-r">R xlsx package : A quick start guide to manipulate Excel files in R</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/r2excel-read-write-and-format-easily-excel-files-using-r-software">r2excel package: Read, write and format easily Excel files using R software</a></li>
</ul>
</div>
</div>
<div id="summary" class="section level1">
<h1>Summary</h1>
<br/>
<div class="block">
Write data from R to Excel files using <strong>xlsx</strong> package: <strong>write.xlsx</strong>(my_data, file = “result.xlsx”, sheetName = “my_data”, append = FALSE).
</div>
<p><br/></p>
</div>
<div id="related-articles" class="section level1">
<h1>Related articles</h1>
<ul>
<li>Previous chapters
<ul>
<li><a href="https://www.sthda.com/english/english/wiki/r-basics-quick-and-easy">R programming basics</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/importing-data-into-r">Importing data into R</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/writing-data-from-r-to-txt-csv-files-r-base-functions">Writing data from R to a txt|csv file: R base functions</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/fast-writing-of-data-from-r-to-txt-csv-files-readr-package">Fast Writing of Data From R to txt|csv Files: readr package</a></li>
</ul></li>
<li>Next chapters
<ul>
<li><a href="https://www.sthda.com/english/english/wiki/saving-data-into-r-data-format-rds-and-rdata">Saving data into R data format: RDATA and RDS</a></li>
</ul></li>
</ul>
</div>
<div id="infos" class="section level1">
<h1>Infos</h1>
<p><span class="warning"> This analysis has been performed using R (ver. 3.2.3). </span></p>
</div>

<script>jQuery(document).ready(function () {
    jQuery('h1').addClass('wiki_paragraph1');
    jQuery('h2').addClass('wiki_paragraph2');
    jQuery('h3').addClass('wiki_paragraph3');
    jQuery('h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->


<!-- END HTML -->]]></description>
			<pubDate>Sat, 09 Apr 2016 08:27:41 +0200</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Fast Writing of Data From R to txt|csv Files: readr package]]></title>
			<link>https://www.sthda.com/english/wiki/fast-writing-of-data-from-r-to-txt-csv-files-readr-package</link>
			<guid>https://www.sthda.com/english/wiki/fast-writing-of-data-from-r-to-txt-csv-files-readr-package</guid>
			<description><![CDATA[<!-- START HTML -->

  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">

<div id="TOC">
<ul>
<li><a href="#preleminary-tasks">Preleminary tasks</a></li>
<li><a href="#installing-and-loading-readr">Installing and loading readr</a></li>
<li><a href="#readr-functions-for-writing-data">readr functions for writing data</a></li>
<li><a href="#writing-data-to-a-file">Writing data to a file</a></li>
<li><a href="#summary">Summary</a></li>
<li><a href="#related-articles">Related articles</a></li>
<li><a href="#infos">Infos</a></li>
</ul>
</div>

<p><br/></p>
<p>There are many solutions for <strong>writing</strong> data from <strong>R</strong> to <strong>txt</strong> (i.e., tsv: tab-separated values) or <strong>csv</strong> (comma-separated values) files. In our previous articles, we described <a href="https://www.sthda.com/english/english/wiki/writing-data-from-r-to-txt-csv-files-r-base-functions">R base functions</a> (write.table() and write.csv()) for writing data from R to txt|csv files R.</p>
<br/>
<div class="block">
<p>In this article, we’ll describe a most modern R package <strong>readr</strong>, developed by Hadley Wickham, for fast reading and writing delimited files. It contains the function <strong>write_delim</strong>(), <strong>write_csv</strong>() and <strong>write_tsv</strong>() to export easily a data from R.</p>
<p>Compared to R base functions (write.csv() and write.table()), <strong>readr</strong> functions:</p>
<ol style="list-style-type: decimal">
<li>are much <strong>faster</strong> (X2),</li>
<li>never write row names.</li>
</ol>
</div>
<p><br/></p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/export-txt-csv-from-r-readr-package.png" alt="Fast Writing of Data From R to txt|csv Files: readr package" /> <br/></p>
<div id="preleminary-tasks" class="section level1">
<h1>Preleminary tasks</h1>
<p><strong>Launch RStudio</strong> as described here: <a href="https://www.sthda.com/english/english/wiki/running-rstudio-and-setting-up-your-working-directory-easy-r-programming">Running RStudio and setting up your working directory</a></p>
</div>
<div id="installing-and-loading-readr" class="section level1">
<h1>Installing and loading readr</h1>
<pre class="r"><code># Installing
install.packages("readr")

# Loading
library("readr")</code></pre>
</div>
<div id="readr-functions-for-writing-data" class="section level1">
<h1>readr functions for writing data</h1>
<p>The function <strong>rwrite_delim</strong>()[in <strong>readr</strong> package] is a general function to export a data table from R. Depending on the format of your file, you can also use:</p>
<br/>
<div class="block">
<ul>
<li><strong>write_csv</strong>(): to write a comma (“,”) separated values</li>
<li><strong>write_tsv</strong>(): to write a tab separated (“\t”) values</li>
</ul>
</div>
<p><br/></p>
<p>The simplified format of these functions are, as follow:</p>
<pre class="r"><code># General function
write_delim(x, path, delim = " ")

# Write comma (",") separated value files
write_csv(file, path)

# Write tab ("\t") separated value files
write_tsv(file, path)</code></pre>
<br/>
<div class="block">
<ul>
<li><strong>x</strong>: a data frame to be written</li>
<li><strong>path</strong>: path to the result file</li>
<li><strong>delim</strong>: Delimiter used to separate values. Must be single character.</li>
</ul>
</div>
<p><br/></p>
</div>
<div id="writing-data-to-a-file" class="section level1">
<h1>Writing data to a file</h1>
<p>The R code below exports the <a href="https://www.sthda.com/english/english/wiki/r-built-in-data-sets">built-in R</a> <em>mtcars</em> data set to a tab-separated ( <em>sep = “\t”</em>) file called mtcars.txt in the current working directory:</p>
<pre class="r"><code># Loading mtcars data
data("mtcars")


library("readr")
# Writing mtcars data to a tsv file
write_tsv(mtcars, path = "mtcars.txt")

# Writing mtcars data to a csv file
write_csv(mtcars, path = "mtcars.csv")</code></pre>
</div>
<div id="summary" class="section level1">
<h1>Summary</h1>
<br/>
<div class="block">
<ul>
<li><p>Write data from R to a txt (i.e., tsv) file: <strong>write_tsv</strong>(my_data, path = “my_data.txt”)</p></li>
<li>Write data from R to a csv file: <strong>write_csv</strong>(my_data, path = “my_data.csv”)</li>
</ul>
</div>
<p><br/></p>
</div>
<div id="related-articles" class="section level1">
<h1>Related articles</h1>
<ul>
<li>Previous chapters
<ul>
<li><a href="https://www.sthda.com/english/english/wiki/r-basics-quick-and-easy">R programming basics</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/importing-data-into-r">Importing data into R</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/writing-data-from-r-to-txt-csv-files-r-base-functions">Writing data from R to a txt|csv file: R base functions</a></li>
</ul></li>
<li>Next chapters
<ul>
<li><a href="https://www.sthda.com/english/english/wiki/writing-data-from-r-to-excel-files-xls-xlsx">Writing data from R to Excel files (xls|xlsx)</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/saving-data-into-r-data-format-rds-and-rdata">Saving data into R data format: RDATA and RDS</a></li>
</ul></li>
</ul>
</div>
<div id="infos" class="section level1">
<h1>Infos</h1>
<p><span class="warning"> This analysis has been performed using R (ver. 3.2.3). </span></p>
</div>

<script>jQuery(document).ready(function () {
    jQuery('h1').addClass('wiki_paragraph1');
    jQuery('h2').addClass('wiki_paragraph2');
    jQuery('h3').addClass('wiki_paragraph3');
    jQuery('h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->



<!-- END HTML -->]]></description>
			<pubDate>Sat, 09 Apr 2016 08:18:39 +0200</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Writing Data From R to txt|csv Files: R Base Functions]]></title>
			<link>https://www.sthda.com/english/wiki/writing-data-from-r-to-txt-csv-files-r-base-functions</link>
			<guid>https://www.sthda.com/english/wiki/writing-data-from-r-to-txt-csv-files-r-base-functions</guid>
			<description><![CDATA[<!-- START HTML -->

  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">

<div id="TOC">
<ul>
<li><a href="#preleminary-tasks">Preleminary tasks</a></li>
<li><a href="#r-base-functions-for-writing-data">R base functions for writing data</a></li>
<li><a href="#writing-data-to-a-file">Writing data to a file</a></li>
<li><a href="#summary">Summary</a></li>
<li><a href="#related-articles">Related articles</a></li>
<li><a href="#infos">Infos</a></li>
</ul>
</div>

<p><br/> Previously, we described <a href="https://www.sthda.com/english/english/wiki/reading-data-from-txt-csv-files-r-base-functions">R base functions</a> (read.delim() and read.csv()) for importing txt and csv files into R.</p>
<br/>
<div class="block">
In this article, you’ll learn how to <strong>export</strong> or <strong>write</strong> data from <strong>R</strong> to <strong>.txt</strong> (tab-separated values) and <strong>.csv</strong> (comma-separated values) file formats.
</div>
<p><br/></p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/export-txt-csv-from-r-base-functions.png" alt="Writing Data From R to txt|csv Files: R Base Functions" /> <br/></p>
<div id="preleminary-tasks" class="section level1">
<h1>Preleminary tasks</h1>
<p><strong>Launch RStudio</strong> as described here: <a href="https://www.sthda.com/english/english/wiki/running-rstudio-and-setting-up-your-working-directory-easy-r-programming">Running RStudio and setting up your working directory</a></p>
</div>
<div id="r-base-functions-for-writing-data" class="section level1">
<h1>R base functions for writing data</h1>
<p>The R base function <strong>write.table()</strong> can be used to export a <a href="https://www.sthda.com/english/english/wiki/easy-r-programming-basics">data frame or a matrix</a> to a file.</p>
<p>A simplified format is as follow:</p>
<pre class="r"><code>write.table(x, file, append = FALSE, sep = " ", dec = ".",
            row.names = TRUE, col.names = TRUE)</code></pre>
<div class="block">
<ul>
<li><strong>x</strong>: a <a href="https://www.sthda.com/english/wiki/(easy-r-programming-basics)">matrix or a data frame</a> to be written.</li>
<li><strong>file</strong>: a character specifying the name of the result file.</li>
<li><strong>sep</strong>: the field separator string, e.g., sep = “\t” (for tab-separated value).</li>
<li><strong>dec</strong>: the string to be used as decimal separator. Default is “.”</li>
<li><strong>row.names</strong>: either a logical value indicating whether the row names of x are to be written along with x, or a character vector of row names to be written.</li>
<li><strong>col.names</strong>: either a logical value indicating whether the column names of x are to be written along with x, or a character vector of column names to be written. If col.names = NA and row.names = TRUE a blank column name is added, which is the convention used for CSV files to be read by spreadsheets.</li>
</ul>
</div>
<p><span class="success">It’s also possible to write <strong>csv</strong> files using the functions <strong>write.csv()</strong> and <strong>write.csv2()</strong>.</span></p>
<ul>
<li><strong>write.csv()</strong> uses “.” for the decimal point and a comma (“,”) for the separator.</li>
<li><strong>write.csv2()</strong> uses a comma (“,”) for the decimal point and a semicolon (“;”) for the separator.</li>
</ul>
<p>The syntax is as follow:</p>
<pre class="r"><code>write.csv(my_data, file = "my_data.csv")

write.csv2(my_data, file = "my_data.csv")</code></pre>
</div>
<div id="writing-data-to-a-file" class="section level1">
<h1>Writing data to a file</h1>
<p>The R code below exports the <a href="https://www.sthda.com/english/english/wiki/r-built-in-data-sets">built-in R</a> <em>mtcars</em> data set to a tab-separated ( <em>sep = “\t”</em>) file called mtcars.txt in the current working directory:</p>
<pre class="r"><code># Loading mtcars data
data("mtcars")

# Writing mtcars data
write.table(mtcars, file = "mtcars.txt", sep = "\t",
            row.names = TRUE, col.names = NA)</code></pre>
<p>If you don’t want to write row names, use row.names = FALSE as follow:</p>
<pre class="r"><code>write.table(mtcars, file = "mtcars.txt", sep = "\t",
            row.names = FALSE)</code></pre>
</div>
<div id="summary" class="section level1">
<h1>Summary</h1>
<br/>
<div class="block">
<ul>
<li><p>Write data from R to a txt file: <strong>write.table</strong>(my_data, file = “my_data.txt”, sep = “”)</p></li>
<li>Write data from R to a csv file: <strong>write.csv</strong>(my_data, file = “my_data.csv”)</li>
</ul>
</div>
<p><br/></p>
</div>
<div id="related-articles" class="section level1">
<h1>Related articles</h1>
<ul>
<li>Previous chapters
<ul>
<li><a href="https://www.sthda.com/english/english/wiki/r-basics-quick-and-easy">R programming basics</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/importing-data-into-r">Importing data into R</a></li>
</ul></li>
<li>Next chapters
<ul>
<li><a href="https://www.sthda.com/english/english/wiki/fast-writing-of-data-from-r-to-txt-csv-files-readr-package">Fast Writing of Data From R to txt|csv Files: readr package</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/writing-data-from-r-to-excel-files-xls-xlsx">Writing data from R to Excel files (xls|xlsx)</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/saving-data-into-r-data-format-rds-and-rdata">Saving data into R data format: RDATA and RDS</a></li>
</ul></li>
</ul>
</div>
<div id="infos" class="section level1">
<h1>Infos</h1>
<p><span class="warning"> This analysis has been performed using R (ver. 3.2.3). </span></p>
</div>

<script>jQuery(document).ready(function () {
    jQuery('h1').addClass('wiki_paragraph1');
    jQuery('h2').addClass('wiki_paragraph2');
    jQuery('h3').addClass('wiki_paragraph3');
    jQuery('h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->


<!-- END HTML -->]]></description>
			<pubDate>Sat, 09 Apr 2016 08:11:48 +0200</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Create a Word document from a template file using R software and ReporteRs package]]></title>
			<link>https://www.sthda.com/english/wiki/create-a-word-document-from-a-template-file-using-r-software-and-reporters-package</link>
			<guid>https://www.sthda.com/english/wiki/create-a-word-document-from-a-template-file-using-r-software-and-reporters-package</guid>
			<description><![CDATA[<!-- START HTML -->

  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">

<div id="TOC">
<ul>
<li><a href="#quick-introduction-to-reporters-package">Quick introduction to ReporteRs package</a></li>
<li><a href="#create-a-word-document-using-a-template-file">Create a Word document using a template file</a></li>
<li><a href="#infos">Infos</a></li>
</ul>
</div>

<p><br/></p>
<p>This <strong>tutorial</strong> describes how to create a <strong>Word document</strong> based on an existing one using <strong>R software</strong> and <strong>Reporters</strong> package. In this case, your final Word document is generated using the layout and the styles from the template file.</p>
<p>This approach is useful in many situations :</p>
<ul>
<li>If you work in a corporate environment, you may need sometimes to generate Word documents based on a template with specific fonts, color, logos, etc.</li>
<li>If you want to insert R outputs in an existing Word document.</li>
<li>If you want to use text formatting styles from a given template file.</li>
</ul>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-word-document-template-image.png" alt="Read and write a Word document from a template using R software and ReporteRs package" /></p>
<p>Before reading this article you should take a look at my first post : <a href="https://www.sthda.com/english/english/wiki/create-and-format-word-documents-using-r-software-and-reporters-package">Create and format Word documents using R software and Reporters package</a>.</p>
<div id="quick-introduction-to-reporters-package" class="section level1">
<h1>Quick introduction to ReporteRs package</h1>
<p><strong>ReporteRs</strong> package provides simple functions to quickly generate and format a word document from <strong>R software</strong>. It can be used as follow :</p>
<pre class="r"><code>#install.packages("ReporteRs")
library("ReporteRs")

# Create a Word document
doc <- docx()

# Add a title
doc <- addTitle(doc, "Example of a Word document from R software", level=1)

# Add paragraph
doc <- addParagraph(doc, "This Word document has been generated from R software using ReporteRs package.")

# Add plots
doc <- addTitle(doc, "Plots", level=1)
doc <- addPlot(doc, function() hist(iris$Sepal.Width, col=4) )

doc <- addPageBreak(doc) # go to the next page

# Add table
doc <- addTitle(doc, "Table", level=1)
doc <- addFlexTable(doc, vanilla.table(iris[1:10,]))

# Write the word document to a file
writeDoc(doc, file="r-reporters-word-example.docx")</code></pre>
<p><span class="success">The Word document created by the <strong>R</strong> code above is available here : <a href="https://www.sthda.com/english/sthda/RDoc/example-files/r-reporters-word-example.docx">R software and ReporteRs package - Example of a Word document</a></span></p>
<iframe src="http://www.slideshare.net/slideshow/embed_code/43175997" width="570" height="510" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;" allowfullscreen> </iframe>
     


</div>
<div id="create-a-word-document-using-a-template-file" class="section level1">
<h1>Create a Word document using a template file</h1>
<p>A template file can be specified to the <strong>docx()</strong> function as follow :</p>
<pre class="r"><code># Create a word document
doc <- docx(template="path/to/your/word/template/file.docx")

# ...............
# Add contents
# ...............

# Write the Word document to a file 
writeDoc(doc, file = "output-file.docx")</code></pre>
<p>In the R code below, a Word document template is downloaded from <strong>STHDA</strong> website and used to write a report :</p>
<pre class="r"><code># Download a Word document template from STHDA website
download.file(url="https://www.sthda.com/sthda/RDoc/example-files/r-reporters-word-document-template.docx",
    destfile="r-reporters-word-document-template.docx", quiet=TRUE)

# Create a Word document using the downloaded template
doc <- docx(title="R software and ReporteRs package",
            template="r-reporters-word-document-template.docx")

# Add titles
doc <- addTitle(doc, "Word document created from a template",
                level=1) 

# Add an introduction
doc <- addTitle(doc, "Introduction", level=2) # Add a sub title
doc <- addParagraph(doc, "This Word document is created from a template using R software and ReporteRs package.")

# Add  a table
doc <- addTitle(doc, "Iris data sets", level=2)
doc <- addFlexTable(doc, FlexTable(iris[1:10,]))
doc <- addTitle(doc, "Description of iris data sets", level=2)
doc <- addParagraph(doc, "iris data set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are Iris setosa, versicolor, and virginica.")

# Add a page break : go to next page
doc <- addPageBreak(doc)

# Add a plot into the Word document
doc <- addTitle(doc, "Nice bar plot")
doc <- addPlot(doc, function() barplot(1:5, col=1:5))

# Write the Word document to a file 
writeDoc(doc, file = "r-reporters-word-document-from-template.docx")

# Remove the downloaded template file
ok <- file.remove("r-reporters-word-document-template.docx")</code></pre>
<p><span class="success">The Word document created by the <strong>R</strong> code above is available here : <a href="https://www.sthda.com/english/sthda/RDoc/example-files/r-reporters-word-document-from-template.docx">R software and ReporteRs package - Word document created from a template</a></span></p>
<iframe src="http://www.slideshare.net/slideshow/embed_code/43008094" width="570" height="510" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;" allowfullscreen> </iframe>
    

<p><span class="warning">Note that, the function <strong>docx()</strong> can take two arguments : a <em>title</em> argument (title of the document, appearing only in the Word document properties) and a <em>template</em> argument (to specify template file).</span></p>
</div>
<div id="infos" class="section level1">
<h1>Infos</h1>
<p><span class="warning"> This analysis has been performed using R (ver. 3.1.0). </span></p>
<p>You can read more about ReporteRs and download the source code at the following link :</p>
<p>GitHub (David Gohel): <a href="https://github.com/davidgohel/ReporteRs" rel="nofollow">ReporteRs</a></p>
</div>

<script>jQuery(document).ready(function () {
    jQuery('h1').addClass('wiki_paragraph1');
    jQuery('h2').addClass('wiki_paragraph2');
    jQuery('h3').addClass('wiki_paragraph3');
    jQuery('h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->

<!-- END HTML -->]]></description>
			<pubDate>Sun, 04 Jan 2015 01:02:07 +0100</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Create and format Word documents using R software and Reporters package]]></title>
			<link>https://www.sthda.com/english/wiki/create-and-format-word-documents-using-r-software-and-reporters-package</link>
			<guid>https://www.sthda.com/english/wiki/create-and-format-word-documents-using-r-software-and-reporters-package</guid>
			<description><![CDATA[<!-- START HTML -->


            
  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">

<div id="TOC">
<ul>
<li><a href="#install-and-load-the-reporters-r-package">Install and load the ReporteRs R package</a></li>
<li><a href="#create-a-simple-word-document">Create a simple Word document</a><ul>
<li><a href="#add-texts-title-and-paragraphs-of-texts">Add texts : title and paragraphs of texts</a></li>
<li><a href="#format-the-text-of-a-word-document-using-r-software">Format the text of a Word document using R software</a><ul>
<li><a href="#text-properties-font-color-size">Text properties : font, color, size</a></li>
<li><a href="#change-the-global-font-size-and-font-family">Change the global font size and font family</a></li>
<li><a href="#change-the-appearance-of-a-piece-of-text-pot">Change the appearance of a “Piece Of Text” (pot)</a></li>
</ul></li>
<li><a href="#add-plots-and-images">Add plots and images</a></li>
<li><a href="#add-a-table">Add a table</a><ul>
<li><a href="#add-a-simple-table">Add a simple table</a></li>
<li><a href="#add-a-zebra-striped-table">Add a zebra striped table</a></li>
</ul></li>
<li><a href="#add-lists-ordered-and-unordered-lists">Add lists : ordered and unordered lists</a><ul>
<li><a href="#add-simple-lists">Add simple lists</a></li>
<li><a href="#add-multi-level-lists">Add multi-level lists</a></li>
</ul></li>
<li><a href="#add-a-footnote-to-a-word-document">Add a footnote to a Word document</a></li>
<li><a href="#add-r-scripts">Add R scripts</a></li>
</ul></li>
<li><a href="#add-a-table-of-contents-into-a-word-document">Add a table of contents into a Word document</a></li>
<li><a href="#infos">Infos</a></li>
</ul>
</div>

<p><br/></p>
<p><strong>Word document</strong> is one of the file formats widely used by many people, including researchers, for editing their text and tracking changes between different authors.</p>
<p>There are different solutions to generate a Word document from <strong>R software</strong> :</p>
<ul>
<li><strong>R2wd</strong> package : unfortunately it depends on statconnDCOM or RDCOMClient servers, and works on Windows only.</li>
<li><a href="http://yihui.name/knitr/demo/pandoc/" rel="nofollow"><strong>knitr + rmarkdown + pandoc</strong></a> : this is an easy way to quickly create a Word report but the output is very basic.
</li>
<li><strong>ReporteRs</strong> package, by David Gohel, provides easy to use functions to write and format Word documents. It can be also used to generate Word document from a template file with logos, fonts, etc. <strong>ReporteRs</strong> is Java-based solution, so it works on Windows, Linux and Mac OS systems.</li>
</ul>
<p>The aim of this <strong>R tutorial</strong> is to show you how to easily and quickly export outputs (including data table, plots, paragraphs of text and R scripts) from <strong>R statistical software</strong> to a Microsoft <strong>Word document</strong> (<strong>.docx</strong> file format) using <strong>ReporteRs</strong> package.</p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-word-document-image.png" alt="Read and write a Word document using R software and ReporteRs package" /></p>
<div id="install-and-load-the-reporters-r-package" class="section level1">
<h1>Install and load the ReporteRs R package</h1>
<p><strong>ReporteRs</strong> can be installed and loaded as follow :</p>
<pre class="r"><code>install.packages(&amp;#39;ReporteRs&amp;#39;) # Install
library(&amp;#39;ReporteRs&amp;#39;) # Load</code></pre>
<p><span class="warning">Note that ReporteRs requires Java (>= 1.6) ; make sure you have an installed JRE</span></p>
<p>The version of Java installed on your computer, can be checked with this <strong>R code</strong> :</p>
<pre class="r"><code>system("java -version")</code></pre>
</div>
<div id="create-a-simple-word-document" class="section level1">
<h1>Create a simple Word document</h1>
<p><strong>Word document</strong> can be created using the <strong>docx()</strong> function. Then, contents can be added to the document using the functions below:</p>
<ul>
<li><strong>addTitle</strong>: Add a title</li>
<li><strong>addParagraph</strong>: Add paragraphs of text</li>
<li><strong>addFlexTable</strong>: Add a table</li>
<li><strong>addPlot</strong>: Add a plot generated in R</li>
<li><strong>addImage</strong>: Add external images</li>
<li><strong>addTOC</strong>: Add a table of contents</li>
<li><strong>addRScript</strong>: highlight and add R code</li>
<li><strong>addMarkdown</strong>: Add markdown</li>
<li><strong>addPageBreak</strong>: Add a page break</li>
</ul>
<p>After adding contents, you have to use the <strong>writeDoc()</strong> function to write the Word document into a .docx file.</p>
<div id="add-texts-title-and-paragraphs-of-texts" class="section level2">
<h2>Add texts : title and paragraphs of texts</h2>
<p>Texts can be added as a <em>title</em> or a <em>paragraph</em> using the <strong>R</strong> functions <strong>addTitle()</strong> or <strong>addParagraph()</strong>. A simplified format of these functions are :</p>
<pre class="r"><code>addTitle(doc, value, level=1) # Add titles
addParagraph(doc, value) # Add a paragraph of texts</code></pre>
<br/>
<div class="block">
<ul>
<li><strong>doc</strong> : docx object.</li>
<li><strong>value</strong> : the text to add as a title or a paragraph.</li>
<li><strong>level</strong> : an integer specifying the heading level. e.g : 1 for title1, 2 for title2, 3 for title3, etc.</li>
</ul>
</div>
<p><br/></p>
<p><em>addTitle()</em> and <em>addParagraph()</em> functions can be used as follow :</p>
<pre class="r"><code>library(ReporteRs)

# Create a word document to contain R outputs
doc <- docx()

# Add a title to the document
doc <- addTitle(doc, "Simple Word document", level=1)

# Add a paragraph of text into the Word document 
doc <- addParagraph(doc, "This simple Word document is created using R software and ReporteRs package. It contains just a simple text.")

# Add a sub title
doc <- addTitle(doc, "What is R language?", level = 2) 
doc <- addParagraph(doc, "R is a language and environment for statistical computing and graphics. R provides a wide variety of statistical and graphical techniques, and is highly extensible.")

doc <- addTitle(doc, "What is ReporteRs", level = 2)
doc <- addParagraph(doc, "ReporteRs is a package to write and format easily a Word document from R software.")

# Write the Word document to a file 
writeDoc(doc, file = "r-reporters-simple-word-document.docx")</code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-simple-word-document.png" alt="Read and write a Word document using R software and ReporteRs package" /></p>
<p><br/></p>
</div>
<div id="format-the-text-of-a-word-document-using-r-software" class="section level2">
<h2>Format the text of a Word document using R software</h2>
<div id="text-properties-font-color-size" class="section level3">
<h3>Text properties : font, color, size</h3>
<p>Among the text properties available in a Word document, there are :</p>
<p><strong>font family</strong> (e.g : “Arial”), <strong>font size</strong> (e.g : 11), <strong>font weight</strong> (e.g: bold), <strong>font style</strong> (e.g : italic), <strong>underlined</strong> text, <strong>vertical align</strong> (superscript, subscript) and <strong>color</strong> (e.g: “blue”).</p>
<p>These properties are shown in the figure below :</p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-word-document-format-text.png" alt="Read and write a Word document using R software and ReporteRs package" />.</p>
<p><span class="success">This section describes how to format and write a Word document using <strong>R software</strong></span></p>
</div>
<div id="change-the-global-font-size-and-font-family" class="section level3">
<h3>Change the global font size and font family</h3>
<p>The default <strong>font size</strong> and <strong>font family</strong> can be changed as follow :</p>
<pre class="r"><code>options( "ReporteRs-fontsize" = 12, 
         "ReporteRs-default-font" = "Arial")</code></pre>
<p><span class="warning">Note that, this changing will be applied to the whole content of the Word document. An example is shown in the next section.</span></p>
</div>
<div id="change-the-appearance-of-a-piece-of-text-pot" class="section level3">
<h3>Change the appearance of a “Piece Of Text” (pot)</h3>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-word-document-text-appearance.png" alt="Read and write a Word document using R software and ReporteRs package, format the text" />.</p>
<p><br/> The function <strong>pot()</strong> [Pieces Of Text] is used to format text chunks before adding into the document. <em>pot()</em> function can be also used to create a <strong>hyperlink</strong> and a <strong>footnote</strong>. A simplified format is :</p>
<pre class="r"><code>pot(value="", format = textProperties())</code></pre>
<br/>
<div class="block">
<ul>
<li><strong>value</strong> : the text to be formatted</li>
<li><strong>format</strong> : the properties to use for formatting the text</li>
</ul>
</div>
<p><br/></p>
<p>The possible values for the parameter <code>format</code> are the following functions :</p>
<ul>
<li><em>textProperties()</em> : the text formatting properties</li>
<li><em>textBold()</em> : shortcut for <em>bold</em> text</li>
<li><em>textItalic()</em> : shortcut for <em>italic</em> text</li>
<li><em>textBoldItalic()</em> : shortcut for <em>bold-italic</em> text</li>
<li><em>textNormal()</em> : shortcut for <em>normal</em> text</li>
</ul>
<p>The arguments for these functions are :</p>
<br/>
<div class="block">
<ul>
<li>color : font color; e.g : color=“#000000” or color = “black”.</li>
<li>font.size : a integer indicating the <em>font size</em>.</li>
<li>font.weight : the <em>font weight</em>. Possible values are “normal” or “bold”.</li>
<li>font.style : the <em>font style</em>. Possible values are “normal” or “italic”.</li>
<li>underlined : a logical value specifying if the text should be <em>underlined</em>.</li>
<li>font.family : the <em>font family</em>; e.g : “Arial”.</li>
<li>vertical.align : a character indicating font <em>vertical alignments</em>. Expected values are “baseline”" or “subscript” or “superscript”. Default value is <em>baseline</em>.</li>
<li>shading.color : background color of the text (e.g “#000000” or “black”)</li>
</ul>
</div>
<p><br/></p>
<p><strong>pot()</strong> function can be used as follow :</p>
<pre class="r"><code># Formatted text
my_text <- &amp;#39;This is a &amp;#39; + 
         pot(&amp;#39;formatted text&amp;#39;, textProperties(color="blue")) + 
        &amp;#39; created using&amp;#39; + pot(&amp;#39;ReporteRs&amp;#39;, textBold()) + &amp;#39;package&amp;#39;

# Create a hyperlynk
my_link <- pot( &amp;#39;Click here to visit STHDA website!&amp;#39;, 
          hyperlink = &amp;#39;https://www.sthda.com/english&amp;#39;,
          format=textBoldItalic(color = &amp;#39;#428BCA&amp;#39;, underline = TRUE ))</code></pre>
<p>The <strong>R</strong> code below creates a Word document containing a formatted text and a hyperlink :</p>
<pre class="r"><code>doc <- docx() 

# Change the default font size and font family
options(&amp;#39;ReporteRs-fontsize&amp;#39;=12, &amp;#39;ReporteRs-default-font&amp;#39;=&amp;#39;Arial&amp;#39;)

# Add a formatted paragraph of texts
#++++++++++++++++++++++++++++++
doc <- addTitle(doc, "Formatted text", level=1)
# Define a style to highlight a text
highlight_style <- textProperties(color=&amp;#39;#1163A5&amp;#39;,font.size = 20,
                font.weight = &amp;#39;bold&amp;#39;, font.family = &amp;#39;Courier New&amp;#39; )

my_text = &amp;#39;This &amp;#39; + pot(&amp;#39;Word document&amp;#39;, highlight_style) +
  &amp;#39; is created using&amp;#39; +
  pot(&amp;#39; R software&amp;#39;, textProperties(color="red", font.size=18)) +
  &amp;#39; and&amp;#39;+
  pot(&amp;#39; ReporteRs&amp;#39;, textBoldItalic(color="#F0A91B", underlined=TRUE)) + &amp;#39; package.&amp;#39;
doc <- addParagraph(doc, my_text)

# Add a hyperlink
doc <- addTitle(doc, "STHDA Web site", level=1)
my_link <- pot(&amp;#39;Click here to visit STHDA web site!&amp;#39;, 
          hyperlink = &amp;#39;https://www.sthda.com/english&amp;#39;,
          format=textBoldItalic(color = &amp;#39;blue&amp;#39;, underline = TRUE ))
doc <- addParagraph(doc, my_link)

# Write the Word document to a file 
writeDoc(doc, file = "r-reporters-formatted-word-document.docx")</code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-word-document-formatted.png" alt="R software and Reporters package, formatted Word document" /></p>
</div>
</div>
<div id="add-plots-and-images" class="section level2">
<h2>Add plots and images</h2>
<p>The functions <strong>addPlot()</strong> and <strong>addImage()</strong> can be used for adding a plot or an external image to the document. <em>addPlot()</em> works with all R plots (base graphics, lattice, ggplot2 and grid).</p>
<p>The format of these 2 functions are :</p>
<pre class="r"><code># Add plots
# fun : R plotting function
# ... : other arguments to pass to the plotting function
addPlot(doc, fun, ...)

# Add images
# filename : path to the external image
addImage(doc, filename)</code></pre>
<p>The R code below creates a Word document containing an introduction, a box plot, a histogram and an image (downloaded from STHDA web site).</p>
<pre class="r"><code>doc <- docx() # Create a Word document

# Add a title
doc <- addTitle(doc, "Word document with plots and Images", 
                level = 1)

# Add an introduction
doc <- addTitle(doc, "Introduction", level = 2)
doc = addParagraph(doc, value ="This Word document is created using R software and ReporteRs package. The goal of this section is to show you how to add plots and images into a Word document. This can be done easily using the functions addPlot() and addImages().")

# Add a box plot
doc <- addTitle(doc, "Box plot using R software", level = 2)
boxplotFunc<-function(){
  boxplot(len ~ dose, data = ToothGrowth,
      col = rainbow(3), main = "Guinea Pigs&amp;#39; Tooth Growth",
      xlab = "Vitamin C dose mg", ylab = "tooth length")
}
doc <- addPlot(doc, boxplotFunc)
 
doc <- addPageBreak(doc) # Go to the next page

# Add a histogram
doc <- addTitle(doc, "Histogram plot", level = 2)
doc <- addPlot(doc, function() hist(iris$Sepal.Width, col="lightblue"))

# Change point size of plotted text (in pixels, default = 12)
doc <- addTitle(doc, "Histogram with pointsize = 18", level = 2)
doc <- addPlot(doc, function() hist(iris$Sepal.Width,col="lightblue"), pointsize=18)

# Add an image
# +++++++++++++++++++++++++++
# download an image from STHDA web site
download.file(url="https://www.sthda.com/sthda/RDoc/figure/easy-ggplot2/ggplot2-histogram-demo.png",
              destfile="ggplot2-histogram-demo.png", quiet=TRUE)

doc <- addTitle(doc, "Image from STHDA web site", level = 2)
doc <- addImage(doc, "ggplot2-histogram-demo.png")

# Write the Word document to a file 
writeDoc(doc, file = "r-reporters-word-document-with-plot.docx")</code></pre>
<p><span class="success">The Word document created by the <strong>R</strong> code above is available here : <a href="https://www.sthda.com/english/sthda/RDoc/example-files/r-reporters-word-document-with-plot.docx">R software and ReporteRs package - Word document containing plots</a></span></p>
<iframe src="http://www.slideshare.net/slideshow/embed_code/43041495" width="570" height="510" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;" allowfullscreen> </iframe>
    



<div class="block">
<ol style="list-style-type: decimal">
<li><p>Note that, for <strong>addPlot()</strong> function, you can use the arguments <em>width</em> and <em>height</em> to specify the plot width and height in inches (default values are 6 X 6)</p></li>
<li><p>Another optional argument of <em>addPlot()</em> function is <em>par.properties</em> which value can be <em>parRight()</em>, <em>parLeft()</em>, <em>parJustify()</em> for the plot alignments:</p></li>
</ol>
<pre class="r"><code>doc <- addPlot( doc = doc, fun = function(){
    barplot( 1:5, par.properties = parCenter() )} 
  )</code></pre>
<ol start="3" style="list-style-type: decimal">
<li>For <em>addImage()</em> function, the allowed file formats are PNG, WMF, JPEG and GIF images..</li>
</ol>
</div>
<p><br/></p>
</div>
<div id="add-a-table" class="section level2">
<h2>Add a table</h2>
<p>The function <strong>addFlexTable()</strong> is used to add a simple or customized table.</p>
<ol style="list-style-type: decimal">
<li>The first step is to create a table using one of the functions below :</li>
</ol>
<ul>
<li><em>FlexTable()</em> to create a ‘flexible’ table which can be easily formatted</li>
<li><em>vanilla.table()</em> which is shortcut to quickly produce a nice <em>FlexTable</em></li>
</ul>
<ol start="2" style="list-style-type: decimal">
<li>The second step is to add the created table into the Word document using <em>addFlexTable()</em> function as follow :</li>
</ol>
<pre class="r"><code># doc : docx object
# flextable : FlexTable object
addFlexTable(doc, flextable)</code></pre>
<div id="add-a-simple-table" class="section level3">
<h3>Add a simple table</h3>
<pre class="r"><code>doc <- docx()
data<-iris[1:5, ]

# Add a first table : Default table
doc <- addTitle(doc, "Default table")
doc <- addFlexTable( doc, FlexTable(data))

doc <- addParagraph(doc, c("", "")) # 2 line breaks

# Add a second table, theme : vanilla table
doc <- addTitle(doc, "Vanilla table")
doc <- addFlexTable( doc, vanilla.table(data))

writeDoc(doc, file = "r-reporters-word-document-add-table.docx")</code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-word-document-add-simple-table.png" alt="R software and Reporters package, add table to a Word document" /></p>
</div>
<div id="add-a-zebra-striped-table" class="section level3">
<h3>Add a zebra striped table</h3>
<p><strong>setZebraStyle()</strong> function can be used to color odd and even rows differently; for example, <em>odd rows</em> in <em>gray</em> color and <em>even rows</em> in <em>white</em> color.</p>
<pre class="r"><code>doc <- docx()

data<-iris[1:5, ]

# Zebra striped tables
doc <- addTitle(doc, "Zebra striped tables")
MyFTable <- vanilla.table(data)
MyFTable <- setZebraStyle(MyFTable, odd = &amp;#39;#eeeeee&amp;#39;, even = &amp;#39;white&amp;#39;)
doc <- addFlexTable( doc, MyFTable)

writeDoc(doc, file = "r-reporters-word-document-zebra.docx")</code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-word-document-table-zebra.png" alt="R software and Reporters package, add table to a Word document" /></p>
</div>
</div>
<div id="add-lists-ordered-and-unordered-lists" class="section level2">
<h2>Add lists : ordered and unordered lists</h2>
<p>Ordered and unordered lists can be added using <strong>addParagraph()</strong> function as follow :</p>
<pre class="r"><code>doc = addParagraph(doc, 
  value = c(&amp;#39;Item 1&amp;#39;, "Item 2", "Item 3")
  par.properties = parProperties(list.style = &amp;#39;ordered&amp;#39;, level = 1 )</code></pre>
<br/>
<div class="block">
<ul>
<li><strong>value</strong> : a set of texts to be added as a list</li>
<li><strong>par.properties</strong> : the paragraph formatting properties
<ul>
<li><em>list.style</em> : possible values are ‘unordered’ and ‘ordered’</li>
<li><em>level</em> : a numeric value indicating the level of the item to be added in the list</li>
</ul></li>
</ul>
</div>
<p><br/></p>
<div id="add-simple-lists" class="section level3">
<h3>Add simple lists</h3>
<pre class="r"><code>doc <- docx()

# Ordered list
doc <- addTitle(doc, "Ordered List")
doc <- addParagraph(doc, value= c("Item 1", "Item 2", "Item 3"),
          par.properties =  parProperties(list.style = &amp;#39;ordered&amp;#39;))

# Unordered list
doc <- addTitle(doc, "Unordered List")
doc <- addParagraph(doc, value= c("Item 1", "Item 2", "Item 3"),
          par.properties =  parProperties(list.style = &amp;#39;unordered&amp;#39;))

writeDoc(doc, file = "r-reporters-word-document-lists.docx")</code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-word-document-simple-list.png" alt="R software and Reporters package, add lists to a Word document" /></p>
</div>
<div id="add-multi-level-lists" class="section level3">
<h3>Add multi-level lists</h3>
<p>To simplify the code, we’ll first define some levels to be used for creating multi-level lists.</p>
<pre class="r"><code>doc <- docx()

# Define some levels for ordered lists (ol)
ol1 = parProperties(list.style = "ordered", level = 1)
ol2 = parProperties(list.style = "ordered", level = 2)

# Define some levels for unordered lists (ul)
ul1 = parProperties(list.style = "unordered", level = 1)
ul2 = parProperties(list.style = "unordered", level = 2)

# Multi-lvel ordered list
doc <- addTitle(doc, "Ordered List")
doc <- addParagraph(doc, value= "Item 1", par.properties =  ol1)
doc <- addParagraph(doc, value= "Item 1.1", par.properties =  ol2)
doc <- addParagraph(doc, value= "Item 1.2", par.properties =  ol2)
doc <- addParagraph(doc, value= "Item 2", par.properties =  ol1)


# Multi-lvel unordered list
doc <- addTitle(doc, "Unordered List")
doc <- addParagraph(doc, value= "Item 1", par.properties =  ul1)
doc <- addParagraph(doc, value= "Item 1.1", par.properties =  ul2)
doc <- addParagraph(doc, value= "Item 1.2", par.properties =  ul2)
doc <- addParagraph(doc, value= "Item 2", par.properties =  ul1)

writeDoc(doc, file = "r-reporters-word-document-multilevel-lists.docx")</code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-word-document-multilevel-list.png" alt="R software and Reporters package, add lists to a Word document" /></p>
</div>
</div>
<div id="add-a-footnote-to-a-word-document" class="section level2">
<h2>Add a footnote to a Word document</h2>
<p>A <strong>footnote</strong> is a set of paragraphs written at the bottom of the Word document.</p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-word-document-footnote.png" alt="R software and Reporters package, add foot note to a Word document" /></p>
<p>The steps for adding a footnote into a document are :</p>
<ol style="list-style-type: decimal">
<li>Create a footnote object using the function <strong>Footnote()</strong></li>
<li>Add a text to the footnote using the function <strong>addParagraph()</strong></li>
</ol>
<pre class="r"><code>library(ReporteRs)

doc <- docx()

# create footnotes
footnote1 <- Footnote( ) # footnote1 about R
footnote1 <- addParagraph(footnote1, "R is a free software for statistical computing and graphics.")

footnote2 <- Footnote( ) # footnote2 about ReporteRs package
footnote2 <- addParagraph(footnote2, "ReporteRs is an R package to write and format easily a Word document.")

# Use the footnotes when writing a paragraph into the document
doc <- addTitle(doc, "Word document with a footnote", level=1)
doc <- addParagraph(doc, 
      "This Word document is created using " + 
      pot("R software", footnote=footnote1)+ " and "+
      pot("ReporteRs", footnote=footnote2) +" package."
      )

writeDoc(doc, file = "r-reporters-word-document-footnote.docx")</code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-word-document-footnote-part1.png" alt="R software and Reporters package, add foot note to a Word document" /> <img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-word-document-footnote-part2.png" alt="R software and Reporters package, add foot note to a Word document" /></p>
</div>
<div id="add-r-scripts" class="section level2">
<h2>Add R scripts</h2>
<p>The function <strong>addRScript()</strong> can be used as follow :</p>
<pre class="r"><code>doc <- docx()

r_code <- &amp;#39;summary(cars$dist)
x <- rnorm(100)
hist(x)
&amp;#39;

# Change the backgroud-color
doc <- addRScript(doc, text = r_code,
    par.properties= parProperties(shading.color = &amp;#39;gray90&amp;#39;))

writeDoc(doc, file = "r-reporters-word-add-r-code.docx")</code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/r-reporters-word-document-r-code.png" alt="R software and Reporters package, add foot note to a Word document" /></p>
</div>
</div>
<div id="add-a-table-of-contents-into-a-word-document" class="section level1">
<h1>Add a table of contents into a Word document</h1>
<p>To add a table of contents, the function <strong>addTOC()</strong> can be used.</p>
<p><span class="warning">Note that, when a table of content (TOC) is added into a Word document, a message box is displayed when opening the file for the first time. This message asks if you want to update TOC entries. This is not an error and you should click ‘Yes’ to update TOC entries. You should save the document to avoid this to happen the next time.</span></p>
<pre class="r"><code>doc <- docx() # Create a Word document

# Add a title
doc <- addTitle(doc, "Create a Word document with TOC", level = 1)

# Add a table of contents
doc <- addTOC(doc)

doc <- addPageBreak(doc) # go to the next page

# Add an introduction
doc <- addTitle(doc, "Introduction", level = 2)
doc = addParagraph(doc, value ="This Word document is created using R software and ReporteRs package. The goal of this section is to show you how to add a table of contents into a Word document. This can be done easily using the function addTOC.")

# Add a box plot
doc <- addTitle(doc, "Box plot using R software", level = 2)
boxplotFunc<-function(){
  boxplot(len ~ dose, data = ToothGrowth,
      col = rainbow(3), main = "Guinea Pigs&amp;#39; Tooth Growth",
      xlab = "Vitamin C dose mg", ylab = "tooth length")
}
doc <- addPlot(doc, boxplotFunc)
 
doc <- addPageBreak(doc) # go to the next page

# Add plots
#+++++++++++++++++++++
doc <- addTitle(doc, "Basic plots using R software", level = 2)
doc <- addParagraph(doc, "R is a free software for plotting and data analysis. This chapter contains examples of graphs generated using R.")

# Add a histogram
doc <- addTitle(doc, "Histogram", level = 3)
doc <- addPlot(doc, function() hist(iris$Sepal.Width,
                                  col="lightblue"))

doc <- addPageBreak(doc) # go to the next page

# Add a bar plot
doc <- addTitle(doc, "Bar plot", level = 3)
doc <- addPlot(doc, function() barplot(VADeaths))

# Write the Word document to a file 
writeDoc(doc, file = "r-reporters-word-document-toc.docx")</code></pre>
<p><span class="success">The Word document created by the <strong>R</strong> code above is available here : <a href="https://www.sthda.com/english/sthda/RDoc/example-files/r-reporters-word-document-toc.docx">R software and ReporteRs package - Word document with a table of contents</a></span></p>
<iframe src="http://www.slideshare.net/slideshow/embed_code/43023168" width="570" height="510" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;" allowfullscreen> </iframe>    
     


</div>
<div id="infos" class="section level1">
<h1>Infos</h1>
<p><span class="warning"> This analysis has been performed using R (ver. 3.1.0). </span></p>
<p>You can read more about ReporteRs and download the source code at the following link :</p>
<p>GitHub (David Gohel): <a href="https://github.com/davidgohel/ReporteRs" rel="nofollow" target="_blank">ReporteRs</a></p>
</div>

<script>jQuery(document).ready(function () {
    jQuery('h1').addClass('wiki_paragraph1');
    jQuery('h2').addClass('wiki_paragraph2');
    jQuery('h3').addClass('wiki_paragraph3');
    jQuery('h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->



<!-- END HTML -->]]></description>
			<pubDate>Sat, 03 Jan 2015 22:52:48 +0100</pubDate>
			
		</item>
		
	</channel>
</rss>
