<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/how-to-write-pelican-blog-posts-using-rmarkdown-knitr/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://michaeltoth.me/how-to-write-pelican-blog-posts-using-rmarkdown-knitr.html"> Michael Toth's Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In this post I’m going to be talking about how to easily modify your Pelican blog configuration to let you directly publish blog posts using RMarkdown. I’m assuming you already have a Pelican blog set up, so I won’t be covering that in today’s post. If you’re interested but haven’t yet set up a blog for yourself, it’s quite straightforward! I recommend checking out these links:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="http://docs.getpelican.com/en/stable/quickstart.html">Official Pelican Guide</a></li>
<li><a target="_blank" rel="nofollow" href="http://duncanlock.net/blog/2013/05/17/how-i-built-this-website-using-pelican-part-1-setup/">Detailed Pelican Setup by Duncan Lock</a></li>
</ul>
<p>Until now, I’ve been writing posts on this blog using standard markdown. This means I’d do an analysis in R, produce a series of graphs and results that I would store locally in image files, and put it all together on my own in a markdown document. It’s not that bad a process, but it is a bit inefficient, and I wanted to see if there was a better way. Luckily, there’s a very easy-to-use Pelican plugin called rmd_reader that will automatically convert any RMarkdown posts you have into Pelican-compliant html documents. In figuring out how to set this up, I drew heavily on these resources:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="https://github.com/getpelican/pelican-plugins/tree/master/rmd_reader">rmd_reader Plugin on Github</a></li>
<li><a target="_blank" rel="nofollow" href="https://rjweiss.github.io/articles/2014_08_25/testing-rmarkdown-integration/">rmd_reader Setup Tutorial by Rebecca Weiss</a>
</li>
</ul>
<h3>Setup Instructions</h3>
<p>First, let’s install the RMD Reader extension so that Pelican knows what to do. We’ll do this by cloning the pelican-plugins github repository and referencing this in our Pelican configuration file. This has the added benefit of allowing you to easily use other Pelican plugins, should you decide you want to do that.</p>
<p>Execute the following command from the directory where you want to store this repository.<br>
<em>(Run from terminal):</em></p>
<div>
<pre><span></span>git clone --recursive https://github.com/getpelican/pelican-plugins
</pre>
</div>
<p>Add the following to your Pelican config file. If you already have these variables defined, simple add the new path and plugin to the end of your existing list.<br>
<em>(Edit pelicanconf.py):</em></p>
<div>
<pre><span></span><span>PLUGIN_PATHS</span> <span>=</span> <span>[</span><span>'your-path-to/pelican-plugins'</span><span>]</span>
<span>PLUGINS</span> <span>=</span> <span>[</span><span>'rmd_reader'</span><span>]</span>
</pre>
</div>
<p>Make sure you have the rpy2 python package installed.<br>
<em>(Run from terminal):</em></p>
<div>
<pre><span></span><span>pip</span> <span>install</span> <span>rpy2</span>
</pre>
</div>
<p>Also make sure you have the knitr R package installed.<br>
<em>(Run from R):</em></p>
<div>
<pre><span></span>install.packages<span>(</span><span>'knitr'</span><span>)</span>
</pre>
</div>
<p></p>
<h3>Additional Setup</h3>
<p>The above is the core setup, but there are a few more tweaks that I recommend you do in order to make your life easier down the road.</p>
<p>Add the following to your Pelican config file. Essentially what we’re doing here is giving knitr instructions on how to name &amp; where to store image files to reduce the likelihood of you having conflicts and overwriting files from older blog posts. There are several ways to do this, but this seemed the best solution to me. For further details, check out the <a target="_blank" rel="nofollow" href="https://github.com/getpelican/pelican-plugins/tree/master/rmd_reader">official rmd_reader documentation</a>.<br>
<em>(Edit pelicanconf.py):</em></p>
<div>
<pre><span></span><span>STATIC_PATHS</span> <span>=</span> <span>[</span><span>'figure'</span><span>]</span>
<span>RMD_READER_RENAME_PLOT</span> <span>=</span> <span>'directory'</span>
<span>RMD_READER_KNITR_OPTS_CHUNK</span> <span>=</span> <span>{</span><span>'fig.path'</span><span>:</span> <span>'figure/'</span><span>}</span>
</pre>
</div>
<p></p>
<h3>Testing &amp; Examples</h3>
<p>Finally, we’re ready to test out our new setup. Try this out with your own .Rmd document or use this one, <a target="_blank" rel="nofollow" href="https://raw.githubusercontent.com/michaeltoth/michaeltoth/master/content/pelican_rmarkdown_setup.Rmd">available on my Github</a>, if you’re just looking for a quick test. The steps are relatively simple:</p>
<ol>
<li>Save your .Rmd file into the same content folder where you’d put any other .md file for your Pelican blog</li>
<li>Run your Pelican blog like you would normally. </li>
</ol>
<p>That’s it. rmd_reader will automatically execute your .Rmd file, produce the relevant graphics, and set up the html for your blog just like base Pelican would.</p>
<p>Just to confirm everythng is working correctly, let’s do some basic operations on the iris dataset.</p>
<p>First let’s see a simple summary of the data:</p>
<div>
<pre><span></span><span>summary</span><span>(</span>iris<span>)</span>
</pre>
</div>
<div>
<pre><span></span>##   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   
##  Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100  
##  1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300  
##  Median :5.800   Median :3.000   Median :4.350   Median :1.300  
##  Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199  
##  3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800  
##  Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  
##        Species  
##  setosa    :50  
##  versicolor:50  
##  virginica :50  
##                 
##                 
## 
</pre>
</div>
<p>Let’s finish with a simple k-means cluster analysis:</p>
<div>
<pre><span></span><span>library</span><span>(</span>broom<span>)</span>
<span>library</span><span>(</span>dplyr<span>)</span>
<span>library</span><span>(</span>ggplot2<span>)</span>

