Each built-in Displayr table has associated metadata available to R under the hood. These attribute parameters can be called via R using the attributes command, see How to Extract Information From an Item Using R. Some of these attributes include statistic (the statistics shown), spans and other labels in the table, base information, and stat testing info. When performing R table calculations, there will be times when these attributes will carry over to your custom tables, but you may wish to alter some of them or add them back in.
One common example is to change the statistic of an R table to a percentage statistic so the data is plotted as % in a visualization. This article describes how to update the statistic attribute in an R table from Count:
to %:
Requirements
- A basic table created in R (not a table created with an R function like CreateCustomTable() or DT()).
Method
For this example, we will use the following two tables to calculate a new R table (pref below) of preference based on awareness:
1. Select Calculation > Custom Code and draw a box on the Page.
2. Paste the below under General > R Code:
#calculate preference based on awareness
pref = table.preference / table.awareness * 100
Because the source tables (above) are both using Count, the final result also shows as Count in the column header and we can't add a % symbol.
3. To change the underlying statistic, we can use the attr command and then return our updated pref table, add the following code to the bottom of the code above:
#set the statistic attribute to % for the new table
attr(pref,"statistic") = "%"
#return the final table in the output
pref
4. Press the % symbol under Apperance > Appearance > Number format and the table will display as expected.
Notes
We can confirm the attribute parameters in the original R table by calling the following:
#see all the attributes of the table
attributes(pref)
Here, you'll see the statistic parameter was changed to %. You can see other attributes available that you can use in other custom R code.
Next
Creating and Modifying Visualizations
How to Add Row Spans to a CreateCustomTable R Table
How to Use Paste Functions to Create Dynamic Text Using R
How to Add Statistical Significance to CreateCustomTable R Tables