This article describes how to go from a standard visualization:
To a visualization that includes images, such as brand logos, which will update along with the visualization if the data changes:
Requirements
- A visualization in Displayr you want to link to the images.
- Images from a publicly accessible URL location (eg, imgur, Google Photos, Dropbox, etc.). A publicly accessible URL will load when anyone inputs the address into a web browser without any authentication or logging-in required.
- Comfort working with simple R code. See How to Learn R to get started with using R in Displayr.
Method
- Create a table that contains the URLs of the images by going to Table > Paste or Enter Table.
- From the object inspector, go to Data > Data Source > Paste or type data.
- Paste or type the item name and its associated URL into the sheet and click OK:
- If your table first appears with numbers in the first column, as seen below:
You will need to go to Data > Data Manipulation and untick Automatically detect row and column names and select First row contains column names and First column contains row names so that your table appears as:
5. Create a table from the variable that you'll use to create the visualization. You can do this by dragging the variable onto your page:
6. Select both of the tables that were created in Steps 4 and 5 above, right-click, and select Combine.
7. From the object inspector, ensure that Data > Combine Tables > Join direction > Side-by-side and Non-matching rows/columns > Matching only is selected.
8. Make a note of the merged table's name in General > General > Name. You'll need it in the next step.
9. To sort the data from highest to lowest, you'll want to add some lines of code to the merged object. Select the merged table, and from the object inspector, go to Data > Show Advanced options > R Code and add this final line:
merged[order(merged[,2], decreasing = TRUE),]
In the example code above, merged is the name of the combined table recorded in the previous step. Replace merged in the code with the name of your table. Also in the example code, "2" indicates that the table should be sorted on the second column of data. You may need to update this based on the column position of your numeric data.
10. The second column of the merged table will need to be extracted for the visualization. From the toolbar, go to Calculation > Custom Code, then click on the page to place the custom calculation.
11. Paste the following into the R Code area:
sorteddata = as.numeric(merged[,2])
sorteddata = as.data.frame(sorteddata)
rownames(sorteddata) = rownames(merged)
sorteddata
In the first line of code, I have used "2" to reference the second column in the merged table again.
12. Go to Visualization > Bar > Bar (or the visualization type of your choice).
13. From the object inspector, go to Data > Data Source > Data and select the output created in Step 11.
14. Next, you will create the outputs that contain the images for your visualization. Go to Calculation > Custom Code and click on the page to create the custom calculation.
15. Paste the following code into the R Code area:
item = 1
src = merged[item,"Src"]
alt = rownames(merged)[item]
text = paste0(
'<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.responsive {
width: 100%;
height: auto;
}
</style>
</head><body><img src="',
src,
'" alt="',
alt,
'" class="responsive">
</body>')
rhtmlMetro::Box(text,text.as.html = TRUE)
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 (Coca-Cola)). The second and third lines reference the merged R Output created in Step 6, including the column that has the URL.
16. Resize the output to fit with your visualization.
17. Make a copy of the first image output by right-clicking it and selecting Duplicate.
18. Update the first line of code to item = 2 to reference the second row of your merged table.
19. Repeat Steps 17-18 for the remaining images that are needed, updating item each time and aligning them to your visualization.