Matthew May
Matthew May

Reputation: 113

I call render to convert R markdown to html, get a Scanner error

question

Hi,everyone.(^_^) I have created a new R markdown file(I have not change anything), save to workspace with a name "test1.Rmd" and then I call render("test1.Rmd").

Immediately, the console show me this error:

Error in yaml::yaml.load(string, ...) : Scanner error: while scanning a simple key at line 3, column 1 could not find expected ':' at line 4, column 1

so, what does that mean?(T_T) I have not changed anything yet.

result

I find a way to solve it:
click the knitr button to create a html,and close it. after that, I can call render() without any error. But I don't know why.

environment:

R version 3.4.2 (2017-09-28)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

reproducible example(add at 2018-01-06)

It's the orginal content of a new Rmd file, I have not changed anything. I save this rmd in the working space with a name "test1.Rmd" and call render("test1.Rmd"). And get the error every time.

---
title: "Untitled"
author: "may"
date: "2018年1月6日"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

(add at 2018-1-7)
I call rmarkdown::render("test1.Rmd") from RStudio on the command line.
I have set Tools->Options->Code->Saving->Default text encoding = UTF-8 when the first time I use RStudio.

Upvotes: 1

Views: 1261

Answers (1)

jimjamslam
jimjamslam

Reputation: 2067

I'm having trouble testing this, because your example works on my Mac, but going by the error and your given example, I'm willing to bet that this is a text encoding problem.

The Japanese/Chinese date characters on line three ("2018年1月6日") are what's tripping it up. They're Unicode characters. Knitr should, to my knowledge, handle them fine (I was able to render your example out to Markdown on my Mac without a problem), but if you're on Windows it's possible that something is causing it to choke.

When you save your RMarkdown file, what program are you using? If it's Notepad (the default Windows editor), are you saving it as UTF-8 (a Unicode-compatible encoding)? Notepad saves in ASCII—a subset that only contains Western characters—by default, and I'm not sure whether knitr will be able to handle those date characters if it thinks the input is ASCII. If you can verify that you're saving the file in UTF-8 (using the dropdown at the bottom of the Save dialog in Notepad, shown below), that'll help us cross some possibilities off 🙂

enter image description here

If you are indeed using UTF-8, it could be that knitr needs guidance to interpret the Unicode characters on Windows. You could also try adding the argument encoding='UTF8' to knit(), like:

knitr::knit('myfile.rmd', encoding = 'UTF8')

Let me know how you go 🙂

EDIT: looks like specifying the encoding in knit() worked for you!

Upvotes: 0

Related Questions