Skip to Tutorial Content

Literate programming

Literate programming allows us to include computer code into documents to allow sophisticated documentation of code and to generate reproducible results from data analysis. To do this, we write our documents in plain text, format the document with Markdown syntax, and insert R code into the document. Compile the code to integrate everything and produce a final, fully formatted document. This final document can be a manuscript, letter, web page, presentation, book, or other form of output.

Here is what the process looks like:

{knitr} is an R package that takes R Markdown documents (.Rmd), processes the R code, and generates a regular Markdown document (.md). pandoc is a program that converts the Markdown document into one of many different kinds of files, including .tex, .pdf, .html, .docx, etc.

Markdown

Syntax review

Markdown is a syntax that allow simple, human readable code for formatting documents. It is completely separate from R, so you can create Markdown documents without embedding R code and have them produce PDFs or HTML code.

Practice

Let's get some more practice writing the syntax that will format text.

Quiz

Inline code

Syntax review

R Markdown is a special form of Markdown that allows the embedding of R code into Markdown. R code can be embedded in two ways: inline code and code chunks.

Inline code means that the R code is embedded in the middle of a sentence. This allows you to effectively write formulas that will automatically insert sample sizes, measures of central tendency and uncertainty, and statistics directly in to your sentences.

To add inline code, you simply use the `r code` syntax, where you insert a single R command for the word code. For example, `r mean(myvector)` will insert the mean of the elements of myvector. But keep in mind that the default output includes a good number of digits, so you may need to round your output: e.g., `r round(mean(myvector), digits = 2)`.

R Markdown documents will fail to compile if the inline R code is incorrect, but the error messages are not always helpful in finding out what code is incorrect. So add inline code incrementally and compile frequently to check as you go. You can also test each individual inline code piece by highlighting the R syntax (without the `r ` part) and pressing Ctrl-Enter to run the code in the console.

Practice

Code chunks

Syntax review

Sometimes you need to run larger sections of code than a single R command. This is called a code chunk. Some R Markdown documents include all of the R code embedded in them, so there are a lot of code chunks. Other documents just source R scripts and use primarily inline code.

Code chunks start with ```{r} followed by the code on a new line and end with ``` on a new line:

```{r}
x <- 2 + 2
y <- 3 + 3
z <- x + y
```

You can control aspects of the chunk with options in the {r} component. You can name the code chunk for subsequent references (don't give two chunks the same name), and you can use arguments to control what the chunk does.

  • The eval argument controls whether the chunk is evaluated, meaning whether the code is passed to R to be run. For teaching purposes, you may want to show code in a chunk without running it, so you would set it to FALSE. By default, it is set to TRUE.
  • The echo argument controls whether the chunk's code is output to the document. Again, this is useful for teaching but perhaps not for actual documents unless you want to show the code.
  • The results argument controls whether the chunk's results are output.
  • The message and warning arguments control whether messages or warnings are output.

For example {r fig1, eval = TRUE, echo = TRUE, results = TRUE, message = FALSE} will name the chunk fig1, run the code, output the actual chunk code, output the results of the chunk, but suppress the messages produced by the chunks.

Practice

Quiz

Wrap-up

Congratulations, you finished the tutorial!

To get credit for this assignment, replace my name with the first name that you submitted in the course introduction form in the code below and click Run Code to generate the text for you to submit to Canvas.

# replace my name below with your first name (surrounded by quotes)
first_name <- "Jeff"
generate_text(first_name)

Assignment complete

Copy that code into Canvas, and you're all set for this tutorial.

R Markdown