<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/using-magick-with-rmarkdown-and-shiny/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://ropensci.org/technotes/2017/11/07/magick-knitr/"> 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/i.imgur.com/tTFk7ig.jpg?w=456&ssl=1" data-recalc-dims="1"></p>
<p>This week <a rel="nofollow" href="https://cran.r-project.org/web/packages/magick/vignettes/intro.html" target="_blank">magick</a> 1.5 appeared on CRAN. The latest update adds support for using images in knitr documents and shiny apps. In this post we show how this nicely ties together a reproducible image workflow in R, from source image or plot directly into your report or application.</p>
<pre><code>library(magick)
stopifnot(packageVersion('magick') >= 1.5)
</code></pre>
<p>Also the magick <a rel="nofollow" href="https://cran.r-project.org/web/packages/magick/vignettes/intro.html" target="_blank">intro vignette</a> has been updated in this version to cover the latest features available in the package.</p>
<h2>Magick in Knitr / RMarkdown Documents</h2>
<p>Magick 1.5 is now fully compatible with knitr. To embed magick images in your rmarkdown report, simply use standard code chunk syntax in your <code>Rmd</code> file. No special options or packages are required; the image automatically appears in your documents when printed!</p>
<pre><code># Example from our post last week
image_read('logo:') %>%
  image_convolve('DoG:0,0,2') %>%
  image_negate() %>%
  image_resize(&quot;400x400&quot;)
</code></pre>
<p><img alt="fig1" src="https://i2.wp.com/i.imgur.com/PhwCJ4k.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<p>You can also combine this with the magick graphics device to post process or animate your plots and figures directly in knitr. Again no special packages or system dependencies are required.</p>
<pre><code># Produce graphic
fig &lt;- image_graph(width = 800, height = 600, res = 96)
ggplot2::qplot(factor(cyl), data = mtcars, fill = factor(gear))
invisible(dev.off())

print(fig)
</code></pre>
<p><img alt="fig2" src="https://i1.wp.com/i.imgur.com/zFLcHws.png?w=456&ssl=1" data-recalc-dims="1"></p>
<pre><code>
# Some post-processing
frink &lt;- image_read(&quot;https://jeroen.github.io/images/frink.png&quot;)

fig %>%
  image_rotate(10) %>%
  image_implode(.6) %>%
  image_composite(frink, offset = &quot;+140+70&quot;) %>%
  image_annotate(&quot;Very usefull stuff&quot;, size = 40, location = &quot;+300+100&quot;, color = &quot;navy&quot;, boxcolor = &quot;pink&quot;)
</code></pre>
<p><img alt="fig3" src="https://i1.wp.com/i.imgur.com/0E5cqaz.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Same works for animation with <code>image_animate()</code>; the figure shows automatically up in the report as a gif image:</p>
<pre><code>image_read(&quot;https://jeroen.github.io/images/banana.gif&quot;) %>%
  image_apply( function(banana){
    image_composite(fig, banana, offset = &quot;+200+200&quot;)
  }) %>%
  image_resize(&quot;50%&quot;) %>%
  image_animate()
</code></pre>
<p><img alt="fig4" src="https://i0.wp.com/i.imgur.com/mi67gjt.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<p>The magick vignette <a rel="nofollow" href="https://raw.githubusercontent.com/ropensci/magick/master/vignettes/intro.Rmd" target="_blank">source code</a> is itself written in Rmarkdown, so it’s great example to see this in action. Try rendering it in RStudio to see how easy it is!</p>
<h2>Magick in Shiny Apps</h2>
<p>While we’re at it, several people had asked how to use magick images in shiny apps. The easiest way is to write the image to a <code>tempfile()</code> within the <code>renderImage()</code> callback function. For example the server part could look like this:</p>
<pre><code>output$img &lt;- renderImage({
    tmpfile &lt;- image %>%
      image_resize(input$size) %>%
      image_implode(input$implode) %>%
      image_blur(input$blur, input$blur) %>%
      image_rotate(input$rotation) %>%
      image_write(tempfile(fileext='jpg'), format = 'jpg')

  # Return a list
  list(src = tmpfile, contentType = &quot;image/jpeg&quot;)
})
</code></pre>
<p>Below is a simple shiny app that demonstrates this. Have a look at the <a rel="nofollow" href="https://github.com/jeroen/shinymagick/blob/master/app.R" target="_blank">source code</a> or just run it in R:</p>
<pre><code>library(shiny)
library(magick)
runGitHub(&quot;jeroen/shinymagick&quot;)
</code></pre>
<p><a rel="nofollow" href="https://jeroen.shinyapps.io/shinymagick" target="_blank"><img alt="tigrou" src="https://i0.wp.com/i.imgur.com/tTFk7ig.jpg?w=456&ssl=1" data-recalc-dims="1"></a></p>
<p>Perhaps there’s an even better way to make this work by wrapping magick images into an htmlwidget but I have not figured this out yet.</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/2017/11/07/magick-knitr/"> 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/uYaqEkvAzYo" height="1"></a></div>