Please note this requires the Data Stories module or a Displayr license.
Most Displayr outputs will generate their own caption (footer) or inherit it from their source table. However, sometimes an output that manipulates tables or a custom one you create doesn't include a caption. Other times, you may want to further customize the caption outside the options in our Edit Caption menu (this must be done on the original Table input), see How to Show / Hide / Edit Captions for Tables and Charts. If your output is an R-based output, you can add and customize its caption within the Data > R CODE by using the "footerhtml" attribute. This article covers these examples:
- Add and format a Caption
- Edit the current Caption
- Include a link in a Caption
- Reference the Caption of an input
and includes Technical Notes at the bottom.
So you can go from an R-based output without a caption like:
to an R-based output with a customized caption like:
Requirements
- Knowledge of what things you can customize with Displayr's standard captions, see How to Show / Hide / Edit Captions for Tables and Charts.
- Familiarity of How to Work with Data in R and How to Change Table Attributes in R Tables.
- An R-based output on the page such as a table, dynamic text, visualization, and more.
Add and format a Caption
You can add a caption to an R-based output by adding a "footerhtml" attribute to the final output. Once the Caption is created with the code, you can format it using the Appearance > APPEARANCE menu, and override the text from the code by manually typing in the Caption shown - just like our standard Captions. Our How to Use Paste Functions to Create Dynamic Text Using R article also shows some examples of how you can use the paste function in R to create dynamic text for your Caption.
For example, I have two tables that I'd like to combine and add a footnote to:
Using a custom R Calculation:
- Click on Calculation = and click on the page to draw the output.
- Paste in the following code:
#identify the tables to merge (you can place your cursor
#in the code below and click on them)
tab1 = table.Preferred.cola
tab2 = table.Brand.attitude.2
#merge them using the row.names keeping all rows
#the merge done below is based on both input tables having one column and
#the row names of each table are the same
merged = Cbind(tab1,tab2)
#add custom column headers if wanted
colnames(merged) = c("Preferred","Attitude")
#make a data.frame for easy handling
merged = data.frame(merged, check.names=F)
#round and add back percentage in first column
merged[,1]=paste0(round(merged[,1],0),"%")
#show final table
merged - The final output in the code above is named merged so this is the object we need to add the "footerhtml" attribute to. On the blank line before we call our final table we need to insert the following code:
#add footnote change merged below to your final output name
#put your Caption text in side "" on the right
attr(merged,"footerhtml") = "This is my footnote."
Using a standard R-based output from the menu
Some of our R-based outputs may not automatically include a Caption. You can modify the Standard R code, you just need to find the name of the object to add "footerhtml" to. This will normally be the General > GENERAL > Name of the output, but can be different. You'll need to look for the last thing that is created with an = in the code.
So you would use either combined.tables or viz.2 in the code below to add the footnote:
#add footnote change combined.tables below to your final output name
#put your Caption text in side "" on the right
attr(combined.tables,"footerhtml") = "This is my footnote."
#call final output
combined.tables
Formatting
You can click into the Caption you add and highlight the text to format it or manually override any text created in the R code. Formatting the Caption is done through the Appearance > APPEARANCE menu.
Edit a Caption
R-based visualizations and some other R-based outputs will inherit the Caption of its input table. You can edit the standard Caption on the input table (see How to Show / Hide / Edit Captions for Tables and Charts), or for something more customized, you can edit the Caption within the R code. Similar to adding a Caption, you just need to add a bit of lines at the end of the R code, and you can replace the text entirely, use paste0() to append text to the current text, or use some other type of R formula to edit the Caption.
You can start with a visualization that looks like:
Then add the following lines to the end of the Data > R CODE where viz.2 is the name of the output being created (see Using a standard R-based output from the menu for how to find this name):
#edit the footnote to append custom text
#change viz.2 below to your final output name
attr(viz.2,"footerhtml") = paste0(attr(viz.2,"footerhtml"),"\nAdd custom text to the bottom.")
#call final output
viz.2
Include a link in a Caption
Because the "footerhtml" attribute can render HTML code, you can also add links and other HTML elements to your Caption. For example the code below adds a link to the Displayr website to the Caption of a merged table, but you could include other links like to the sources of your data as well:
#add footnote change merged below to your final output name
#put your Caption text in side "" on the right
attr(merged,"footerhtml") = "This is my footnote.\nCreated in <a href='http://www.displayr.com'>Displayr</a>."
#show final table
merged
Reference the Caption of an input
When you create a custom R Calculation, you may want to reuse the same Caption as was on one of the inputs used in the code. You can do this by reusing the "footerhtml" or other attributes from your inputs. In the example above where we manually merged together two tables, you can use one of the original Captions as the Caption on the combined table with the following code:
#add footnote change merged below to your final output name
#put the other table or output to reference for the Caption on the right
attr(merged,"footerhtml") = attr(table.Preferred.cola,"footerhtml")
#show final table
merged
You can pull of other information from inputs as well, see How to Change Table Attributes in R Tables and How to Extract Information from an R Object. For example the below only shows the sample size based on the preferred table:
#add footnote change merged below to your final output name
#put the other table name or output to reference for the Caption on the right
#change "basedescriptiontext" to the appropriate attribute
attr(merged,"footerhtml") = attr(table.Preferred.cola,"basedescriptiontext")
#show final table
merged
Technical Notes
- Once a "footerhtml" Caption is added with the code, the Caption shown on the output behaves as a Standard Caption in regards to formatting with the Appearance menu and manually editing the code in the output.
- Some R-based outputs have footers written to the output itself and is not created like our standard Caption, such as footers on regressions and other types of models. You can add a "footerhtml" to these outputs to create a standard type of Caption, but you will not be able to customize the footer that's already included on the output this way.