Skip to content

When in knitting context, current chunk option should be used and not global one #485

@cderv

Description

@cderv

first reported at quarto-dev/quarto-cli#6643

Same example as #484 but trying to override global dev by in-chunk dev, while using animate()

---
title: "Label shouldn't be named"
output: 
  html_document:
    dev: svg
---

```{r}
library(gganimate)
```

```{r, dev = "png"}
p <- ggplot(mtcars, aes(factor(cyl), mpg)) + 
  geom_boxplot() + 
  transition_states(gear)
animate(p)
```

Traceback is

Quitting from lines 13-17 [unnamed-chunk-2] (test.Rmd)
                                                                                                            
Error in `device()`:
! arguments inutilisés (units = "in", res = 96)
Backtrace:
 1. gganimate::animate(p)
 2. gganimate:::animate.gganim(p)
 4. gganimate (local) `<fn>`(...)
Exécution arrêtée

knit_print method is not used in this case, and animate try to deal with knitr context itself. Though it is using opts_chunk$get()

chunk_args <- if (is_knitting()) get_knitr_options(knitr::opts_chunk$get(), unlist = FALSE) else list(dev_args = list())

This means only global option will be used. Hence same error as in #484 because dev: svg from YAML field is used, setting device to svg()

opts_current$get() should be used to get options including modify ones by current chunk level options

---
title: "Label shouldn't be named"
output: 
  html_document:
    dev: svg
---

```{r}
library(gganimate)
```

```{r, dev = "png"}
knitr::opts_chunk$get("dev")
knitr::opts_current$get("dev")
```

image

I believe this should fix it

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions