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 that 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
- From the Data Sources tree, click + Add Data or click + to add a new data source.
- In the Where is your data window, expand the Advanced data file options section.
- Set the Data format to Summary table.
- Click Paste or Enter Data.
- Paste or type the item name and its associated URL into the sheet and click OK:
- A new summary table will appear in the Data Sources tree. Select it and drag it onto a page to create a table.
- Select the rows and columns you want to include. You can click in the upper-right cell to select all rows/columns.
-
Click Save.
-
Drag the variable you'll use to create the visualization onto the page to create a table.
-
Select both of the tables that were created in Steps 8 and 9 above, right-click, and select Combine.
- From the object inspector, ensure that Data > Combine Tables > Join direction > Side-by-side and Non-matching rows/columns > Matching only is selected.
- Make a note of the merged table's name in General > General > Name. You'll need it in the next step.
- 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 > Edit Code and add this final line:
merged[order(merged[,1], decreasing = TRUE),]In the example code above, merged is the name of the combined table copied in the previous step. Replace merged in the code with the name of your table. Also in the example code, "1" indicates that the table should be sorted on the first column of data. You may need to update this based on the column position of your numeric data.
14. The column containing the sorted numbers 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.
15. Paste the following into the R Code editor:
sorteddata = as.numeric(merged[,1])
sorteddata = as.data.frame(sorteddata)
rownames(sorteddata) = rownames(merged)
sorteddataIn the first line of code, I have used "1" to reference the first column in the merged table again, and merged will need to be replaced with the name of the table copied at Step 12.
16. Go to Visualization > Bar > Bar (or the visualization type of your choice).
17. From the object inspector, go to Data > Data Source > Data and select the output created in Step 15.
18. 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.
19. Paste the following code into the R Code editor:
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 copied in Step 12, including the column that has the URL.
20. Resize the output to fit with your visualization.
21. Make a copy of the first image output by right-clicking it and selecting Duplicate.
22. Update the first line of code to item = 2 to reference the second row of your merged table.
23. Repeat Steps 21-22 for the remaining images that are needed, updating item each time and aligning them to your visualization.