rmarkdown error “attempt to use zero-length variab

2020-08-10 07:51发布

问题:

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

回答1:

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.



回答2:

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

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



回答3:

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.



回答4:

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.



标签: r r-markdown