knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

grabargs

hi, kitten

A very small package with only one real purpose: to import the grab_args function into any package or Shiny App. I got tired of importing this function into all my private packages, so deploying as a package itself.

Installation

You can install the released version of grabargs from GitHub with:

devtools::install_github("CarlBoneri/grabargs")

Why this function is helpful

Let's say you have a function with a ton of optional arguments. A good example is when building an API and the query params are mostly optional. Let's use twitter as an example for searching tweets.

I'm not in the mood to write out all the params, so let's parse the doc:

library(xml2)

raw_page <- xml2::read_html("https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets")

params_table <- xml_find_all(raw_page, ".//h2[contains(.,'Parameters')]//following-sibling::table") %>%
  rvest::html_table() %>% .[[1]] %>%  dplyr::rename_all(function(x){
    tolower(x) %>% gsub("(\\s+)", "_", ., perl = TRUE)
  })

# The description column is making this thing wrap way too much, so gonna truncate:
# 
params_table$description <- lapply(params_table$description, function(i){
    r <- stri_split_regex(i, "(?<=\\.|\\!|\\?)(\\s+)") %>% unlist()
    if(length(r) > 1){
        stri_join(r[1:2], collapse = " ")
    }else {
        r
    }
}) %>% unlist

knitr::kable(params_table %>% select(-default_value), align = "l")

So our function would want to allow the user to pass all the params, but testing for each would be a pain, because there are r nrow(params_table) possible params. For instance:

params_table$name

Here's what our function would look like if we did have to check for each (please note this is an example, we would need to check for auth tokens etc in a real scenario, and that httr will actually drop NULL values in query lists):

t.search_test <- function(q = NULL, geocode = NULL, lang = NULL, locale = NULL, result_type = c("mixed", "recent", "popular"), 
                          count = 100, until = NULL, since_id = NULL, max_id = NULL, include_entities = FALSE){

  # Notice we have to build a full list of objects and perform another step cleaning...
  query <- list(q = q, geocode = geocode, lang = lang, locale = locale, result_type = match.arg(result_type),
                count = count, until = until, since_id = since_id, max_id = max_id, include_entities = include_entities)

  query <- query[!mapply(is.null, query)]

  query
}

t.search_test()

VS

source("R/grab_args.R")
t.search_test2 <- function(q = NULL, geocode = NULL, lang = NULL, locale = NULL, result_type = c("mixed", "recent", "popular"), 
                           count = 100, until = NULL, since_id = NULL, max_id = NULL, include_entities = FALSE){

  query <- grab_args_clean()


  query

}
t.search_test2()

As you can see this has shortened our manual process greatly, as well as given us a cleaner approach.



CarlBoneri/grabargs documentation built on Oct. 30, 2019, 5:35 a.m.