I want to set the fig.height in an RMD chunk based in the number of rows in the data. For this, I have just set
{r, fig.height = nrow(data)}
but this doesn't change the figure height at all. However, it works when I set
{r, fig.height = 25}
where 25 is the number of rows in the data. I wonder why that is since they should be the same thing, right? I need the fig.height to be set dynamically with nrow since I have multiple datasets with different number of rows. I've even tried setting first
height <- nrow(date)
in another chunk and then using it as the fig.height but this doesn't work either.
Using some toy data, here is a reproducible example.
pl <- mpg %>%
ggplot(aes(x=class)) %>%
geom_bar()
Printing the plot in a separate chunk with {r, fig.height = length(unique(mpg$class))} produces a plot with a different height than setting {r, fig.height = 7} so the problem is not only for nrow() for me. Using {r, fig.height = length(unique(mpg$class))} looks the same as when not setting any height.