2

I have a shiny app where we use bookmarking URL. The URLs seem to be too long. I am saving these URLs in a datatable. How can I short them before adding to the app? Ex for the long URL is as below:

http://127.0.0.1:4534/?_inputs_&bookmarkName=%2211m%22&budget_input=11000000&col=%22table%22&col1=%22bud_digi_table%22&drop624996478=1&drop624996478_state=true&inp_lb=0.5&inp_ub=1.5&iterations=2000&myNavbarPage=%22Optimizer%22&opt_reset=0&opt_run=0
SNT
  • 1,283
  • 3
  • 32
  • 78

2 Answers2

3

Using the <a href=full URL target=_blank>URL</a> tag we can pass the URL in span tag and added the attribute title which is the attribute HTML uses as the default for mouse-overs. Here how we can use the span tag:
<a href=full URL target=_blank> <span title= full URL > Short URL </span> </a>

Then we will use JS to display a reasonable length for span in data.table in this case will be 30

library(DT)
datatable(data.frame(URLs), options = list(columnDefs = list(list(
  targets = c(1),
  render = JS(
    "function(data, type, row, meta) {",
    "return type === 'display' && data.length > 30 ?",
    "'<a href=\"' + data + 'target=\"_blank\"> <span title=\"' + data + '\">' +
         data.substr(0, 30) + '...</span></a>' : data;",
    "}")
))))

Data

URLs <- c("http://127.0.0.1:4534/?_inputs_&bookmarkName=%2211m%22&budget_input=11000000&col=%22table%22&col1=%22bud_digi_table%22&drop624996478=1&drop624996478_state=true&inp_lb=0.5&inp_ub=1.5&iterations=2000&myNavbarPage=%22Optimizer%22&opt_reset=0&opt_run=0
", "http://127.0.0.1:4534/?_inputs_&bookmarkName=%2211m%22&budget_input=11000000&col=%22table%22&col1=%22bud_digi_table%22&drop624996478=1&drop624996478_state=true&inp_lb=0.5&inp_ub=1.5&iterations=2000&myNavbarPage=%22Optimizer%22&opt_reset=0&opt_run=0
")
A. Suliman
  • 12,923
  • 5
  • 24
  • 37
0

The following will actually compress a string using gzip encoding. The following will represent the raw encoding as a base64 string:

library(base64enc)

url <- "http://127.0.0.1:4534/?_inputs_&bookmarkName=%2211m%22&budget_input=11000000&col=%22table%22&col1=%22bud_digi_table%22&drop624996478=1&drop624996478_state=true&inp_lb=0.5&inp_ub=1.5&iterations=2000&myNavbarPage=%22Optimizer%22&opt_reset=0&opt_run=0"
gzip_url <- memCompress(charToRaw(url),type="gzip")
compressed_url<-base64encode(what=gzip_url)

rawToChar(memDecompress(from=base64decode(what=compressed_url),type="gzip"))

This does shorten the length of the total character string. Although not be much. The better way to shorten is to create a separate lookup table that replaces some of your longer varaibles, like "bookmarkName" and "myNavbarPage" etc to variables that you control, "B" or "m" for example that you can control as use string replacement. Otherwise, any type of compression or encoding will need to capture these lengthy strings. There's a helpful discussion here: An efficient compression algorithm for short text strings

You can also convert it to a tiny url. This also uses a look-up table by taking a long url and created an alternative, shorter reference url. You could either manage this internally or use a standard tiny url service to do it. There's also an R package that helps to manage this and you can read more about it here: https://www.r-bloggers.com/urlshortener-a-package-for-shortening-urls/

Soren
  • 1,792
  • 1
  • 13
  • 16
  • Thanks . It doesnt shorten as much as compared to bitly service. The urlshortener package doesnt seem to be working. – SNT Mar 19 '19 at 22:14
  • Strictly speaking, the bitly service isn't shortening it -- it's just creating an alias for it and storing it in a hashtable. You can internalize this as well, such as by appending a new bookmarked string into a data.frame and abbreviating it to the row number. So your string could simply be shortened to "1" if it's the first entry. Bitly is effectively the same but keeps things still "looking" like a link for social media purposes. – Soren Mar 19 '19 at 22:18
  • Thanks. That's what the last resort seemed to be until there was some package to do it. – SNT Mar 19 '19 at 22:22