Most times, control boxes are used to filter outputs on the page for specific characteristics, but they also can also be used to select what table (or other output) to use in an item. This can be helpful when choosing between what table to use as the input to a visualization, such as when you create a control box to pick which variable set to show in an output. There is a built-in feature Table > Select that can do this between tables, but you can also write simple if-else logic using R code to select which table to show based on the control box. This article describes how to go from two or more tables...
...to an R table that switches between them based on a Control box selection:
Requirements
- Two or more tables. Here, we will use date-by-gender and age-by-gender tables. If using the Table > Select feature, the table will only show one statistic. Otherwise, custom R code can show tables with multiple statistics.
- A Control box or an R-based Calculation that outputs the names of the tables. Here, we will use a combo box that includes the table names of Date and Age:
Method - Table > Select
1. Select your first table, for example, the age-by-gender table. Go to Table > Select in the toolbar. You can also manually select the table under Data > Table 1 > Tables in Page(s) in the object inspector, if you first create a table via Table
> Select. Here, we will use our date-by-gender table.
2. OPTIONAL: Choose the appropriate Select rows/columns by option in Table 1. In this example, we will leave the default settings.
3. OPTIONAL: Select the rows and columns to display under Rows/columns to exclude in Table 1. Again, we will leave the default settings.
4. Select your second table under Data > Table 2 > Tables in Page(s). This will be our age-by-gender table.
5. Adjust the appropriate selection options as per steps 2 and 3. In this example, we will keep all the defaults for Select rows/columns by.
6. OPTIONAL: Add a third table under Data > Table 3 > Tables in Page(s) and repeat the above steps until you have included all tables.
7. Once you select more than one table, a Table Selection group will appear at the bottom of the object inspector.
8. Enter the name of the tables under Table names (default 1, 2, ...) to match the control box items, i.e. Date, Age
. The table names need to be separated by a comma. They must match the order specified in the control.
9. Select your control box under Toggle between tables using. The item names in the control should match Table names.
10. OPTIONAL: Update Properties > Appearance > Decimals and Number format as required.
Method - Custom R code
If you want to switch between tables with multiple statistics (or other items in your Report), you can write R code that uses simple if-else logic to select the table to show based on the control. You will create a Calculation > Custom Code output, and write code similar to the following:
#specify the control box to use to select the table
cb=Question.control.box
#write if else logic for what table to show based on the label in the control
if (cb == "Date") table.Date.by.What.is.your.gender else
if (cb == "Age") table.Age.by.What.is.your.gender
More detail on this is in Method 2 of How to Dynamically Change a Question Based on a Control Box.
Next
How to Select Specific Rows and Columns to Appear in a Table
How to Select Specific Rows and Columns to Appear in a Date/Time Table
How to Use a Control Box as an Input for Calculations