How to shorten URLs with R

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 is.gd and v.gd API services. The only difference between them is that v.gd shows link preview and does not redirect automatically to the original long URL. Whereas is.gd will take you to the original automatically without showing any preview. By default linkPreview is set FALSE in the below user defined R function.
library(httr)
library(jsonlite)

ShortURL <- function(link, linkPreview = FALSE) {
  
  api <- if(linkPreview) {"http://v.gd/create.php?format=json"} else {"http://is.gd/create.php?format=json"}
  query <- list(url = link)
  request <- httr::GET(api, query = query)
  content <- httr::content(request, as = "text", encoding = "utf-8")
  result <- jsonlite::fromJSON(content)
  
  return(result)
  
}

ShortURL("https://www.listendata.com/2021/01/run-sas-in-python-without-installation.html")

Test if shortened URL is working

Shorter <- ShortURL("https://www.listendata.com/2021/01/run-sas-in-python-without-installation.html")
browseURL(Shorter$shorturl)
Spread the Word!
Share
Related Posts
About Author:

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 has worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and Human Resource.

0 Response to "How to shorten URLs with R"

Post a Comment

Next → ← Prev
Love this post? Support Us!
Buy Me A Coffee