How to link images to a visualization in Displayr
Images, logos, or even product/concept images can help bring insights to life. Having an image that dynamically updates to match a particular data point is achievable in Displayr with a little help from some R code (mingled with some HTML). The purpose of this article is to give you a worked example of how this can be done. You can then adapt and tweak the example for your own purposes.
Consider the bar chart in the image on the left below. When the data is set to automatically sort rows by decreasing order, brand orders can change when a filter is applied to the bar chart. The point of interest here is that the logos (which are six separate objects that sit adjacent to the visualization) also dynamically update to reflect the order. For example, the second field updates to switch from the Coke Zero logo to the Pepsi Max logo. I'll use this as a worked example in this post.
Preparation: Getting a URL for each image
We are not going to be working with images you insert via Insert > Images from the Ribbon because those are static images. Responsive images actually sit within an R Output (Insert > R Output) that can only read images from a URL location. So the first step is to get the URLs for all images.
There are several ways to do this. Some Displayr users might have a network or shared server that can generate URLs. You can use a cloud-based app like Google Photos or Dropbox to generate links.
For instance, I used Dropbox to make my URL, which generated a URL that looks like this:
https://www.dropbox.com/s/b4vteqiwkxexxc6/Coca-Cola.png?dl=1. Because I have the Dropbox app on my desktop, I made this link by right-clicking on the file that was in a Dropbox folder and selected "Copy Dropbox Link." Importantly, I had to change the last digit from "0" to "1" at the end of the URL so that Dropbox, which allows the download from inside Displayr.
In Displayr, it's useful to have a pasted table of all your URLs alongside the brand name. We will later refer to that table when we write the code. As this table will be a reference page, I suggest putting the table on a new page and hiding it (from Viewers). The following steps will do this for you:
- Insert a new page by going to Insert > New Page > Title Only from the Ribbon
- Insert a table by going to Insert > Enter Table from the Ribbon
- Select the blank table object on the page, and go to Object Inspector > Inputs > DATA SOURCE > Paste or Type Data
- Enter your table in the spreadsheet with your URLs. The one I did in my example looks like the below:
- Click OK (Note: the name of the table in this example is
- Optional: Give the page a title like "Image Reference," and then select the page in the Pages Tree and hide it by going to Appearance > Hide from the Ribbon.
Create a merged table of data and image URLs
The next step is to line up the data that will feed into the visualization alongside the image. We ultimately want to achieve a merged table, like the one in the image below. The table has the data for the visualization in the first column (the %'s in this case) alongside the corresponding image URL. There are several ways to do this, and my example is just one method. You may like to write your own R code to make a custom table. What matters is that you have matched the item (brand), the data (% in this case), and the correct image URL.
First, I made a table of my source data, which was created by dragging the Preferred Cola variable onto the page to generate a standard summary table. The table is named table.Q3.Preferred.cola.
I created a merged table of my source data and images and matched it up by brand. To do this, I used Home > Tables > Merge Two Tables. Over in the Object Inspector, I nominated the tables to merge. In this case, it was table.Q3.Preferred.cola and table.output. I selected Side-by-Side and Matching Only as my options. Because I want the R Output to sort the data from highest to lowest, I added some lines of code to the merged object. I did this by selecting the merged table, going to Properties > R CODE in the Object Inspector, and adding this final line:
merged[order(merged[,1], decreasing = TRUE),]. If you're not familiar with this process, I recommend reading the blog post How to sort your data with R in Displayr.
Finally, in my example, I extracted the first column of the merged table because the visualization needs it separate from the URL text. I did that with the following code within another R Output (Insert > R Output). The name of this output is by default sorted_data.
sorted_data = as.numeric(merged[,1]) sorted_data = as.data.frame(sorted_data) rownames(sorted_data) = rownames(merged) sorted_data
Create your visualization
Now insert your visualization and link it up to the data table. In my example, I selected Insert > Visualization > Bar Chart. I hooked it up to the R Output sorted_data. If you're not sure how to set up a visualization, I recommend reading How to Create a Bar Chart in Displayr (noting the Visualization section is the relevant section, not the Charts section).
Create holders for your images
Now, create another R Output, and then use the following code. You will need to change the references in the first three lines. The first line tells us which order the item is (in this example, it is the first). The second and third line reference the merged R Output, including the column that has the URL. If you're not familiar with table subsetting and referencing with R Output, I recommend reading How to do Simple Table Manipulations with R in Displayr.
item = 1 src = merged[item,"URL"] alt = rownames(merged)[item] text = paste0( '<head> <meta name="viewport" content="width=device-width, initial-scale=1"> <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cstyle%3E%0A.responsive%20%7B%0Awidth%3A%20100%25%3B%0Aheight%3A%20auto%3B%0A%7D%0A%3C%2Fstyle%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&amp;lt;style&amp;gt;" title="&amp;lt;style&amp;gt;" /> </head><body><img src="', src, '" alt="', alt, '" class="responsive"> </body>') rhtmlMetro::Box(text,text.as.html = TRUE)
Next, resize your R Output. The image is set to responsively adjust in size to fill the R Output container (that's why there is lots of HTML code cited within the R code above!) Then duplicate your R Output (Home > Duplicate) and adjust the first line as necessary (eg:
item = 2,
item = 3, etc) in each successive R Output. Finally, align them next to the visualization.
Try for yourself
The worked example can be found in this Displayr document.