# Stat Bytes: R Workflow Tips with RStudio, Shiny, knitr

## Jerzy Wieczorek, 2/10/2014

### Reproducible Research / Literate Programming

• Beneficial even for informal reports/homework or websites
• R Notebook example
• R Markdown example
• R Sweave + Beamer example
• RWordPress package
• Further details/documentation: Yihui Xie's knitr website and book, Dynamic Documents with R and knitr
• Two more benefits to highlight:
• R Notebook and R Markdown create an encapsulated HTML file: Images are encoded within the file, and MathJax is called to draw any $$\LaTeX$$ math on demand. So you only have a single HTML file to pass around.
• In R Markdown output, commands are uncommented, and results are commented out, so it's easy to cut-and-paste back into R. (Contrast this with usual output where the prompt '>' precedes commands, and results are uncommented, so every line needs to be modified before pasting into R.)
• knitr and Shiny are integrated well into RStudio, but it's not necessary: they work in base R too

### RStudio Tips

• RStudio Server
• Shortcuts (run code, switch windows)
• Extract a function from code
# (Setting eval=FALSE for this code chunk, in order to just show the code
# but not run it)

# Set up data for a model, y = beta*x + e
x = 1:30
beta = 0.2

# Let's function-ify this code and call the function lmplot: Select these
# four lines of code, and go to Code > Extract Function
e = rnorm(length(x))
y = beta * x + e
plot(x, y, main = "y = x + e")
abline(lm(y ~ x))

# Now we can run our new lmplot function
lmplot(x = 1:50, beta = 0.2)