How to shorten URLs with R

Deepanshu Bhalla Add Comment ,
In this tutorial we will see how we can shorten URL using R programming language. APIs used in the tutorial are free and do not require any registration or sign-up before using them. This is especially useful when you need to share some links with the team. Long URLs do not look attractive and error prone in copy paste.
URL Shortener with R
In the code below, we are using and API services. The only difference between them is that shows link preview and does not redirect automatically to the original long URL. Whereas will take you to the original automatically without showing any preview. By default linkPreview is set FALSE in the below user defined R function.

ShortURL <- function(link, linkPreview = FALSE) {
  api <- if(linkPreview) {""} else {""}
  query <- list(url = link)
  request <- httr::GET(api, query = query)
  content <- httr::content(request, as = "text", encoding = "utf-8")
  result <- jsonlite::fromJSON(content)


Test if shortened URL is working

Shorter <- ShortURL("")
Related Posts
Spread the Word!
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 0 Response to "How to shorten URLs with R"
Next → ← Prev