iris_sub <span>&lt;-</span> select<span>(</span>iris<span>,</span> x1 <span>=</span> Petal.Length<span>,</span> x2 <span>=</span> Petal.Width<span>)</span>

kclusts <span>&lt;-</span> <span>data.frame</span><span>(</span>k<span>=</span><span>1</span><span>:</span><span>6</span><span>)</span> <span>%>%</span> group_by<span>(</span>k<span>)</span> <span>%>%</span> do<span>(</span>kclust<span>=</span>kmeans<span>(</span>iris_sub<span>,</span> <span>.</span><span>$</span>k<span>))</span>
clusters <span>&lt;-</span> kclusts <span>%>%</span> group_by<span>(</span>k<span>)</span> <span>%>%</span> do<span>(</span>tidy<span>(</span><span>.</span><span>$</span>kclust<span>[[</span><span>1</span><span>]]))</span>
assignments <span>&lt;-</span> kclusts <span>%>%</span> group_by<span>(</span>k<span>)</span> <span>%>%</span> do<span>(</span>augment<span>(</span><span>.</span><span>$</span>kclust<span>[[</span><span>1</span><span>]],</span> iris_sub<span>))</span>
clusterings <span>&lt;-</span> kclusts <span>%>%</span> group_by<span>(</span>k<span>)</span> <span>%>%</span> do<span>(</span>glance<span>(</span><span>.</span><span>$</span>kclust<span>[[</span><span>1</span><span>]]))</span>

ggplot<span>(</span>assignments<span>,</span> aes<span>(</span>x <span>=</span> x1<span>,</span> y <span>=</span> x2<span>))</span> <span>+</span> 
    facet_wrap<span>(</span><span>~</span> k<span>)</span> <span>+</span>
    geom_point<span>(</span>aes<span>(</span>color<span>=</span><span>.</span>cluster<span>))</span> <span>+</span> 
    geom_point<span>(</span>data<span>=</span>clusters<span>,</span> size<span>=</span><span>10</span><span>,</span> shape<span>=</span><span>&quot;x&quot;</span><span>)</span>
</pre>
</div>
<p><img alt="plot of chunk iris-plot" src="https://i1.wp.com/michaeltoth.me/figure/pelican_rmarkdown_setup-iris-plot-1.png?w=456" data-recalc-dims="1"></p>
<h3>Closing Remarks</h3>
<p>That’s it! I’ve been meaning to get this set up for a while, and I’m pretty excited about it. Since most of my blog posts are R analyses, this is going to really simplify my workflow, which should make it much for me to actually finalize and post my results, something I’ve had issues with before. I’m also glad I’ll be able to make greater use of R Markdown/Knitr, which will help me to organize my thoughts while analyzing as well as create reproducible research documents to share. I hope you find this useful as well!</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="http://michaeltoth.me/how-to-write-pelican-blog-posts-using-rmarkdown-knitr.html"> Michael Toth's Blog</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/YTHEQxGn_XE"></a></div>