<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/spelling-1-0-quick-and-effective-spell-checking-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ropensci.org/blog/technotes/2017/09/07/spelling-release"> rOpenSci Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The new rOpenSci <a rel="nofollow" href="https://cran.r-project.org/web/packages/spelling/index.html" target="_blank">spelling</a> package provides utilities for spell checking common document formats including latex, markdown, manual pages, and DESCRIPTION files. It also includes tools especially for package authors to automate spell checking of R documentation and vignettes.</p>
<h2>Spell Checking Packages</h2>
<p>The main purpose of this package is to quickly find spelling errors in R packages. The <code>spell_check_package()</code> function extracts all text from your package manual pages and vignettes, compares it against a language (e.g. en_US or en_GB), and lists potential errors in a nice tidy format:</p>
<div>
<pre><code data-lang="r"><span></span><span>></span> spelling<span>::</span>spell_check_package<span>(</span><span>&quot;~/workspace/writexl&quot;</span><span>)</span>
  WORD       FOUND IN
booleans   write_xlsx.Rd<span>:</span><span>21</span>
xlsx       write_xlsx.Rd<span>:</span><span>6</span><span>,</span><span>18</span>
           title<span>:</span><span>1</span>
           description<span>:</span><span>1</span>
</code></pre>
</div>
<p>Results may contain false positives, i.e. names or technical jargon which does not appear in the English dictionary. Therefore you can create a WORDLIST file, which serves as a package-specific dictionary of allowed words:</p>
<div>
<pre><code data-lang="r"><span></span><span>></span> spelling<span>::</span>update_wordlist<span>(</span><span>&quot;~/workspace/writexl&quot;</span><span>)</span>
The following words will be added to the wordlist<span>:</span>
 <span>-</span> booleans
 <span>-</span> xlsx
Are you sure you want to update the wordlist<span>?</span>
<span>1</span><span>:</span> Yes
<span>2</span><span>:</span> No
</code></pre>
</div>
<p>Words added to this file are ignored in the spell check, making it easier to catch actual spelling errors:</p>
<div>
<pre><code data-lang="r"><span></span><span>></span> spell_check_package<span>(</span><span>&quot;~/workspace/writexl&quot;</span><span>)</span>
No spelling errors found.
</code></pre>
</div>
<p>The package also includes a cool function <code>spell_check_setup()</code> which adds a unit test to your package that automatically runs the spell check.</p>
<div>
<pre><code data-lang="r"><span></span><span>></span> spelling<span>::</span>spell_check_setup<span>(</span><span>&quot;~/workspace/writexl&quot;</span><span>)</span>
No changes required to <span>/</span>Users<span>/</span>jeroen<span>/</span>workspace<span>/</span>writexl<span>/</span>inst<span>/</span>WORDLIST
Updated <span>/</span>Users<span>/</span>jeroen<span>/</span>workspace<span>/</span>writexl<span>/</span>tests<span>/</span>spelling.R
</code></pre>
</div>
<p>By default this unit test will never actually fail; it merely displays potential spelling errors at the end of a <code>R CMD check</code>. But you can configure it to fail if you'd like, which can be useful to automatically highlight spelling errors on e.g. Travis CI.</p>
<h2>Under the Hood</h2>
<p>The spelling package builds on <a rel="nofollow" href="https://ropensci.org/blog/technotes/2016/09/12/hunspell-release-20" target="_blank">hunspell</a> which has a fully customizable spell checking engine. Most of the code in the spelling package is dedicated to parsing and extracting text from documents before feeding it to the spell checker.<br>
For example, when spell checking an rmarkdown file, we first extract words from headers and paragraphs (but not urls or R syntax).</p>
<div>
<pre><code data-lang="r"><span></span><span># Spell check this post</span>
<span>></span> spelling<span>::</span>spell_check_files<span>(</span><span>&quot;~/workspace/roweb/_posts/2017-09-07-spelling-release.md&quot;</span><span>,</span> lang <span>=</span> <span>'en_US'</span><span>)</span>
  WORD         FOUND IN
blog         <span>2017-09-07</span><span>-</span>spelling<span>-</span>release.md<span>:</span><span>7</span>
commonmark   <span>2017-09-07</span><span>-</span>spelling<span>-</span>release.md<span>:</span><span>88</span>
hunspell     <span>2017-09-07</span><span>-</span>spelling<span>-</span>release.md<span>:</span><span>69</span>
Jeroen       <span>2017-09-07</span><span>-</span>spelling<span>-</span>release.md<span>:</span><span>7</span>
knitr        <span>2017-09-07</span><span>-</span>spelling<span>-</span>release.md<span>:</span><span>88</span>
Ooms         <span>2017-09-07</span><span>-</span>spelling<span>-</span>release.md<span>:</span><span>7</span>
rmarkdown    <span>2017-09-07</span><span>-</span>spelling<span>-</span>release.md<span>:</span><span>88</span>
rOpenSci     <span>2017-09-07</span><span>-</span>spelling<span>-</span>release.md<span>:</span><span>18</span>
urls         <span>2017-09-07</span><span>-</span>spelling<span>-</span>release.md<span>:</span><span>88</span>
wordlist     <span>2017-09-07</span><span>-</span>spelling<span>-</span>release.md<span>:</span><span>49</span>
WORDLIST     <span>2017-09-07</span><span>-</span>spelling<span>-</span>release.md<span>:</span><span>34</span>
</code></pre>
</div>
<p>To accomplish this, we use knitr to drop code chunks, and subsequently parse markdown using <a rel="nofollow" href="https://ropensci.org/blog/blog/2016/12/02/commonmark" target="_blank">commonmark</a> and xml2, which gives us the text nodes and approximate line numbers in the source document.</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://ropensci.org/blog/technotes/2017/09/07/spelling-release"> rOpenSci 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 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/NHnV-UCDCQA" height="1"></a></div>