Difference between revisions of "R knitr"

From "A B C"
Jump to navigation Jump to search
Line 25: Line 25:
  
  
Let's give it a try: we'll develop an '''R''' function that will find us a random phobia to ponder on.
+
Let's give it a try: we'll write and document an '''R''' function that will find us a random phobia to ponder on.
  
 
{{task|
 
{{task|

Revision as of 17:57, 17 January 2015

knitr


This page is a placeholder, or under current development; it is here principally to establish the logical framework of the site. The material on this page is correct, but incomplete.


This page contains examples for the use of knitr to create documents from RMarkdown or LaTex sources.



knitr is an R package for literate programming. It is integrated with R Studio and the exercises on this page assume you have R Studio installed.


RMarkdown

Markdown is an extremely simple and informal way of structuring documents that is useful if for some reason you feel html is too complicated. That's really all it does: format documents in a simple way so they can be displayed as Web pages. For Markdown documentation, see here.. The concept is quite similar to Wiki markup syntax, the syntax is (regrettably) different, and for a number of features there there are (regrettably) several different ways to achieve the same results.

RMarkdown is an R package that is integrated with R Studio and allows integrating R code with Markdown documents. knitr can work with Markdown files, and this gives additional output options, such as PDF and MSWord documents.


Let's give it a try: we'll write and document an R function that will find us a random phobia to ponder on.

Task:

  • Open an R Studio session.
  • Select Session → Set Working Directory → Choose Directory... and choose some project directory.
    • Note that there is a bug in R Studio that will prevent the knitr interface from working correctly if your home directory contains an .Rprofile file that issues a setwd() command to a directory other than your project directory. If you run into an error when weaving your file, remove any setwd() command you might find in such a profile.
  • Select File → New File → RMarkdown.... When you do this the first time, R Studio will ask you whether you want to install/update a number of required packages. Click Yes.
  • Enter "Random Phobia" as the Title and your name as the Author, select to create a Document, and check HTML as the default output option.

R Studio will load some default text and markup into the script pane which we can edit.

Let's introduce our plan: copy/paste the following text into the document.

##Phobias!
We all have some, but we could always use more. How to know them all? With this code we access the Oxford English Dictionary's Website - the most authoritative source on the English language, and scrape a list of phobias. A function is supplied to retrieve a random phobia, which we can subsequently ponder on - either to delight in the fact that we don't have that fear, or to add to our daily quota of anxieties <small>(like our well-founded [fear of bad programming practice](http://xkcd.com/292/))</small>.

To load the list, we will "screenscrape" a list of Phobias from the [OED Phobia list](http://www.oxforddictionaries.com/words/phobias-list). First, we load the XML library (or install it from CRAN, if we don't have it).

Note the following Markdown elements in the code:


- a tag <small>Text...</small> to set text to a smaller font size (we could have used <span style="font-size:85%;">Text...</span> instead because Markdown respects HTML elelements).
- a Web link [Text...](URL)added to text
  • Click on the green question mark of the menu of your script pane. There is a link to an overview of RMarkdown use and to a quick reference. Load the quick reference (it will appear in the Help pane) and scan it.
  • The filename in the script pane tab is red, because it contains unsaved changes. Save the file in your project directory, note that the extension .Rmd is automatically added.

Time to add our first bit of R code

  • Copy and paste the following:
```{r loadLibrary}
if (!require(XML, quiet=TRUE)) { 
  install.packages("XML")
  library(XML)
}
```


This is what is know as a "code chunk". It is delimited by three backticks ``` and has directives and options for the chunk in the first line. It is labelled as R code, and note that after the {r we have added an (optional) label for the chunk. That is useful, because we can rapidly navigate between chunks (click on the navigation menu at the bottom of the script pane), and we can refer to the labels to execute chunks that are coded later in the document at an earlier stage. This is an important idea of literal programming: the flow of the document should not be determined by the requirements of the code, but by the logic of the narrative. TLDR; label your chunks. It's useful.

Other options can be added after a comma, for example we can suppress printing of a chunk into the document altogether, if we think it is not relevant for the document, by adding the option echo=FALSE[1].

  • To execute a particular chunk, simply place the cursor into the chunk and select Chunks → Run Current Chunk from the menu at the top of the script pane. Try this and check the console pane, the library should load without error.
  • Let's add more text and code: copy and paste this into the document to add more comment and a second chunk.

The XML package provides a function -- `readHTMLTable()` -- that makes our life very easy: it accesses an URL, looks for all HTML formatted tables, parses them and returns them as lists. Internally, by default `readHTMLTable` reads the data into a dataframe, so to avoid converting all the text into factors we set the option `stringsAsFactors=FALSE`. There may be several tables in the source page, each one is returned as a list element. Since we know (hope?) the OED page contains only one table, we use only the first list element.

```{r getPageData, cache=TRUE}
phobiaFrame <- readHTMLTable("http://www.oxforddictionaries.com/words/phobias-list"
                             stringsAsFactors{{=}}FALSE)[[1]]
```

Two things to note here:

  • Enclosing a piece of text in "backticks" `Text...` formats that text as "code" - typically in a fixed-width font.
  • For this chunk we have set the option cache as TRUE. This is a very useful and well thought out mechanism that avoids recomputing code that takes a long time or should otherwise be limited. The results of a cached chunk of code are stored locally and retrieved when the file is weaved. Only if anything within the chunk is changed (or cache is set to FALSE), is the chunk evaluated again. This prevents us from excessively pounding on the OED as we develop our script, which is a question of good manners in the context of this example, but can save a lot of time as our projects become large and the calculations become complex.

In order to make sure everything has worked, we'll print a sample from the table to our documentation file. RMarkdown provides a shorthand notation for tables - just like Wiki markup. I never use these. HTML tables are easy enough to format and remember and they provide many more options. In the example below, we customize the row background-color for alternating rows. That is something we could not do with simple markdown.

  • Paste the following:
**Table**: seven random phobias
```{r renderPhobiaTable, echo=FALSE, results='asis'}
cat("<table border=\"1\", width=\"50%\">\n")
cat("<tr style=\"background-color:#CCFFF0;\"><th>Phobia</th><th>Fear of...</th></tr>\n")
for (i in 1:7) {
  r <- randRow(phobiaFrame)
  if (i %% 2) {
    cat("<tr style=\"background-color:#F9F9F9;\">")
  }
  else {
    cat("<tr style=\"background-color:#EEFFF9;\">")
  }
  cat(paste("<td>", r[2], "</td><td>", r[1], "</td></tr>\n", sep=""))
}
cat("</table>\n")
```

This is now a mix of markup code and R code. Two important options in the chunk header:

  • echo=FALSE prevents the contents of the chunk to be printed. We don't want this code in our output, we only want the result.
  • results='asis' prevents the results from being marked up. The raw HTML is sent to the output document.

But note the following: this piece of code calls a function randRow(phobiaFrame) that we have not defined yet. In an R script this would not work. But in a knitr document we can reference a chunk of code anywhere in (and outside) of the document and thus define our function before the renderPhobiaTable chunk is executed. This is important for literate programming, where we don't want to be constrained by the requirements of the code.

Therefore, paste the following before the previous chunk:

```{r , ref.label="randRow", echo=FALSE}
```

This executes the code chunk with the label randRow without giving any output.

To finish off, paste the following:

<p>&nbsp;
<p>
To pick a single random phobia from the list, we take a (pseudo) random sample of size 1 from the number of rows in the `phobiaFrame` object. Our function thus returns a random row from a matrix or dataframe, and it uses an optional argument: `seed`. This can either be Boolean `FALSE` (the default), or an integer that is used in R's `set.seed()` function.

```{r randRow}
randRow <- function(M, seed=FALSE) {
  if (seed) set.seed(as.integer(seed))
  return(M[sample(1:nrow(M), 1),])
}
```

With this useful tool we can ponder on our favourite phobia of the day. For today, let it be **`r randRow(phobiaFrame, seed=1123581321)[2]`**, the fear of `r randRow(phobiaFrame, seed=1123581321)[1]`.

Reptiles! Awful.

This piece now contains the function definition for randRow, which it prints to the document after our comments. It also contains inline R code that is executed as the document is built.

  • That should be all. You should be able to save the document and click the Knit HTML button to execute the code, build, and load a Webpage with the document we just wrote. Please get in touch if you run into problems.

If all the pasting of bits and chunks was confusing, the final .Rmd file is here.




 

LaTex

TBD

 

Notes, Further Reading, and Resources

  1. For a complete list of chunk options, see the documentation by knitr's author, Xie Yihui.


http://yihui.name/knitr/options/ A list of chunk options by the author of knitr. Required reading.
http://yihui.name/knitr/demos/ Demo files for many different aspects of knitr use. Learn by example.