Fix the 'non-numeric argument to binary operator' Error in R Markdown and Quarto

Nan Xiao June 6, 2023 1 min read
Rubber duck debugging. Photo by Timothy Dykes.
Rubber duck debugging. Photo by Timothy Dykes.

If Google leads you here, you probably assigned a character value to a knitr chunk option such as fig.width or fig.height that only accepts numeric values, and your are trying to render that R Markdown or Quarto document.

The exact error message you encountered looks like this:

Error in `options[[sprintf("fig.%s", i)]] * options$dpi`:
! non-numeric argument to binary operator
Backtrace:
  1. rmarkdown::render("example.Rmd", encoding = "UTF-8")
  2. knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet)
  3. knitr:::process_file(text, output)
  8. knitr:::process_group.block(group)
  9. knitr:::call_block(x)
 10. knitr:::fix_options(params)
 11. options[[o <- sprintf("out.%s", i)]] %n% ...

This is because it is easy to make this mistake:

```{r, fig.width="100%"}
plot(1:10)
```

where you probably meant:

```{r, fig.width=6, out.width="100%"}
plot(1:10)
```

That’s all! I hope this helps.