When inserting chunks they don't have the green arrow, and can't be run

Hi,

I can not run any of the chunks that I have created, I figured it is because they are written incorrect, since they do not have the green arrow, but cannot figure out how to get the green arrow. This is done in Rnotebook, in Rstudio.

One of the chunks looks like this:

```{r}
mu = n*a/N

"sigma^2" = (n*a*(N-a)*(N-n))/(N^2)*(N-1)
```

When trying to preview the file I get the following error message:

Fejl i yaml::yaml.load(..., eval.expr = TRUE) :
Scanner error: mapping values are not allowed in this context at line 2, column 22

Thanks in advance for your help!

Greetings,
Caro

Sounds like there is an issue with your YAML front-matter. Can you share more of your document, especially the line that contains the error?

Well the line 2 which is mentioned in the error is as follows:

title: "Lecture 2"

And it is part of the start of the document, which looks like this

title: "Lecture 2"
output: html_document:
toc: true
toc_float: true

White-space matters when it comes to YAML, so it seems like a white-space issue. But it is hard to tell with how your post is formatted. Can you please copy and paste your entire YAML front-matter verbatim and paste it inside code fences (three back-ticks) like this:

```
---
title: "This is my title"
---
```

If your above post is formatted as it is actually found in your Rmd file then you definitely have some whitespace issues. html_document: should be on its own line with two spaces before it, and both toc: true and toc_float: true should be below that and with four spaces. Like this:

---
title: "Lecture 2"
output: 
  html_document:
    toc: true
    toc_float: true
---

html_document is an argument to output so it needs to be nested inside output. Similarly, toc and toc_float are arguments to html_document so they need to be nested inside that. Nesting is done by white-space/tabbing in YAML.

I have tried to incorporate the corrections you mentioned, so now it looks like this

---
title: "Lecture 2"
output: 
  html_document:
    toc: true
    toc_float: true
    
### Exercise 2.1
# Comment on the exercise
"```{r}"
2+2
"```"
---

I made quotation marks around the chunk, since it would not show up correctly otherwise. So the quotation marks are not part of the real document.
I hope this is better to read. I am quite new to R and Rstudio, so I do very much appreciate your help :blush:

You can use four back-ticks (rather than 3) to be able to show code chunks on this website. For example, the below text is wrapped in four back-ticks.

---
title: "Lecture 2"
output: 
  html_document:
    toc: true
    toc_float: true
    
### Exercise 2.1
# Comment on the exercise
```{r}
2+2
```
---

Anyway, the issue is that only options related to how the document should be rendered should be included in the YAML header. The YAML header is defined by the three slashes (---). Any prose or code needs to come afterward. Try the text/code below in your Rmd file:

---
title: "Lecture 2"
output: 
  html_document:
    toc: true
    toc_float: true
---

### Exercise 2.1
# Comment on the exercise
```{r}
2+2
```

The play button and other chunk features should now appear, and you should be able to render your document.

2 Likes

Thank you very much, that fixed the problem!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.