What this error means
In R, it's possible to create variables whose names contain spaces, punctuation, parentheses, etc. To do this, we use backticks (`
) around the variable name:
> `variable with space and special characters in the name (??!!)` <- 1
> print(`variable with space and special characters in the name (??!!)`)
[1] 1
However, there is a constraint: the variable name must contain at least one character:
> `` <- 1
Error: attempt to use a variable name with zero length
Since `
is also used a lot in Markdown, this can lead to confusing situations. Let's examine some cases.
Trying to run the ```{r}
line
If you're positioned on the ```{r}
line and press Ctrl+Enter, you'll receive this same error message. Just move the cursor to a line within the code block that you want to run.
{r}
"tag" not added
You can get this error if the {r}
tag is forgotten after the initial ```
. For example, if you have the block above and press Ctrl+Enter, you will get the error:
```
a <- 1
```
If you change it as shown below, it works:
```{r}
a <- 1
```
I'm not totally sure what happens at the parsing level to lead to this error... But happens so often and so consistently that it deserves a mention here.
Forgetting a backtick at the end ```
If you miss a backtick at the end of the block, as shown below...
```{r}
a <- 1
``
... and you execute Run current chunk
, this error will occur.
Trying to run an Rmarkdown file as an R script
Rmarkdown files include documentation in addition to code. If you try to execute them as if they were had code onlu and, for example, there's a block at the beginning of the file, this error message might appear. This can happen if you attempt to run something like R file.Rmd
from the terminal or select the entire content of an article in RStudio and press Ctrl+Enter.
These are just a few examples. The possible causes are numerous! In some cases, it might even be bugs in a particular package. Even so, having these cases in mind might help to clarify what's going on.
knitr_1.17
and windows 10 – Calldata(cars)
and then execute thesummary(cars)
– Calldata(cars)
, it is working? In that case, it could be a problem of saving in the global env. – Call