<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/writing-and-publishing-my-first-r-package/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rviews.rstudio.com/2017/09/05/writing-and-publishing-my-first-r-package/"> R Views</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<h4>Inspired by the Community</h4>
<p>One of the themes at useR 2017 in Brussels was “Get involved”. People were encouraged to contribute to the community, even when they did not consider themselves R specialists (yet). This could be by writing a package or a blog post, but also by simply correcting typos through pull requests, or sending a tweet about a successful analysis. Bottom line: get your stuff out in the open. Share your work!</p>
<p>I felt this urge of getting involved already a year ago, at the useR conference in 2016. Hearing all these people speak about the great work they had done was really inspiring, and I wanted to be a part of it. I wanted to find out what it was like to develop a package. I knew that what I could do was only minor; I have a full-time job as a data scientist of which contributing to the community is not part of the job description. However, I could free up one day a week by working a few hours more on the other days of the week and I could do a little on the train to work every day. I was not sure if I was up to developing a full R package, but I could at least try.</p>
</div>
<div>
<h4>Writing <code>padr</code></h4>
<p>The idea for the <a rel="nofollow" href="https://cran.r-project.org/package=padr" target="_blank">padr</a> package came at work, where I was dealing with time-stamped data a lot. Aggregating data to a higher level and filling missing values took me a quite some time each time I did it. I thought there must be a better way for doing this. I did not have a clear end product in mind. I just started trying stuff and stitched small ideas together to do more complex operations. In the meantime, I worked my way through Hadley’s <em>R Packages</em>, and learned about the fundamentals of software design in R, picking up skills while I went along.</p>
<p>This past January, I published the first version of <code>padr</code> on CRAN, and I must say that it was quite scary. Putting your product out in the open comes with a lot of impostor feelings. I had thoroughly tested the functions, but still was afraid all kinds of bugs would pop up. Well, bugs did pop up and this appeared to be just fine. I had very nice comments by people suggesting improvements or who even fixed bugs themselves. If there is one lesson I learned from writing a package, it is that software does not have to be perfect to be useful. Of course, you don’t bring junk that is full of errors to CRAN, but once you have tried your best, you should just get it in the open and wait for feedback. This would improve the software much faster than spending endless evenings trying to find bugs.</p>
</div>
<div>
<h4>How <code>padr</code> works</h4>
<p>The central concept for the <code>padr</code> package is that time-stamped data are associated with an interval. This is the heartbeat of the data. Observed data points are separated by multiples of the interval. But, there can be observations missing. We compute the largest, evenly spaced grid that fits all our observations. See the following example.</p>
<pre><code>library(padr)
library(dplyr)
as.Date(c(&quot;2017-01-01&quot;,&quot;2017-01-03&quot;,&quot;2017-01-07&quot;)) %>% get_interval()</code></pre>
<pre><code>## [1] &quot;2 day&quot;</code></pre>
<p>Now, the first main thing that <code>padr</code> does is <code>thicken()</code> the datetime variable in a data frame. It adds a variable of a higher interval to the data frame. We can subsequently aggregate to this higher interval with <code>dplyr</code>. We use the <code>coffee</code> data set, that comes with <code>padr</code> as an example. This contains four hypothetical purchases in a coffee shop.</p>
<pre><code>coffee</code></pre>
<pre><code>##            time_stamp amount
## 1 2016-07-07 00:11:21   3.14
## 2 2016-07-07 00:46:48   2.98
## 3 2016-07-09 04:25:17   4.11
## 4 2016-07-10 01:45:11   3.14</code></pre>
<p>Now, to get the total amount spent per day, we run the following code.</p>
<pre><code>(coffee_day &lt;- coffee %>% 
  thicken(&quot;day&quot;) %>% 
  group_by(time_stamp_day) %>% 
  summarise(day_amnt = sum(amount)))</code></pre>
<pre><code>## # A tibble: 3 x 2
##   time_stamp_day day_amnt
##           </code></pre></div>