I’ve had a couple of students bring me R programs that did not work. Looking at the output, I could not figure out why they did not work. It turns out that you have to look at the code itself and not the output. Let me show what happened using a very simple example.
Let’s suppose that I asked students in my Introduction to R class to write a program that prints ten random numbers from a standard normal distribution. You can do this with a single line.
rnorm(10)
So your student creates the following program.
---
title: "A tricky program to debug"
author: "Steve Simon"
output: html_document
---
```{}
rnorm(10)
```
and it produces the following output
So where are the ten random numbers? They didn’t print. Looking at the output by itself, you would not know why.
To understand what is going on here, you need to understand that RMarkdown (along with Jupyter and Quatro) allows you to run code from a variety of different languages. if you wanted to run Python code, you would enclose it in a program chunk that looks like
```{python}
# Insert Python code here
```
If you wanted to run Julia code, you would use
```{julia}
# Insert Julia code here
```
There are many other languages that you can insert here.
So what happens, when you forget to specify the language? What happens when you have a bare set of curly braces? What happens when you run the code shown below?
```{}
rnorm(10)
```
Well, a bare set of curly braces tells RMarkdown to display the text inside the chunk as is, without any changes to the spacing or line breaks. I use this construct all the time to display code with proper indenting but without any desire to try to run the code.
It is easy enough to forget the “r” inside the curly braces. When you put it back in (see below),
---
title: "A tricky program to debug"
author: "Steve Simon"
output: html_document
---
```{r}
rnorm(10)
```
it produces the following output
Voila! Problem solved.