<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/gifski-on-cran-the-fastest-gif-encoder-in-the-universe/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://ropensci.org/technotes/2018/07/23/gifski-release/"> rOpenSci - open tools for open science</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p> <img alt="cover image" src="https://i0.wp.com/gif.ski/demo.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<p>The <a rel="nofollow" href="https://cloud.r-project.org/web/packages/gifski/index.html" target="_blank">gifski</a> package which was demonstrated in May at <a rel="nofollow" href="https://2018.erum.io/" target="_blank">eRum 2018</a> in Budapest is now on CRAN. Gifski is a simple but powerful package which can hopefully take away an important performance bottleneck for generating animated graphics in R.</p>
<h2>What is Gifski</h2>
<p>Gifski is a multi-threaded high-quality GIF encoder written in Rust. It can create animated GIF images with thousands of colors per frame and do so much faster than other software. The <a rel="nofollow" href="https://gif.ski/" target="_blank">Gifski Website</a> has more technical details and beautiful examples.</p>
<p>The R package wraps the Rust crate and can be installed in the usual way from CRAN. One of the major benefits of Rust is that it has no runtime, so the R package has no dependencies.</p>
<pre><code>install.packages(&quot;gifski&quot;)
</code></pre>
<p>On Linux you need to install <code>cargo</code> to compile the rust code, but the package does not require any external libraries. Cargo automatically compiles and links all Rust code when building the R package. If you are on MacOS, try installing from source to see how it works:</p>
<pre><code>install.packages(&quot;gifski&quot;, type = &quot;source&quot;)
</code></pre>
<h2>How to Use</h2>
<p>The R interface is very simple: either generate a GIF from a set of images, or directly from the R graphics device. The <code>?gifski</code> manual page contains example of both. The <code>gifski()</code> function combines a set of PNG images into a single animated GIF file:</p>
<pre><code># Convert png files to gif
png(&quot;frame%03d.png&quot;)
par(ask = FALSE)
for(i in 1:10)
  plot(rnorm(i * 10), main = i)
dev.off()
png_files &lt;- sprintf(&quot;frame%03d.png&quot;, 1:10)
gif_file &lt;- tempfile(fileext = &quot;.gif&quot;)
gifski(png_files, gif_file)
utils::browseURL(gif_file)
</code></pre>
<p>Alternatively the <code>save_gif()</code> function captures plots R generated in an R expression and saves them as a animated gif, just like e.g. <code>animation::saveGIF()</code> but much faster:</p>
<pre><code># Example borrowed from gganimate
library(gapminder)
library(ggplot2)
makeplot &lt;- function(){
  datalist &lt;- split(gapminder, gapminder$year)
  lapply(datalist, function(data){
    p &lt;- ggplot(data, aes(gdpPercap, lifeExp, size = pop, color = continent)) +
      scale_size(&quot;population&quot;, limits = range(gapminder$pop)) + geom_point() + ylim(20, 90) +
      scale_x_log10(limits = range(gapminder$gdpPercap)) + ggtitle(data$year) + theme_classic()
    print(p)
  })
}

# High Definition images:
gif_file &lt;- save_gif(makeplot(), width = 800, height = 450, res = 92)
utils::browseURL(gif_file)
</code></pre>
<p><img alt="gapminder" src="https://i0.wp.com/i.imgur.com/Uwz4Fyi.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Gifski shows a progress meter while generating the GIF. Running this example shows that the GIF encoding is no longer a serious overhead: time spent in encoding is only a small fraction of the total time to generate the plot. Hopefully this will make it easier to generate animations with hundreds or even thousands of frames using for example the <a rel="nofollow" href="https://github.com/thomasp85/gganimate" target="_blank">gganimate</a> package.</p>
<h2>More about Rust in R</h2>
<p>This is the first CRAN package that interfaces a Rust library. In this case the R package itself does not contain any Rust code because we can call Rust directly from C.</p>
<p>If you are interested in learning more about using Rust in R packages, have a look at my <a rel="nofollow" href="https://jeroen.github.io/erum2018/" target="_blank">slides</a> from eRum 2018. Also see the <a rel="nofollow" href="https://github.com/r-rust/" target="_blank">r-rust</a> organization on Github for more examples R packages, especially the <a rel="nofollow" href="https://github.com/r-rust/hellorust#readme" target="_blank">hellorust</a> package.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://ropensci.org/technotes/2018/07/23/gifski-release/"> rOpenSci - open tools for open science</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/gulxx-wVFsk" height="1"></a></div>