R Markdown YAML "Scanner error: mapping values..."
Asked Answered
A

7

26

I have noticed this issue when knitting all file types (html, pdf, word). To make sure there's not an issue specific to my program, I went ahead and ran the default .rmd file you get when you create a new markdown. In each case, it does knit correctly, but I always see this at the end. I have searched online and here but cannot seem to find an explanation

Error in yaml::yaml.load(string, ...) : 
  Scanner error: mapping values are not allowed in this context at line 6, column 19
Error in yaml::yaml.load(string, ...) : 
  Scanner error: mapping values are not allowed in this context at line 6, column 19
Error in yaml::yaml.load(string, ...) : 
  Scanner error: mapping values are not allowed in this context at line 4, column 22

Here is my default YAML

---
title: "Untitled"
author: "Scott Jackson"
date: "April 20, 2017"
output: word_document
---

Line 4, column 22 is the space between the 7 and " I'm not sure where Line 6, column 19 is, but that line is the dashes at the bottom

Any ideas?

Thank you.

Amandaamandi answered 20/4, 2017 at 18:45 Comment(7)
I can only guess. There might be some spaces in any of those lines that dont belong there?Bullshit
Do you get this error running the default markdown file (if you do use markdown)?Amandaamandi
No. As I said, I think I had this error once when I had unnecessary spaces in my YAML.Bullshit
I get this error whenever I open a new RStudio session.Asuncionasunder
I had this because of a space in my style file.Albie
I had this because I wasn't wrapping by title with quotation marks. That worked fine until I added a colon in the title. Then I started getting that error, which was solved by putting the document title between quotes.Chocolate
I also receive this same error. I wrote: title:"Untitled" without space between title: and "Untitled". I fix this error when i wrote title: "Untitled" with space.Geochemistry
H
23

I get this error when trying to add a table of contents to the YAML:

title: "STAC2020 Data Analysis"
date: "July 16, 2020"
output: html_notebook:
  toc: true

However, if I put html_notebook: on to a separate line then I don't get the error:

title: "STAC2020 Data Analysis"
date: "July 16, 2020"
output:
  html_notebook:
    toc: true

I do not know why this formatting makes a difference, but it allowed my document to knit and with a table of contents.

Handel answered 16/7, 2020 at 18:10 Comment(1)
frustrating because the "wrong" example you posted is basically lifted straight from the RMarkdown cheat sheetMale
B
21

I realize this question has gone unanswered for awhile, but maybe someone can still benefit. I had the same error message and I realized I had an extra header command in my yaml. I can't reproduce your exact error, but I get the same message with different line/column references with:

---
title: "Untitled"
author: "Scott Jackson"
date: "April 20, 2017"
output: output: word_document
---

Error in yaml::yaml.load(string, ...) : 
  Scanner error: mapping values are not allowed in this context at line 4, column 15
Calls: <Anonymous> ... parse_yaml_front_matter -> yaml_load_utf8 -> <Anonymous>
Execution halted

Line 4 column 15 seems to refer to the second colon after the second "output".

Bioclimatology answered 4/9, 2018 at 21:49 Comment(1)
Yet another example of a thoroughly unhelpful and misleading error message! It seems to mean "Something wrong with your YAML, but don't know what".Trawler
A
8

I received this error when there was an indentation in the wrong place:

For example, the indentation before header-includes as seen in the example code below caused the error

---
title: "This is a title"
author: "Author Name"
   header-includes:
.
.
.
---

When you remove the indentation, the following code below did not produce the error:

---
title: "This is a title"
author: "Author Name"
header-includes:
.
.
.
---
Admix answered 8/3, 2019 at 19:13 Comment(0)
P
1

Similarly to Tim Ewers I also got this error when I added a TOC to the YAML:

title: "My title"
date: "April 1, 2020"
output:
  pdf_document: default 
    toc: true
  html_document: paged

However, the solution I found was to remove "default", this allowed me to knit the document without an error:

title: "My title"
date: "April 1, 2020"
output:
  pdf_document:
    toc: true
  html_document: paged
Phantom answered 3/2, 2021 at 10:13 Comment(0)
F
1

I know this is a 5 year old question but I just got this same error as I was missing a colon

---
title: ''
output: 
  pdf_document
    includes: 
      before_body: before_body.tex
---

should have been

---
title: ''
output: 
  pdf_document:
    includes: 
      before_body: before_body.tex
---

and while that doesn't strictly answer the example given, I hope it will help future sufferers of this error message.

Fishback answered 14/11, 2022 at 15:25 Comment(0)
W
0

I guess this error happens on your content instead of your yaml block. Because there is no extra content display so I will give a minimal example.

> library(yaml)
> library(magrittr)
> "
+ ---
+ title: 'This is a title'
+ output: github_document
+ ---
+ 
+ some content
+ " %>% 
+     yaml.load()
$title
[1] "This is a title"

$output
[1] "github_document"

It works well. And here is another example.

> "
+ ---
+ title: 'This is a title'
+ output: github_document
+ ---
+ 
+ some content
+ some content: some content
+ " %>% 
+     yaml.load()
Error in yaml.load(.) : 
  Scanner error: mapping values are not allowed in this context at line 8, column 13

The errors happens at line 8. Because there is a key-value pair not at yaml block. yaml.load is not enough smart for me. The temporal solution for me is just extract all lines above the second ---.

> text <- "
+ ---
+ title: 'This is a title'
+ output: github_document
+ ---
+ 
+ some content
+ some content: some content
+ "
> library(xfun)
> read_lines(text,n_max = 5) %>% 
+     yaml.load()
$title
[1] "This is a title"

$output
[1] "github_document"
Wetmore answered 21/1, 2019 at 3:33 Comment(0)
O
0

I also got the error when I had a colon in my title--

Failed

title: Bad: Title
output: html_document

Fixed

title: Good Title
output: html_document
Orient answered 20/12, 2023 at 14:49 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Aside

© 2022 - 2024 — McMap. All rights reserved.