<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/how-to-create-reports-with-r-markdown-in-rstudio/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exercises.com/2017/07/21/how-to-create-reports-with-r-markdown-in-rstudio/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-create-reports-with-r-markdown-in-rstudio"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://i2.wp.com/www.r-exercises.com/wp-content/uploads/2017/07/document-428335__340.jpg" target="_blank"><img sizes="(max-width: 150px) 100vw, 150px" src="https://i1.wp.com/www.r-exercises.com/wp-content/uploads/2017/07/document-428335__340-150x150.jpg?resize=150%2C150" alt="" class="wp-image-7151" data-recalc-dims="1"></a><strong>Introduction</strong></p>
<p>R Markdown is one of the most popular data science tools and is used to save and execute code to create exceptional reports whice are easily shareable.</p>
<p>The documents that R Markdown provides are fully reproducible and support a wide variety of static and dynamic output formats. </p>
<p>R Markdown uses markdown syntax, which provides an easy way of creating documents that can be converted to many other file types, while embeding R code in the report, so it is not necessary to keep the report and R script separately. Furthermore The report is written as normal text, so knowledge of HTML is not required. Of course no additional files are needed because everything is incorporated in the HTML file.</p>
<p><strong>Package Installation</strong></p>
<p>In order to use the R Markdown package we have to install and call it with:<br>
<code>install.packages(&quot;rmarkdown&quot;)<br>
library(rmarkdown) </code></p>
<p><strong>Create an R Markdown file</strong></p>
<p>To create a new R Markdown file (.Rmd) in RStudio we follow these steps: select File -> New File -> R Markdown…, then choose the file type you want to create. We choose the default .html, which can be easily converted to other file types later.</p>
<p>The .Rmd file we just created comes with default text as you can see but we will create our own file step by step so we delete everything.</p>
<p><strong>R Markdown Syntax</strong></p>
<p><strong><em>The YAML Header</em></strong></p>
<p>At the top of any R Markdown script is always the YAML header section enclosed by <code>---</code> and is the minimum code chunk that should be put there. By default this includes a title, author, date, and the file type you want as output. Whatever we set in the header section is applied to the whole document.</p>
<p>This is an example of a YAML header at the top of an .Rmd script which creates an html document.<br>
<code>---<br>
title: &quot;R Markdown Example&quot;<br>
author: Makis Kasvikis<br>
date: 17/Aug/2017<br>
output: html_document<br>
---</code></p>
<p><strong>Inserting Code</strong></p>
<p>Code that is included in your .Rmd document should be enclosed by three backwards apostrophes <code>```</code> like the example below:<br>
<code>```{r}<br>
summary(cars)<br>
```</code></p>
<p>The space inside the brackets is where you can assign rules for that code chunk. The code chunk above says that the code is R code.</p>
<p>Of course you can click the green button with the “plus” symbol in <a rel="nofollow" href="https://www.rstudio.com/products/rstudio/download/" target="_blank">RStudio</a> to put a code chunk wherever you want.</p>
<div>
<aside><img src="http://www.r-exercises.com/2017/07/21/how-to-create-reports-with-r-markdown-in-rstudio/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-create-reports-with-r-markdown-in-rstudio"></aside>
<div><strong>Learn more</strong> about reporting your results in the online course: <a rel="nofollow" href="http://www.r-exercises.com/product/r-for-data-science-solutions/" target="_blank">R for Data Science Solutions</a>. In this course you will learn how to:
<ul>
<li>Build a complete workflow in R for your data science problem</li>
<li>Get indepth on how to report your results in a interactive way</li>
<li>And much more</li>
</ul>
</div>
</div>
<p><strong>Instructions for Code Chunks </strong></p>
<p>As you have probably understood by now you can use code to do everything you want. For example you can generate a plot like this:<br>
<code>```{r}<br>
plot(cars)<br>
```</code></p>
<p>Or you can create a dataframe:<br>
<code>```{r}<br>
A &lt;- c(&quot;Bob&quot;, &quot;Tom&quot;, &quot;Bill&quot;, &quot;Joe&quot;)<br>
B &lt;- c(1.78, 1.86, 1.85, 1.70)<br>
dataframe &lt;- data.frame(A, B)<br>
head(dataframe)<br>
```</code></p>
<p>In case you don’t want that code to appear in the report and just display the result, then you should set <code>echo=FALSE</code> in the code chunk instructions.<br>
<code>```{r,echo=FALSE}<br>
A &lt;- c(&quot;Bob&quot;, &quot;Tom&quot;, &quot;Bill&quot;, &quot;Joe&quot;)<br>
B &lt;- c(1.78, 1.86, 1.85, 1.70)<br>
dataframe &lt;- data.frame(A, B)<br>
head(dataframe)<br>
```</code></p>
<p>If you want to load a package like <code>knitr</code> you must load the package in the .Rmd file. In case you do not want the entire code chunk and its output from appearing in the report, use <code>include=FALSE:<br>
```{r, include=FALSE}<br>
library(knitr)<br>
```</code></p>
<p><strong>More code instructions</strong></p>
<p><strong>eval:</strong> <code>eval=TRUE</code><strong>–</strong> Is the code run and the results included in the output?<br>
<strong>include:</strong>	<code>include=TRUE</code><strong>–</strong> Are the code and the results included in the output (the code is still run)?<br>
<strong>echo:</strong> <code>echo=TRUE</code><strong>–</strong> Is the code displayed alongside the results?<br>
<strong>warning:</strong> <code>warning=TRUE</code><strong>–</strong> Are warning messages displayed?<br>
<strong>error:</strong> <code>error=FALSE</code><strong>–</strong> Are error messages displayed?<br>
<strong>message:</strong> <code>message=TRUE</code><strong>–</strong> Are messages displayed?<br>
<strong>tidy:</strong> <code>tidy=FALSE</code><strong>–</strong> Is the code reformatted to make it look “tidy”?<br>
<strong>results:</strong> <code>results=&quot;markup&quot;</code><strong>–</strong> How are results treated?<br>
<strong>cache:</strong> <code>cache=FALSE</code><strong>–</strong> Are the results cached for future renders<br>
<strong>comment:</strong><code>comment=&quot;##&quot;</code><strong>–</strong> What character are comments prefaced with?<br>
<strong>fig.width:</strong>, <code>fig.width=7</code><strong>–</strong> What width (in inches) are the plots?<br>
<strong>fig.align:</strong> <code>fig.align=&quot;left&quot;</code><strong>–</strong> “left” “right” “center”</p>
<p><strong>Inserting Figures</strong></p>
<p>To manually set the figure dimensions, you can insert an instruction:<br>
<code>```{r,fig.width=5, fig.height=5,echo=FALSE}<br>
plot(cars)<br>
```</code></p>
<p>By default, figures are rendered as .png files by R Markdown, so you can change that to .svg, a vector file format by adding dev=”svg” to the code chunk instruction section.<br>
<code>```{r,fig.width=5, fig.height=5,echo=FALSE,dev=&quot;svg&quot;}<br>
plot(cars)<br>
```</code></p>
<p><strong>Inserting Tables</strong></p>
<p>While R Markdown can print the contents of a data frame easily by enclosing the name of the data frame in a code chunk this doea not seem the best idea.<br>
<code>```{r,echo=FALSE}<br>
dataframe<br>
```</code><br>
A better solution could be to use the table formatting function <code>kable()</code> in the <code>knitr</code> package like the example below:<br>
<code>```{r,echo=FALSE}<br>
library(knitr)<br>
kable(dataframe,digits=1)<br>
```</code></p>
<p><strong>Formatting Text</strong></p>
<p>Markdown syntax can be used to change how text appears in your output file, here are a few common formatting commands</p>
<p>header 1: <code># header 1</code><br>
header 2: <code>## header 2</code><br>
header 3: <code>### header 3</code><br>
header 4: <code>#### header 4</code><br>
bold text: <code>**bold**</code> text<br>
italics text: <code>*italics*</code> text<br>
code text: <code>`code`</code> text<br>
inlcude a link: <code>[link](www.r exercises.com)</code><br>
Including a picture: <code>R Studio Logo ![R Studio Logo](img/R Studio Logo.png)</code><br>
LaTeX equation syntax: <code>$A = \pi \times r^{2}$</code></p>
<p>You can also manually create small tables using markdown syntax.</p>
<p><code>`:----:`</code>:	<strong>Centre</strong><br>
<code>`:-----`</code>:	<strong>Left</strong><br>
<code>`-----:`</code>:	<strong>Right</strong><br>
<code>`------`</code>:	<strong>Auto</strong></p>
<p>For example:<br>
<code><br>
| Teams | Wins  | Loses  |<br>
|:------|:-----:|-------:|<br>
| A     | 21    | 4      |<br>
| B     | 19    | 6      |<br>
| C     | 17    | 8      |<br>
</code></p>
<p><strong>Compiling an R Markdown file</strong></p>
<p>Select Knit -> Knit to HTML in <a rel="nofollow" href="https://www.rstudio.com/products/rstudio/download/" target="_blank">RStudio</a>.</p>
<p>A preview appears in the Viewer window in <a rel="nofollow" href="https://www.rstudio.com/products/rstudio/download/" target="_blank">RStudio</a> and the .html file is saved to your working directory. You can set your working directory location using:<br>
<code>setwd(&quot;~/...&quot;)</code></p>
<p>You can find your working directory using:<br>
<code>getwd()</code></p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="http://www.r-exercises.com/2015/10/09/vector-exercises/" title="Vector exercises" target="_blank">Vector exercises </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2016/12/12/building-shiny-app-exercises-part-1/" title="Building Shiny App exercises part 1" target="_blank">Building Shiny App exercises part 1 </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/04/01/building-shiny-app-exercises-part-9/" title="Building Shiny App Exercises (part-9)" target="_blank">Building Shiny App Exercises (part-9) </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/start-here-to-learn-r/" target="_blank">Explore all our (>1000) R exercises</a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/r-courses/" target="_blank">Find an R course using our R Course Finder</a> directory</li>
</ol>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.r-exercises.com/2017/07/21/how-to-create-reports-with-r-markdown-in-rstudio/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-create-reports-with-r-markdown-in-rstudio"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/khZ2XlvJhL0" height="1"></a></div>