<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/convert-epub-to-text-for-processing-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rud.is/b/2018/04/12/convert-epub-to-text-for-processing-in-r/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="http://twitter.com/RMHoge" target="_blank">@RMHoge</a> asked the following on Twitter:</p>
<blockquote class="twitter-tweet" data-lang="en">
<p lang="en" dir="ltr">Hello <a rel="nofollow" href="https://twitter.com/hashtag/rstats?src=hash&ref_src=twsrc%5Etfw" target="_blank">#rstats</a> hyve mind! Is there a package that reads epub into R? I can not find any, I now convert to text and parse the text but you sort of lose the structure of the text. Pinging <a rel="nofollow" href="https://twitter.com/dataandme?ref_src=twsrc%5Etfw" target="_blank"><a rel="nofollow" href="http://twitter.com/dataandme" target="_blank">@dataandme</a></a> <a rel="nofollow" href="https://twitter.com/hrbrmstr?ref_src=twsrc%5Etfw" target="_blank"><a rel="nofollow" href="http://twitter.com/hrbrmstr" target="_blank">@hrbrmstr</a></a></p>
<p>— Roel (<a rel="nofollow" href="http://twitter.com/RMHoge" target="_blank">@RMHoge</a>) <a rel="nofollow" href="https://twitter.com/RMHoge/status/984345828671344640?ref_src=twsrc%5Etfw" target="_blank">April 12, 2018</a></p>
</blockquote>
<p></p>
<p>Here’s one way to do that which doesn’t rely on <code>pandoc</code> (<code>pandoc</code> can easily do this and ships with RStudio but shelling out for this is cheating <img alt="🙂" src="https://s.w.org/images/core/emoji/2.4/72x72/1f642.png" class="wp-smiley"></p>
<p>We’ll need some help (NOTE that 2 of these are “GitHub” packages)</p>
<pre><code>library(archive) # install_github(&quot;jimhester/archive&quot;) + 3rd party library
library(hgr) # install_github(&quot;hrbrmstr/hgr&quot;)
library(stringi)
library(tidyverse)</code></pre>
<p>We’ll use one of <a rel="nofollow" href="http://twitter.com/hadley" target="_blank">@hadley</a>’s books since it’s O’Reilly and they do epubs well. The <code>archive</code> package lets us treat the epub (which is really just a ZIP file) as a mini-filesystem and embraces “tidy” so we have lovely data frames to work with:</p>
<pre><code>bk_src &lt;- &quot;~/Data/R Packages.epub&quot;

bk &lt;- archive::archive(bk_src)

bk
## # A tibble: 92 x 3
##    path                           size date               
##    </code></pre>