tmap icon indicating copy to clipboard operation
tmap copied to clipboard

tm_animate does not return anything for rendered documents (e.g., rmd, qmd, reprex)

Open Nowosad opened this issue 6 months ago • 11 comments

It would be great if it would render the animation directly in the qmd/rmd output.

library(tmap)
library(spData)

tm_shape(nz) +
  tm_polygons() +
  tm_animate("Name")

Nowosad avatar Jun 25 '25 14:06 Nowosad

Yes, that would be great.

I don't know how. Normally the animation should be printed over here https://github.com/r-tmap/tmap/blob/master/R/tmap_animation.R#L174-L195

It does go there, but somehow nothing happens.

mtennekes avatar Jun 25 '25 15:06 mtennekes

Maybe related to https://github.com/r-tmap/tmap/issues/989 ? Any idea @olivroy or @tim-salabim ?

mtennekes avatar Jun 25 '25 15:06 mtennekes

Possibly gganimate could serve as an inspiration with its knit_print.gganim(x, options, ...) function (https://gganimate.com/reference/animate.html)

library(ggplot2)
library(gganimate)

ggplot(mtcars, aes(factor(cyl), mpg)) +
  geom_boxplot() +
  # Here comes the gganimate code
  transition_states(
    gear,
    transition_length = 2,
    state_length = 1
  ) +
  enter_fade() +
  exit_shrink() +
  ease_aes('sine-in-out')

Nowosad avatar Jun 27 '25 10:06 Nowosad

Hopefully, it works now, please test.

mtennekes avatar Jul 01 '25 07:07 mtennekes

Works -- see https://tmap.geocompx.org/animations

Nowosad avatar Jul 01 '25 08:07 Nowosad

There is still a minor issue: the animations do not render correctly when the quarto chunk has two (or more) animations at the same time.

See the code at https://github.com/geocompx/tmap/blob/15ceb827dd8303d5a4c1526642b4dd36f29dc4ba/animations.qmd#L42-L53 and the outcome at https://tmap.geocompx.org/animations#fig-anims.

Nowosad avatar Jul 07 '25 09:07 Nowosad

Not sure if multiple animations in one chunk are supported (?) I basically took the code from gganimate and put it here to make it work. If gganimate works, then we can check where the difference comes from.

mtennekes avatar Jul 07 '25 09:07 mtennekes

@mtennekes I confirm that this issue also applies to gganimate.

I do not know if this is helpful, but when the quarto code chunk layout has two or more figures, then they are saved independently with numeric suffixes (i.e., -1, -2, etc. see https://github.com/geocompx/tmap/blob/main/figures/fig-mproj-ortho-1.png and https://github.com/geocompx/tmap/blob/main/figures/fig-mproj-ortho-2.png). However, for the animation the second gif overwrites the first one using the -1 suffix -- see https://github.com/geocompx/tmap/blob/main/figures/fig-anims-1.gif (it should be fig-anims-1.gif and fig-anims-2.gif).

Nowosad avatar Jul 07 '25 10:07 Nowosad

Have you noticed if this was a problem before. evaluate has been re-written last year. https://evaluate.r-lib.org/news/index.html#evaluate-100 and multiple bug fixes releases were done since then. I wonder if this reproduces with evaluate 0.24 and knitr 1.47.

Maybe we could open an issue on the gganimate side to see if this is something they noticed,

There is also this Open evaluate PR that may solve the issue at the end?

olivroy avatar Jul 08 '25 14:07 olivroy

@olivroy evaluate 0.24 and knitr 1.47 -- the issue persists.

Nowosad avatar Jul 15 '25 13:07 Nowosad

@olivroy that open pull request is also not helping here.

@olivroy -- any suggestions for the next step? I could open an issue, but I am unsure where (gganimate repo, quarto repo,etc.)

Nowosad avatar Jul 15 '25 13:07 Nowosad