XLConnect 0.2-14

[This article was first published on Mirai Solutions, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

CRAN_Status_Badge Research software impact Rdoc

We are pleased to announce the release of XLConnect 0.2-14, now available from CRAN and from our GitHub repository.

This release is mainly a maintenance release and comes with the following changes:

The following example illustrates the use of XLConnect to produce a simple report:

<span class="n">require</span><span class="p">(</span><span class="n">XLConnect</span><span class="p">)</span><span class="w">

</span><span class="c1"># Create new Excel workbook</span><span class="w">
</span><span class="n">wb</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">loadWorkbook</span><span class="p">(</span><span class="s2">"earthquakes.xlsx"</span><span class="p">,</span><span class="w"> </span><span class="n">create</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">TRUE</span><span class="p">)</span><span class="w">

</span><span class="c1"># Create worksheet to hold data</span><span class="w">
</span><span class="n">targetSheet</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"Earthquakes"</span><span class="w">
</span><span class="n">createSheet</span><span class="p">(</span><span class="n">wb</span><span class="p">,</span><span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">targetSheet</span><span class="p">)</span><span class="w">

</span><span class="c1"># Export earthquake data</span><span class="w">
</span><span class="n">writeWorksheet</span><span class="p">(</span><span class="n">wb</span><span class="p">,</span><span class="w"> </span><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">quakes</span><span class="p">,</span><span class="w"> </span><span class="n">sheet</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">targetSheet</span><span class="p">)</span><span class="w">

</span><span class="c1"># Create cell style to color events with a magnitude >= 6.0 on the</span><span class="w">
</span><span class="c1"># Richter scale</span><span class="w">
</span><span class="n">largeMagnitudeStyle</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">getOrCreateCellStyle</span><span class="p">(</span><span class="n">wb</span><span class="p">,</span><span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Large Magnitude"</span><span class="p">)</span><span class="w">

</span><span class="c1"># Use solid red foreground to color rows</span><span class="w">
</span><span class="n">setFillForegroundColor</span><span class="p">(</span><span class="n">largeMagnitudeStyle</span><span class="p">,</span><span class="w"> </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">XLC</span><span class="o">$</span><span class="n">COLOR.RED</span><span class="p">)</span><span class="w">
</span><span class="n">setFillPattern</span><span class="p">(</span><span class="n">largeMagnitudeStyle</span><span class="p">,</span><span class="w"> </span><span class="n">fill</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">XLC</span><span class="o">$</span><span class="n">FILL.SOLID_FOREGROUND</span><span class="p">)</span><span class="w">

</span><span class="c1"># Determine large magnitude earthquakes</span><span class="w">
</span><span class="n">rowIdx</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">which</span><span class="p">(</span><span class="n">quakes</span><span class="o">$</span><span class="n">mag</span><span class="w"> </span><span class="o">>=</span><span class="w"> </span><span class="m">6.0</span><span class="p">)</span><span class="w">

</span><span class="c1"># Color rows accordingly</span><span class="w">
</span><span class="c1"># NOTE: Since we include a header row in the worksheet</span><span class="w">
</span><span class="c1"># we need to offset the row indices by 1!</span><span class="w">
</span><span class="n">setCellStyle</span><span class="p">(</span><span class="n">wb</span><span class="p">,</span><span class="w"> </span><span class="n">sheet</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">targetSheet</span><span class="p">,</span><span class="w"> 
             </span><span class="n">row</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">rep</span><span class="p">(</span><span class="n">rowIdx</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="n">each</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ncol</span><span class="p">(</span><span class="n">quakes</span><span class="p">)),</span><span class="w">
             </span><span class="n">col</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">rep_len</span><span class="p">(</span><span class="nf">seq_len</span><span class="p">(</span><span class="n">ncol</span><span class="p">(</span><span class="n">quakes</span><span class="p">)),</span><span class="w"> </span><span class="nf">length</span><span class="p">(</span><span class="n">rowIdx</span><span class="p">)),</span><span class="w">
             </span><span class="n">cellstyle</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">largeMagnitudeStyle</span><span class="p">)</span><span class="w">

</span><span class="c1"># Save workbook</span><span class="w">
</span><span class="n">saveWorkbook</span><span class="p">(</span><span class="n">wb</span><span class="p">)</span><span class="w">
</span>

XLConnect is a comprehensive and cross-platform R package for manipulating Microsoft Excel files from within R. It does not require any installation of Microsoft Excel or any other special drivers to be able to read & write Excel files. The only requirement is a recent version of a Java Runtime Environment (JRE).

To leave a comment for the author, please follow the link and comment on their blog: Mirai Solutions.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)