A very common way to use R in Displayr is for filtering. You can create an R filter variable to filter outputs on the page or add that same criteria/logic within a Calculation to create your own custom outputs on the page directly. Though more options are discussed at the bottom, this article specifically describes how to go from a table of raw data...
...to a filtered table based on a defined condition:
- A raw data table
- Can create a data.frame of raw data in a custom Calculation using something like, data.frame(q1a, q3), see How to Work with Data in R.
- Can use a native table of a single variable set showing Columns > RAW DATA.
- Or use Anything > Table > Raw Data > Variables. See How to Export Raw Data to Excel for details on creating the latter. In this example, our table is called cola.raw.data.
- An understanding of how to construct conditional formulas in R, see How to Work with Conditional R Formulas and How to Use R Code to Create a Filter Based on Single-Response Questions.
Method - Creating a custom Calculation
In this example, we wish to filter our table by those who chose Coca-Cola as their preferred cola.
1. Select Calculation > Custom Code.
2. Paste the below under Properties > R CODE:
#identify the table being used name it df for the code
#or create one with data.frame(variablename1, variablename2)
df = cola.raw.data
#use brackets to subset the rows (before the ,) of the table where the second column
#df[,2] is equal to Coca-Cola. The %in% operator accounts for missing data
#You can put other values separated by , within quotes in the c()
new.df = df[df[,2] %in% c("Coca-Cola"),]
Note, you can also add further items to the filter condition as follows:
#rows where the second columns contains Coca-Cola or Coke
df[,2] %in% c("Coca-Cola","Coke")
If you have multiple conditions, it's useful to define those outside the bracket subsetting first:
#identify the raw data table being used name it df for the code
df = cola.raw.data
#keep coke drinkers
coke = df[,2] %in% c("Coca-Cola","Coke")
#remove pepsi drinkers
nopep = !df[,2] %in% c("Pepsi")
#use brackets to subset the rows (before the ,) of the table that meet criteria above
new.df = df[coke & nopep,]
Method - Creating an R filter variable
To create a filter variable that can filter your raw data table (and other outputs), see How to Use R Code to Create a Filter Based on Single-Response Questions.