<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/building-a-package-automatically/"></div></div><div><a data-url="https://www.r-bloggers.com/building-a-package-automatically/" data-counturl="https://www.r-bloggers.com/building-a-package-automatically/" data-count="horizontal" data-text="Building a package automatically" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.56n.dk/building-a-package-automatically/"> Renglish – 56north | Skræddersyet dataanalyse</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>So, I just finished building a R wrapper for FOAAS (F*ck Off As A Service).  FOAAS (F*ck Off As A Service) provides a modern, RESTful, scalable solution to the common problem of telling people to f*ck off.</p>
<p>I wanted to share the package with you and also tell you how I automated the build. You can find the package at <a target="_blank" rel="nofollow" href="https://github.com/56north/foaas">https://github.com/56north/foaas</a></p>
<p>I thought the API was quite a fun idea and I wanted to make a wrapper. I was curious to see if I could automate the build. The API has 72 different f*ck off calls and so I build a function to handle the heavy lifting. The reason for this is essentially what Hadley writes in “<a target="_blank" rel="nofollow" href="http://r4ds.had.co.nz/functions.html">R for Data Science</a>“:</p>
<blockquote><p>One of the best ways to improve your reach as a data scientist is to write functions. Functions allow you to automate common tasks in a more powerful and general way than copy-and-pasting. Writing a function has three big advantages over using copy-and-paste:</p>
<p>You can give a function an evocative name that makes your code easier to understand.</p>
<p>As requirements change, you only need to update code in one place, instead of many.</p>
<p>You eliminate the chance of making incidental mistakes when you copy and paste (i.e. updating a variable name in one place, but not in another).</p>
</blockquote>
<p>I build a function that got all the different calls from the API. Then I looped through each one and pasted together a string that would build a function for each call. I included som roxygen info and then wrote them all to a functions.R file.</p>
<p>Once the procedure was clear and the code was written it took &lt; 2 seconds to write a package with 72 functions. Not bad! <img alt="🙂" class="wp-smiley" src="https://i2.wp.com/s.w.org/images/core/emoji/72x72/1f642.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Here is the code:</p>
<p># Get possible functions<br>
url &lt;- “https://www.foaas.com/operations”</p>
<p>foaas_calls &lt;- httr::content(httr::GET(url))</p>
<p># Write functions for each<br>
functions &lt;- lapply(foaas_calls, function(x){<br>
name &lt;- unlist(stringr::str_split(x$url, “/”))[2]</p>
<p>description &lt;- x$name</p>
<p>parms &lt;- unlist(lapply(x$fields, function(y){ y$field }))</p>
<p>func_base &lt;- paste0(“paste0(\”https://www.foaas.com/”, name, “/\”,”)<br>
func_end &lt;- paste(parms, collapse = “, \”/\”, “)</p>
<p>pck_name &lt;- make.names(name)</p>
<p>func &lt;- paste0(“foaas_”, pck_name, ” &lt;- function(“, paste(parms, collapse = “, “), “){\n”,<br>
“url &lt;- “, paste(func_base, func_end, “)”), “\n”,<br>
“return(jsonlite::fromJSON(url))\n”,<br>
“}”)</p>
<p>func_ex &lt;- paste0(“#’ “, description, “\n”,<br>
“#’ @export \n\n”,<br>
func, “\n\n##############################\n\n”)</p>
<p>return(func_ex)<br>
})</p>
<p># Write functions to package<br>
readr::write_lines(functions[[1]], “R/fucking_functions.R”)</p>
<p>lapply(functions[2:72], function(x){</p>
<p>readr::write_lines(x, “R/fucking_functions.R”, append = T)</p>
<p>})</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/building-a-package-automatically/"></div></div><div><a data-url="https://www.r-bloggers.com/building-a-package-automatically/" data-counturl="https://www.r-bloggers.com/building-a-package-automatically/" data-count="vertical" data-text="Building a package automatically" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.56n.dk/building-a-package-automatically/"> Renglish – 56north | Skræddersyet dataanalyse</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/K4hS3muh2y0"></a></div>