27

When i generate a new rmarkdown file (or open existing rmarkdown-files) and try to run a rmarkdown chunk, i get this error: "Error: attempt to use zero-length variable name". I have Win10 and did a fresh install of R and Rstudio yesterday. What did i miss? Where does this error come from?

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

```{r cars} Error: attempt to use zero-length variable name

enter image description here

SEMson
  • 1,285
  • 3
  • 20
  • 25
  • Not able to reproduce the problem using `knitr_1.17` and windows 10 – akrun Sep 12 '17 at 08:23
  • I would have quessed so. The code is the default-code from any new rmarkdown-file and until yesterday, it worked well on my machine. I have no explanation where this error comes from. – SEMson Sep 12 '17 at 08:24
  • Do you save the global environment when you log out. Possibly, it could pollute the env. Try `data(cars)` and then execute the `summary(cars)` – akrun Sep 12 '17 at 08:25
  • R u saying that when you used `data(cars)`, it is working? In that case, it could be a problem of saving in the global env. – akrun Sep 12 '17 at 08:29

9 Answers9

50

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.

Donovan192
  • 658
  • 1
  • 8
  • 13
  • 5
    ctrl+shift+enter (cursor positioned anywhere within the chunk) will run the entire code chunk if saved as .Rmd first. – val Oct 16 '18 at 17:46
10

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.

dpritch
  • 1,149
  • 13
  • 15
7

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

akrun
  • 874,273
  • 37
  • 540
  • 662
  • 2
    Thanks. I just realized, that it appears, if i select the lines and run them, but it doesnt, if i click the green arrow to run the current chunk. – SEMson Sep 12 '17 at 09:42
  • 1
    This was the solution that worked for me; slightly different symptom, but similar error. – RichardBJ Nov 25 '22 at 11:23
6

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
rdfleay
  • 71
  • 1
  • 7
1

I got the same message, my error was to have four backtick in one of the block codes

1

I had the same error. The problem was that comments within the code chunk had missing '#´s. The particular code chunk partially executed, showing the results and a syntax warning (but the markdown did not knit) What happened what that I used the keyboard short cut of 'CTRL+AlT+I" to insert a code chunk and it 'swallowed' the comments. A sneaky error to be sure.

0

I had the following code in my R Studio to suppress some warnings. What I wanted to do was hide all possible output but still evaluate the code, e.g., hide text output (results='hide'), hide warnings, hide messages

```{r message=FALSE, warning=FALSE, results='hide'}

When I got rid of those lines, I stopped getting the error as well.

Hope that helps.

I still get the same error, could you help? I am trying to copy the code for Iran from https://github.com/timchurches/blog/blob/master/_posts/2020-02-18-analysing-covid-19-2019-ncov-outbreak-data-with-r-part-1/analysing-covid-19-2019-ncov-outbreak-data-with-r-part-1.Rmd.

newb2019
  • 3
  • 2
Amit
  • 559
  • 5
  • 8
0

In my case this issue was caused by copying the sequence "''' {r}" into the R studio document. After manually re-entering this phrase, everything worked fine and I successfully generated a markdown file.

Marius
  • 1
-1

This approach involves selecting (highlighting) the R code only (summary(pressure)), not any of the backticks/fences from the code chunk. (If you see Error: attempt to use zero-length variable name it is because you have accidentally highlighted the backticks along with the R code........source: https://rstudio-conf-2020.github.io/r-for-excel/rstudio.html