EvaMaeRey / flipbookr

Presenting code step-by-step and side-by-side with its output
https://evamaerey.github.io/flipbookr/
Other
198 stars 20 forks source link

gif of flipbook via flipbookr + pagedown + magick #22

Closed brshallo closed 4 years ago

brshallo commented 4 years ago

After adding in the css noted in #21 , the following function should create a gif (as well as html and pdf outputs) of your flipbook via flipbookr --> pagedown --> magick.

flipbook_to_gif <- function(flipbook_filepath_rmd, 
                            density = 100,
                            delay = 1){

  filepath <- fs::path_ext_remove(flipbook_filepath_rmd)
  filepath_pdf <- fs::path_ext_set(filepath, "pdf")
  filepath_gif <- fs::path_ext_set(filepath, "gif")

  pagedown::chrome_print(flipbook_filepath_rmd)
  images <- magick::image_read_pdf(filepath_pdf, density = density)

  magick::image_write_gif(images,
                          filepath_gif,
                          delay = delay)
}

See notes and reproducible examples at brshallo/flipbookr-gifs-examples.

E.g. now below is built through flipbookr rather than flair (per recommendation of @kbodwin )

brshallo commented 4 years ago

The pagedown::print_chrome() step acts funny when called in a function environment (seems to not always work when flipbook has objects other than what is being revealed). Have not tested whether this is specific to flipbookr or a general problem with pagedown.

However the following modification seems to fix things so that pagedown::print_chrome(flipbook_filepath) can more generally run within a function:

flipbook_to_gif <- function(flipbook_filepath, 
                            density = 100,
                            delay = 1,
                            env_pdf = rlang::caller_env()){

  filepath <- fs::path_ext_remove(flipbook_filepath)
  filepath_pdf <- fs::path_ext_set(filepath, "pdf")
  filepath_gif <- fs::path_ext_set(filepath, "gif")

  env_pdf$flipbook_filepath <- flipbook_filepath
  with(env_pdf, pagedown::chrome_print(flipbook_filepath))

  images <- magick::image_read_pdf(filepath_pdf, density = density)
  magick::image_write_gif(images,
                          filepath_gif,
                          delay = delay)
}

I updated the source code at the link above and also added a third example that runs this on the "minimal-flipbook.Rmd" (so can see it looks pretty good outside of just simple flipbookr::chunk_reveal() examples).

EvaMaeRey commented 4 years ago

There's now guidance about how to create a gif preview in the template.