<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/how-to-make-and-share-an-r-package-in-3-steps/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://datascienceplus.com/how-to-make-and-share-an-r-package-in-3-steps/"> R Programming – DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>If you find yourself often repeating the same scripts in R, you might come to the point where you want to turn them into reusable functions and create your own R package. I recently reached that point and wanted to learn how to build my own R package – as simple as possible.</p>
<p>It took me some time to figure out the full process from A to Z. I was often left with remaining questions:</p>
<ul>
<li>What to do with the import of external libraries?</li>
<li>What about documentation?</li>
<li>How to actually install my package?</li>
<li>How can I share it with others?</li>
</ul>
<p>Therefore, I will explain how I made <a rel="nofollow" href="https://github.com/Emelieh21/FlightsR" target="_blank">my first R package</a> and which methods I found helpful. Of course, there are different approaches out there, some of them very well documented, like for example <a rel="nofollow" href="https://datarvalue.blogspot.com/2017/05/mit-step-by-step-instructions-for.html" target="_blank">this one</a> – but the following <em>3 easy steps</em> worked for me, so maybe they will help you too getting your first R package ready, installed and online quickly.</p>
<p>My first package is a wrapper for the <a rel="nofollow" href="https://developer.flightstats.com/" target="_blank">FlightStats API</a>. You can sign up for FlightStats to get a free trial API key (which unfortunately works for one month only). However, you can of course make a wrapper for any API you like or make a non-API related package. Two links I found very useful for getting started making my first package are <a rel="nofollow" href="https://stat.ethz.ch/R-manual/R-devel/library/utils/html/package.skeleton.html" target="_blank">this one</a> and <a rel="nofollow" href="http://blog.revolutionanalytics.com/2015/11/how-to-store-and-use-authentication-details-with-r.html" target="_blank">this one</a> (regarding the storing of API keys). Also, I learned a lot using <a rel="nofollow" href="https://github.com/ropensci/gtfsr" target="_blank">this package</a> as an example.</p>
<h2>Prepare the functions you want to include</h2>
<p>First of all, we need to have all the functions (and possibly dataframes &amp; other objects) ready in our R environment. For example, imagine we want to include these two functions to store the API key and an app ID:</p>
<pre>setAPIKey &lt;- function(){
  input = readline(prompt=&quot;Enter your FlightStats API Key: &quot;)
  Sys.setenv(flightstats_api_key = input) # this is a more simple way of storing API keys, it saves it in the .Rprofile file, however this is only temporary - meaning next session the login details will have to be provided again. See below how to store login details in a more durable way.
  }
setAppId &lt;- function(){
  input = readline(prompt=&quot;Enter your FlightStats appID: &quot;)
  Sys.setenv(flightstats_app_id = input)
}
</pre>
<p>Now you can retrieve the login in details like this:</p>
<pre>ID = Sys.getenv(&quot;flightstats_app_id&quot;) # if the key does not exist, this returns an empty string (&quot;&quot;), in this case the user should be prompted to use the setAPIKey() and setAppID() functions
KEY = Sys.getenv(&quot;flightstats_api_key&quot;) 
</pre>
<p>However, if you would like the login details to still be accessible the next time you open R, you can define your login details in the <em>.Renviron</em> file (instead of the <em>.Rprofile</em> file). The <em>.Renviron</em> file can be found in your home directory. You can find the path to your home directory by running <code>Sys.getenv(&quot;HOME&quot;)</code>. For more info see <a rel="nofollow" href="https://csgillespie.github.io/efficientR/r-startup.html" target="_blank">here</a>. The final function to store the API key in the FlightsR package looked like <a rel="nofollow" href="https://github.com/Emelieh21/FlightsR/blob/master/R/setAPIKey.R" target="_blank">this</a>.</p>
<p>Side note: I actually did not know this before, but find it quite handy: If you want to know the script of any function in R, you can find it by just typing the function name and hit enter. For example:</p>
<pre>> read.csv
function (file, header = TRUE, sep = &quot;,&quot;, quote = &quot;\&quot;&quot;, dec = &quot;.&quot;, 
    fill = TRUE, comment.char = &quot;&quot;, ...) 
read.table(file = file, header = header, sep = sep, quote = quote, 
    dec = dec, fill = fill, comment.char = comment.char, ...)
</pre>