Building a package automatically

[This article was first published on Renglish – 56north | Skræddersyet dataanalyse, 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.

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.

I wanted to share the package with you and also tell you how I automated the build. You can find the package at https://github.com/56north/foaas

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 “R for Data Science“:

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:

You can give a function an evocative name that makes your code easier to understand.

As requirements change, you only need to update code in one place, instead of many.

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).

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.

Once the procedure was clear and the code was written it took < 2 seconds to write a package with 72 functions. Not bad! ?

Here is the code:

# Get possible functions
url <- “https://www.foaas.com/operations”

foaas_calls <- httr::content(httr::GET(url))

# Write functions for each
functions <- lapply(foaas_calls, function(x){
name <- unlist(stringr::str_split(x$url, “/”))[2]

description <- x$name

parms <- unlist(lapply(x$fields, function(y){ y$field }))

func_base <- paste0(“paste0(\”https://www.foaas.com/”, name, “/\”,”)
func_end <- paste(parms, collapse = “, \”/\”, “)

pck_name <- make.names(name)

func <- paste0(“foaas_”, pck_name, ” <- function(“, paste(parms, collapse = “, “), “){\n”,
“url <- “, paste(func_base, func_end, “)”), “\n”,
“return(jsonlite::fromJSON(url))\n”,
“}”)

func_ex <- paste0(“#’ “, description, “\n”,
“#’ @export \n\n”,
func, “\n\n##############################\n\n”)

return(func_ex)
})

# Write functions to package
readr::write_lines(functions[[1]], “R/fucking_functions.R”)

lapply(functions[2:72], function(x){

readr::write_lines(x, “R/fucking_functions.R”, append = T)

})

To leave a comment for the author, please follow the link and comment on their blog: Renglish – 56north | Skræddersyet dataanalyse.

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)