unshorten urls in R code example

Example 1: unshorten url in R

decode_short_url <- function(url, ...) {
  # PACKAGES #
  require(RCurl)
 
  # LOCAL FUNCTIONS #
  decode <- function(u) {
    Sys.sleep(0.5)
    x <- try( getURL(u, header = TRUE, nobody = TRUE, followlocation = FALSE, cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")) )
    if(inherits(x, 'try-error') | length(grep(".*Location: (\\S+).*", x))<1) {
      return(u)
    } else {
      return(gsub('.*Location: (\\S+).*', '\\1', x))
    }
  }
 
  # MAIN #
  gc()
  # return decoded URLs
  urls <- c(url, ...)
  l <- vector(mode = "list", length = length(urls))
  l <- lapply(urls, decode)
  names(l) <- urls
  return(l)
}

Example 2: unshorten url in R

library("knitr")
library(urlshorteneR)

Example 3: unshorten url in R

if (interactive()) {

bitly_retrieve_group(ui$default_group_guid)
bitly_retrieve_groups()
}

Tags:

Misc Example