-1

I am trying to specify the size of a plot dynamically in RMarkdown. Because it is a facetted plot with a dynamic number of plots, the aspect ratio needs to change.

I'm using this code:

...
jobs_levels <- 9

```

#### Figure 3.1.1. Effect of Inidivdual Interventions in Living Rooms

```{r figure_3_3_1, fig.height=jobs_levels}

jobs_levels
print(figure_3_3_1)

```

For the sake of simplicity, I've hard coded the jobs_levels (the number of levels in the facetting factor). As you can see, I set the value very clearly,and then use it in the very next code chuck. I can see the value clear as day in the environment. It is 9. But I get this:

Error in eval(ele) : object 'jobs_levels' not found
> ```{r figure_3_3_1, fig.height=jobs_levels}
Error: attempt to use zero-length variable name

When I run this in batch mode, it crashes. Any idea what is going on with this?

I even put in the extra lines:

jobs_levels

to debug. EAch time I run one of those lines with cotrl enter, it evaluates right, but I see the error message again too...

monkey
  • 1,213
  • 2
  • 13
  • 35
  • based on your post, you are not defining `jobs_levels` in an r code chunk – rawr Aug 30 '21 at 05:02
  • Please provide a self-contained minimal [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) that we can copy/paste into R to test with. Are you saying `jobs_levels <- 9` in in the Rmd document? What exactly do you mean when you say you run it in "bactch mode" – MrFlick Aug 30 '21 at 05:12
  • How is `figure_3_3_1` defined? – Roland Aug 30 '21 at 05:47

1 Answers1

1

The error message makes it look as though you are trying to evaluate the chunk header as R code. You had

```{r figure_3_3_1, fig.height=jobs_levels}

The first two backticks would be parsed as a zero length variable name, as the error message states.

You can't run R Markdown code as R code, you need to use rmarkdown::render or a related function to run it.

user2554330
  • 37,248
  • 4
  • 43
  • 90
  • This *does* explain the error. So this was the combination of R's non-existent error reporting, a different unrelated error and the confusion between running the chunk and rendering it. – monkey Aug 30 '21 at 21:07
  • "Non-existent"? But it was the R error message that made the problem clear. – user2554330 Aug 31 '21 at 09:05
  • A metaphor: In literal terms, insufficient. – monkey Aug 31 '21 at 11:07