rmarkdown error "attempt to use zero-length variable name"

The problem could be due to the object being changed in the global environment in an earlier session and that session got saved in the global ennvironment. It is better to not save anything in the global environment, while ending the Rstudio session (or R console). One option would be to call the data(cars) again so that we get the original dataset

---
title: "Untitled"
output:
  html_document: default
  'html_document:': default
---

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

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

-output enter image description here

One option to avoid these kind of surprises is to use the "Don't save" option while quitting the session

enter image description here


For me the issue was that I had a missing backtick in the closing code block. In other words, it looked like the following (note that there are only two closing backticks, not three as there should be).

```{r}
# do some stuff
``

So the two backticks were being processed as part of the code block, which is legal code for supplying a variable name such as e.g.

`+`

But since no variable name was provided between the backticks, I was getting the "attempt to use zero-length variable name" error.


check the header's.. very top of the script / page..

--- 
title: "R Notebook" -->
author: "your_name_here" -->
date: "28/05/2022" -->
output: html_document --> 'notebook' returns 'zero-length' error..
---

from above, change...

output: html_notebook

...to...

output: html_document

Putting this as an answer for visibility: this happens if you try to run by selecting all in the Rmd and pressing enter like you would with a normal R script. RStudio tries to run this all as R code, including the markdown parts, leading to the errors you saw.

You can avoid this by running an individual chunk by clicking the green play button or by selecting one of the run options in the dropdown at the top of the Rmd editor.

Tags:

R

R Markdown