You can restrict what people see on a Displayr dashboard based on their department, geographic region, or some other user characteristic. For example, you can create a single report to present the results of an employee survey, share it with the managers in your business, and each manager will only see results for their departments.
In Displayr, people's logins can be organized into user groups. These groups are often used to determine which documents a person can view or edit. The user groups are also visible to the code in your document. Thus, you can create a document that responds and changes based on which user groups the viewer belongs to. In this article, you will be taken through a simple example of a dashboard that shows different results based on the user's location.
Requirements
A Displayr account with multiple user groups. For more information see: How to Change to a Multi-User Account
Comfort writing custom R code. To start learning R, see: How to Learn R
Method
Part 1: Set up User Groups
If the dashboard is to be filtered according to a person's user group, then you need to begin by creating user groups and assigning your users to them.
To create a group:
- Log in to Displayr.
- Click on the Account icon in the top right
and select Account Settings.
- Select the Settings option
- Scroll down to the Groups section and select + New Group.
- Enter a Group name
- Un-tick Users in this group can Explore. (Explore mode allows people to see other data in the data set, so you need to turn this off).
- Click Save.
In this article, I am creating a document that will filter results by Australian states, so I have created user groups in my account for each state that my stakeholders come from. The result looks like this:
In addition to the default user groups (Administrators, Edit Projects, and View Projects) there is a separate group for each state (ACT, NSW, NT, etc).
Part 2: Assign your users to groups
Next, in order to ensure that people only see results for a subset of the sample, each user requires an individual login and a correctly assigned user group. Importantly, you should avoid assigning these people to any groups that have access to the whole document. It will depend on how you have organized your groups, but typically, restricted users should NOT be part of the default groups like Administrators, Edit Projects, or View Projects.
You can assign a user login to a user group when you first invite them to Displayr. To do so:
- Go to your account Settings page.
- Click the + New User button in the User section.
- Enter the person's Name, Email, and any Notes.
- Choose the appropriate group in the Group membership section. You can choose more than one group by holding CTRL and clicking.
- Click Save.
In the example above, the user Chris F will be added to the NSW user group when he accepts his invitation to join this Displayr account.
You can also change a person's group membership later on. To do so:
- Click the person's name under the Users section.
- Fill out the form as above.
- Click Save.
Part 3: Create a Calculation to identify the user group
Within your document, every page must contain a Calculation that accesses the user groups for the person who is currently viewing the page. One example of the code that you can use is as follows:
- Select Calculation > Custom Code and click on the page.
- Paste the code below into the R CODE section.
- Click Calculate.
- Hide this output by selecting Toolbar > Hide.
The code I am using in this example is:
UserGroups = QViewModeUserGroups
if (is.null(UserGroups)) {
UserGroups = c('NSW', 'VIC')
}
if ("Administrators" %in% UserGroups) {
UserGroups = c('NSW', 'VIC')
}
UserGroupsPage1 = UserGroups
The output will look like this in Edit mode, but will not be visible when viewing the document as it is hidden:
Some notes on how this code works:
- The special property QViewModeUserGroups is used to obtain the user groups.
- If you are in Edit mode, then the QViewModeUserGroups property returns a value of NULL. So the second part of the code checks for a NULL value, and in that case, manually assigns user groups NSW and VIC (for New South Wales and Victoria in Australia). When you want to test the appearance in Edit mode, this is the line to change.
- The third part of the code determines what account administrators will see when viewing the document. Again, here I have assigned NSW and VIC, and this line can be changed when you want to see what a person from a specific user group will see.
- The final line assigns the name of this output to be UserGroupsPage1. As mentioned above, an output like this needs to be present on every page that you want to filter for the user. It is good housekeeping to name the output according to the page it is used on, particularly when you are making a document with many pages.
Part 4: Create a filter based on the user groups
For each page you wish to filter by user, you need to construct an R Variable which reads the user groups identified in Part 3, and uses that information to make decisions about which cases should be shown. As I am working with a very simple example, my filter will simply match any respondents whose State variable from the survey matches any of the current user groups.
- Hover between any two variables in the Data Sets tree until a + symbol appears.
- Click the + sign, and select Custom Code > R-Numeric.
- Enter code like the example below into the R CODE section in the object inspector.
- Click Calculate.
- Enter a Name and Label for your new variable. Again, as this needs to be done for each page, it is good practice to label your filter according to the name of the page on which it is to be used.
- Tick the Usable as a filter box.
- Tick the Hidden except in data tree box.
State %in% UserGroupsPage1
Some notes about this code:
- I am using the State variable from my data set. This variable has categories which are the states of Australia (NSW, VIC, SA, etc). For simplicity, I have deliberately chosen my user groups to match the categories of this variable.
- I have used the %in% operator. This checks each respondent's State category and works out if it is included in the set of user groups for the person currently viewing this page.
- Depending on the setup of your user groups and how you need to work out which respondents should be included in the sample for each user group you will likely need to use more complicated logic.
Part 5: Filter your page
To make use of your new filter you need to apply it to the outputs on your page.
- Holding CTRL on your keyboard, click on all of the tables and visualizations which need to be filtered.
- In the Object Inspector, use Inputs > FILTERS AND WEIGHT > Filter(s) to apply this filter variable.
Part 6: Edit the document settings
Now that you have a document ready to go, you need to ensure that all of the user groups have been given permission to view this document.
- Click on the name of your Displayr Document and then select Document Settings.
- A new tab will open. Expand the Properties section.
- In the Authorised for view only section, select all of the user groups that you created for this document in Part 1 above.
- Click Save.
You can't see the whole menu at once, but my selections for this project for step 4 look like this:
Part 7: Publish
Finally, you need to publish your document as a Private Web Page. To do so:
- Select Publish > Publish as Webpages.
- Under Who has access, choose Login and password required.
- Click Publish (or Republish).
The link will now be available to copy and share with others, the Open in new tab button will take you to the published version, and the Save button will return you to your document.
Testing
There are two main approaches for testing the function of your dashboard. The first is to use a testing account as follows:
- Add a test user (using the steps in Part 3 above) with an email address that is different from the one you use to log in to Displayr. It should be an email account that you have access to so that you can confirm and change the password.
- Add the test user to the group that you want to test.
- Log out of Displayr and log in again with this test account, or start a Private Browsing or Incognito window in your browser (CTRL + SHIFT + N in Chrome or CTRL + SHIFT + P in Firefox) and log in with the test account.
To test a different group, simply modify the group membership of your test account (as in Part 3 above).
An alternative is to modify the code in Part 3 to determine which group name is provided to Displayr when you view your document from your Administrator account. This will allow you to test different scenarios by modifying the code.
Next
How to Publish a Document as a Web Page (Dashboard)
How to Control who Can View Your Documents in Displayr
How to Control Who Can View Pages in Your Document
How to Partition a Document for Multiple Stakeholders
How to Restrict User Access to Individual Pages within a Document
How to Give Groups of Users Access to a Document