% Tutorial 7: Exporting Your Work % DPI R Bootcamp % Jared Knowles

# Overview

In this lesson we hope to learn:

• Quick and basic export of results
• Writing a basic report
• Exporting graphics for use in other documents
• Reproducible research

# Why does Export Matter?

• Need to be able to share work with others and present it
• This can be tricky in R because R has so many choices for export
• R can talk to a number of outside formats including Excel, Word, PDF, PNG, HTML
• R can even be used as a web service: https://public.opencpu.org/pages/
• We'll cover the basics, but there are a lot of outside resources for doing what you need in your own environment

# Generating a basic report

• There are a few key concepts that R allows that we should follow when preparing a report on data
1. Include the data, source code, and output together in one package
2. Have the source code available for raw data to finished product
3. Present figures, tables, and code in a single document
• Why do we do this?

• Transparency
• Reproducibility
• It isn't much harder than the basic analysis itself

# A few terms

• Rmarkdown extension .Rmd and used for R flavored markdown in RStudio
• R script extension .R used to store R code only
• R HTML extension .RHtml used to store R HTML
• Text extension .txt used to write plain text
• ** R Sweave** extension .Rnw used to write LaTeX reports with embedded R code

# Beginning

• Open a new script file in RStudio–“R Markdown”
• This opens a template file for an HTML report, the easiest type to create
• In R there are a number of packages designed to help create reports and place them on a scale like so:

# Get the tools

• install.packages('knitr','markdown'); sweave is part of R base already
• We need a LaTeX distribution for Tex documents
• What is LaTeX?
• An open source typsetting framework that can be used to produce complex technical documents
• We won't mess with LaTeX today - instead we will focus on HTML and markdown

# A Simple Example

• Paste this in a new R script
• Save the script in your working directory as myscript.R
#' This is some text
#'

# + myplot, dev='svg',out.width='500px',out.height='400px'

library(ggplot2)
data(diamonds)
qplot(carat, price, data = diamonds, alpha = I(0.3), color = clarity)

#' Diamond size is clearly related to price, but not in a linear fashion.
#'


# Converting the Script

• This happens in two steps to make an HTML file
o <- spin("C:/Path/To/myscript.R", knit = FALSE)
knit2html(o, envir = new.env())


# Example Script II

• Create myscript2.R
#' This is some text that I want to explain
#' For example, this plot is important, let's look below

# + myplot,
# dev='svg',out.width='500px',out.height='400px',warning=FALSE,message=FALSE

library(ggplot2)
load("PATH/TO/MY/DATA.rda")
qplot(readSS, mathSS, data = df, alpha = I(0.2)) + geom_smooth()

#' There is not a linear relationship, but it sure is close.
#' Let's do some regression
#'

test <- lm(mathSS ~ readSS + factor(grade), data = df)
summary(test)

#' It's all statistically significant


# Spin 2

o <- spin("C:/Path/To/myscript2.R", knit = FALSE)
knit2html(o, envir = new.env())
# We specify that new environment is used to carry out the analysis, not
# the current environment


# Stitch

• Stiching is similar to spinning, but we don't even have to mess around with anything but our R script
• Just write the R code
• Any comments you write preceeded by the # denote something we want printed in the text
• Even more simplified than spin

# Stitch Example

## title: My Super Report ## Author: Mr. Data ##

# A plot and some text
library(ggplot2)
library(xtable)
load("PATH/TO/MY/DATA")
qplot(readSS, mathSS, data = df, alpha = I(0.2)) + geom_smooth()