Setting up Git

Git and Dropbox

Git is a distributed version control network. Think of it as a Polaroid camera that takes snapshots of your project files across the duration of the project, and then stores these snapshots in an easily accessible way. If you would like to see how your project code was different a month ago, you are able to simply view that snapshot with a few lines of code, or through a GUI. I'm not going to get into the depths of Git here, but using version control is quite important, especially if we are interested in producing easily reproducible reports. There is a useful online Git manual here. Let's get started.

setwd("/home/nick/Dropbox/Gittest")

Various Example Git Command Line Operations (for X systems)

# add a file to Git repository, or with wildcard, all of a filetype
system("git add *.Rmd")
# get status of repository before next commit
system("git status")
# commit file changes to already added files (don't need to re-add each
# time)
system("git commit -a -m 'Git commit note here'")
# print commit history
system("git log")

# note, in the above code, I call to the system shell with the 'system()'
# function.

R Markdown

R Markdown is an adoption of the markdown language for use in R. It allows for quick report writing and publishing results to HTML, which in turn allows for easier integration of dynamic charts and figures into your analysis. It also allows for rapid generation of interactive HTML5 slide shows via a package called slidify. Markdown is a simple to type language that seeks to ease the task of writing within documents that will then recieve additional automatic formatting. A package called knitr allows for a specific variant of Markdown, R Markdown. The package knitr translates the R Markdown, which allows direct integration of R code chunks, into Markdown and then into HTML. knitr also is an improvement on Sweave, allowing for integrated scripting of R and Latex. Check it out.

R Markdown Examples

R Markdown and Graphs

R Markdown produces nice inlaid graphs from R into the HTML.

First, create some variables.

rainfall <- rnorm(100)
temperature <- rnorm(100)
aridity <- rnorm(100)
gradient <- rnorm(100)

The below code will evaluate and show code and plot the graph:

plot(rainfall, temperature)

plot of chunk plot example1

hist(aridity)

plot of chunk plot example1

plot(density(gradient))

plot of chunk plot example1

R Markdown and Tables

We can even hand-make HTML tables!

Column 1 Column 2 Column 3
Hello Hi there Yo
Good day Howdy Hola

In R Studio, to use knitr to translate R Markdown into html, hit Ctrl+shift+h.

But manually typsetting the above table is pretty damn tedious. More on this later.

Latex tables can be directly input into R Markdown, but they won't show up in HTML. Using a separate program, Pandoc, however, allows translation of the markdown produced by knitr into LaTeX. The table then appears in the resulting .pdf. I'm going to focus primarily on HTML output in this treatment, but at the bottom is a bit of the code needed to get you started.

\begin{tabular}{|l|l|}\hline Age & Frequency \ \hline 18–25 & 15 \ 26–35 & 33 \ 36–45 & 22 \ \hline \end{tabular}

Automatic HTML Table Generation

Install and call xtable first.

# install.packages('xtable')
library(xtable)

Then, we need to create a function to automate the use of the xtable function. It, by default, annoyingly requires manual input of attribute vectors with length equal to the number of columns in the resulting table.

# create a function to automate xtable HTML tables automatically adjusts
# for number of columns
library(xtable)
htmltab <- function(input, dig = 3, algn = "c", descr = "Caption", typ = "html") {

    numcol <- ncol(xtable(input))

    col <- numcol + 1

    print(xtable(input, caption = c(descr), align = c(rep(algn, col)), digits = c(rep(dig, 
        col))), type = typ, quote = FALSE)

}

Then, create a HTML regression table.

htmltab(lm(rainfall ~ temperature + gradient + aridity), descr = "Example Regression Table")
Example Regression Table
Estimate Std. Error t value Pr(> |t|)
(Intercept) 0.124 0.096 1.286 0.201
temperature -0.053 0.106 -0.498 0.619
gradient 0.083 0.109 0.759 0.450
aridity -0.159 0.100 -1.589 0.115

Unfortunately, the default table produced by R Studio CSS isn't very pretty, so need to edit to use a custom CSS file. CSS is easy and there are lots of templates online, so be creative. Below is the neccessary option code to change the .css file from here.

options(rstudio.markdownToHTML = function(inputFile, outputFile) {
    require(markdown)
    markdownToHTML(inputFile, outputFile, stylesheet = "/home/nick/Dropbox/rstudio/custom_knitr.css")
})

R Markdown and LaTeX Equations

If you need to write an equation in your HTML, you can just use LaTeX code to do so. For example, to display an equation:

$$\beta=\frac{3+\alpha}{\zeta}$$

Pandoc for R Markdown to .pdf

To use Pandoc, need specific packages:

# install.packages('knitr','markdown')
library(knitr)
library(markdown)
knit("markdown_notes_Gittest.Rmd")
options(rstudio.markdownToHTML = function(inputFile, outputFile) {
    system(paste("/home/nick/.cabal/bin/pandoc", shQuote(inputFile), "-o", shQuote(outputFile)))
})
markdownToHTML("markdown_notes_Gittest.md", "markdown_notes_pandoc.html")

Now the LaTeX table should look good. But, it doesn't? That's because the LaTeX can't go nicely into HTML. HTML5 tables will have to be used instead if one wants to publish tables to the web. Or, do a workaround by creating .pdf tables separately and then calling them as images into your HTML document.

So, try to get to a .pdf:

system("~/.cabal/bin/pandoc markdown_notes_Gittest.md -o markdown_notes_Gittest.pdf")

And now you have a .pdf from R Markdown.

Also, you should look into Pander as it seems like it might hold a lot of promise here too as well.

Other Examples Using R Markdown