How to Dynamically Create Hyperlinks in Displayr

How to Dynamically Create Hyperlinks in Displayr

If you're creating a dashboard where you want the users to be able to go to different web sites or pages based on selections they've made in Displayr, then this can be achieved with a bit of R code.

Sometimes it’s necessary to direct users away from your lovely dashboards.  Manually entering links into Displayr (with Insert Hyperlink) means that you can link from any image or text on your pages, but what if you have a lot of links to create, or if you have a sample of companies’ (with their website URLs) websites and would like to direct the dashboard user to that website? Manually creating all those links could be time-consuming.  Instead, let’s create them dynamically!

Start with a list of links

We can’t link to somewhere without the address.  Thus, the first thing you’ll need is a list of URLs (or a dynamically created list of URLs) that we can direct your users to.  This may come from any source, but it should be a full URL.  That means it should include the protocol at the beginning of the link – the bit that says “http” or “https”.  Here’s a list of links to some of my favorite websites, which I’ll use in the rest of this example:

https://www.etymonline.com/No joke, I go here at least twice a day
https://www.displayr.com/Alright, they pay my salary and this blog post is on the Displayr website
https://sok.riksarkivet.se/digitala-forskarsalenSwedish genealogy research rules!
https://www.goodreads.com/For all you avid readers out there.
http://www.bom.gov.au/products/
national_radar_sat.loop.shtml
Do I need to bring an umbrella?

That may sound trickier than you feel comfortable with, so I’ve written the code for your here, and you can just copy and paste it into Displayr.  Here’s what to do:

Write a little script that turns these into clickable links

  1. In Displayr, go to Insert > R Output.
  2. Under Properties > R CODE paste in the code below.  You need to edit the list of URLs (unless you just want to visit my favorite sites), and the list of the link text to show.  The latter is text that gets shown on screen instead of the URL itself.
  3. Tick Automatic to run the code
## Enter the list of links here
links = c("https://www.etymonline.com/", 
          "https://www.displayr.com/", 
          "https://sok.riksarkivet.se/digitala-forskarsalen", 
          "https://www.goodreads.com/", 
          "http://www.bom.gov.au/products/national_radar_sat.loop.shtml")

## Enter the labels you want to show instead of the URL itself
link_text_to_show = c("Etymonline", 
                       "Displayr", 
                       "Riksarkivet", 
                       "Goodreads", 
                       "Australian weather radar")</pre>
contents = character()
for (a in 1:length(links)) {
    contents = paste(contents, '<a href="',links[a],'" target="_blank">', link_text_to_show[a], '</a><br></body></html>', sep="")    
}

your.html = paste('<DOCTYPE html><html><head></head><body>', contents, '</a></body></html>', sep="")
rhtmlMetro::Box(text = your.html, text.as.html = TRUE)

You’ll get an output that looks like the below.  Not the most visually exciting, but you can consider styling it with CSS. Unfortunately the usual Displayr formatting buttons won’t work on an HTML output.  However, you now have the satisfaction of having a list of links without having to create them manually, one by one, using the Displayr interface.

Hyperlink

Generating links dynamically

Some websites are databases.  That often means that each page on the website in question can be identified by a section of the URL.  For example, etymonline has a database of words, and these words form part of the link.  Thus, if I wanted to create direct links to some words, I could simply update the script we made before to include those words instead.  Here are my example words:

  1. Antidisestablishmentarianism
  2. Display
  3. Dashboard
  4. Peacock
  5. Visualize

And here’s the code I would use to create links for these words automatically:

 
## Enter the list of links here
base_link = "https://www.etymonline.com/word/"

## Enter the labels you want to show instead of the URL itself
links = c("Antidisestablishmentarianism",
           "Display",
           "Dashboard",
           "Peacock",
           "Visualize")

contents = character()
for (a in 1:length(links)) {
    contents = paste(contents, '<a href="', base_link, links[a],'" target="_blank">', links[a], '</a><br></body></html><br>', sep="")    
}

your.html = paste('<!DOCTYPE html><html><head></head><body>', contents, '</a></body></html>', sep="")
rhtmlMetro::Box(text = your.html, text.as.html = TRUE)

In this code we only have a single URL. The rest of the programming takes the individual words and attaches them to the end of the URL, thereby creating a correct link directly to the page for the word. Simultaneously, the code builds the HTML needed for the links.

Although the examples here are simplistic, these methods could be used to create more complicated links that take information directly from data stored in your Displayr document. For example, if you create a dashboard that produces results for several clients or competitors using a custom drop-down menu, like our Supermarket Brand Health example, you could create a link which sends you to the homepage of whichever company is selected. We use this on our own customer health dashboard to quickly link to customer information.

About Mattias Engdahl

Matt has spent the entirety of his career in the market research space, working principally with data collection and processing. He loves nothing more than working out just why it is that you're one case out in Q63 when it *should* have been seen by everyone, or coming up with new solutions and scripts to make sure you have the right chart or table for that final report.