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
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.
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.
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
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
I got the same message, my error was to have four backtick in one of the block codes
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.
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.
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.
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
Related
Since I have some fairly large code chunks (plantuml charts, sql), I'd prefer to keep those as external files. I see in the knitr manual I can do something like code=readLines(...) to bring in outside sources.
When using external code this way, I'm seeing a difference in behavior between running the chunk in RStudio with the green play button, and knitting the code.
With these two files:
main.R:
---
title: "test"
author: "Me"
date: "6/17/2020"
output: html_document
---
```{r setup, include=FALSE}
library(here)
library(xfun)
```
```{r, code=xfun::read_utf8(here::here('test.R'))}
print('world')
```
and test.R:
print('hello')
when I knit, hello is printed. When I run the code chunks using run in RStudio, or restart R and run all chunks, then the preview says world. The print('world') here is just to illustrate that the inline code is being ran; in my case, the inline code would be empty.
I see in the comments that "Knitr will replace the code in the chunk with the code in the code option", but does that mean that separate chunks are not run by knitr by another environment? Can I do something to have the external code be executed by both rather than one executing the inline code and the other the external code?
Is it possible to use pandoc-crossref in bookdown?
I tried to change the yaml header to:
output:
bookdown::tufte_book2:
toc: yes
highlight: tango
pandoc_args: -F /usr/local/bin/pandoc-crossref
Which should pass the filter to pandoc, but I get the error:
pandoc: Error running filter pandoc-crossref:
Could not find executable ' pandoc-crossref'.
The above error does not make sense, as I entered the correct path. What kind of env is bookdown using, which is precluding the access to the filter file?
Here is an example
---
output: bookdown::html_document2
---
# Section name {#id}
```{r pressure, echo=FALSE, fig.cap='test plot'}
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. To cross-reference the figure, use `\#ref(fig:pressure)` to produce Figure \#ref(fig:pressure). All this is found within the section \#ref(id).
Which produces...
See https://bookdown.org/yihui/bookdown/figures.html for the official documentation.
I had a similar issue while I was trying to put numbers to equations in Word files (SO question). I got the same error Could not find executable 'pandoc-crossref'.
My RStudio installation (on Windows) did not come with pandoc-crossref. Here is what I did:
Downloaded pandoc-crossref from here.
Find the path where RStudio saved the file pandoc.exe:
rmarkdown::find_pandoc()
Put the pandoc-crossref.exe in the folder I got in (2).
Using the chunk option eval=FALSE one can suppress chunk evaluation in an RMarkdown file or R Notebook when it is knit. Is there a way to make this apply during interactive running of the document in RStudio (i.e., making "run all chunks" skip certain chunks)?
I've got some chunks in the beginning of my analysis that take a while to run, which the later sections don't depend on. I want to be able to source the important parts of the code so I can continue writing the downstream stuff, without having to do it manually chunk-by-chunk so I can avoid the parts I don't need in my workspace for further writing.
I've set up the rmarkdown document with logical parameters meant to change which parts of the code need to get run - I meant these as control flags for when the code is actually finished and getting used, but I was hoping I could use those same parameters to exclude chunks from running in interactive mode (i.e., something like eval=params$run_part1).
Setting knitr::opts_chunk and knitr::opts_hooks only help you when knitting, not in interactive mode, so while I could be wrong I'm going to tentatively say you can't control that behavior with dynamic chunk options (yet).
As a workaround you could use interactive() and if blocks so that the code is only run when knitted. It would also mesh well with your logical parameters, despite the pain of having to be in a bracket block.
---
title: "R Notebook"
output:
html_document: default
html_notebook: default
---
```{r}
if (!interactive()) {
print("long running code")
}
```
```{r}
print(2)
```
```{r}
print(3)
```
Pressing "Run All Chunks Above":
Knitting:
I am probably missing something simple here. When I run code in an R markdown code cell, it throws this error:
(*) NOTE: I saw chunk options " dataFrames, echo=TRUE eval=TRUE"
please go to http://yihui.name/knitr/options
(it is likely that you forgot to quote "character" options)
Here is the code from the cell that triggered the error:
```{r dataFrames, echo=TRUE eval=TRUE}
df1 <- data.frame(c(1, 2, 3),
c("R","S","T"),
c(TRUE, FALSE, TRUE))
```
I tried the link given in the error, but did not run across anything that explained the glitch for me. Can anyone spot it?
Just noticed what appears to be the problem. Forgot a comma between the last two arguments as in:
```{r dataFrames, echo=TRUE, eval=TRUE}
not:
```{r dataFrames, echo=TRUE eval=TRUE}
oops.
Adding this in from the comments: If anyone else comes across this, it can also be caused by saying something like out.width=50% instead of out.width='50%' (with quotes).
I just wanted to note that I also came across this issue when using a single apostrophe in the name of my chunk
i.e.:
{r Chronbach's Alpha} throws the error, but {r Chronbachs Alpha} does not.
I have a .Rpres file in RStudio. I would like to include code, but not have it run (I am only showing the code to explain how it works). Is it possible to accomplish this (and ensure that it will not produce errors, because it is not running)?
Have you tried eval=FALSE in the knitr code chunk options? e.g.:
```{r eval=FALSE}
print("Don't run me")
```
{r, eval=F, echo=T} will include the R source code in the output file while it is not evaluated
Posting for anyone who may come across this like I have. I've found that for small examples (if you don't want to use chunks), you can also just use back ticks like you would with regular markdown inline, but just don't add the "r" at the beginning:
`plot(cars)`
Will print the code itself, but will not print the plot.