vincentarelbundock · GitHub

@grantmcdermott and @etiennebacher,

Here’s a first attempt to resolve the tinytheme in a loop issue. The idea is:

  1. Whenever tpar() sets a hook, we save that hook to the .tinyplot environment.
  2. When tinytheme() resets the theme via init_tpar(), we retrieve the hooks from the .tinyplot environment and remove only the "before.plot.new" hooks that are identical() to those.

Implementation notes:

  1. I copied the set_hooks() and remove_hooks() functions from the evaluate package and made a few minor changes to them. That package is MIT and included a note at the top of the file.
  2. To make it easier to retrieve variables from the .tinyplot environment, I added a get_environment_variable() and set_environment_variable() functions.

The tests pass on my computer, and this seems to work.

I’m not exactly sure I know all the behaviors that were broken, so I’d appreciate if you could take this for a spin.

res <- evaluate::evaluate(function(){
    library(tinyplot)
    for (thm in c("dark", "minimal")) {
        tinytheme(thm)
        tinyplot(I(Sepal.Length * 1e4) ~ Petal.Length | Species, data = iris)
    }
})
sapply(res, class)
#> [1] "source"       "source"       "recordedplot" "recordedplot" "recordedplot"
#> [6] "recordedplot"

Read the original on github.com ↗