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 and 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 group 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
- Experience writing custom R code. To start learning R, see: How to Learn R.
- Administrator-level access to your Displayr account if creating new user groups or inviting new users.
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:
- Click on the initials icon in the top right () and select Account Settings.
- Select the Settings tab.
- Scroll down to the Groups section and select + New Group.
- Enter a Group name.
- Untick 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, Create/Edit Documents, and View Documents) there is a separate group for each state (ACT, NSW, NT, etc).
Part 2: Assign your users to groups
Next, 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, Create/Edit Documents, or View Documents.
You can assign a user login to a user group when you first invite them to Displayr. To do so:
- Go to the Settings tab.
- 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 Cristin X will be added to the NSW user group when she accepts her 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.
- Update the Group membership.
- Click Save.
Part 3: Create a Calculation to identify the user group
Your document 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:
- Click Calculation and then Custom Code from the toolbar and click on the page to place the calculation.
- Paste the code below into the R Code section.
- Click Calculate.
- Hide this output by right-clicking the calculation and selecting Hide.
The code I am using in this example is:
#Set a new calculation UserGroups to the groups the current login is a part of
UserGroups = QViewModeUserGroups
#if UserGroups is null that means you are in edit mode and should see all respondents
if (is.null(UserGroups)) {
UserGroups = c('NSW', 'VIC')
}
#Administrators should see all respondents as well
if ("Administrators" %in% UserGroups) {
UserGroups = c('NSW', 'VIC')
}
###You can add more sections of logic like above to set UserGroups to the appropriate groups IF
###the name of their UserGroups isn't a category to use in the filter
#call the final UserGroup at the end
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 Displayr variable QViewModeUserGroups is available to Calculations on the page.
- 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 for a particular filter group, 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 calls the values of UserGroups.
Part 4: Create a filter based on the user groups
Now that we have an output that declares what User Group the login is a part of, we need to construct an R Variable which uses the group names 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 (as I've named my user groups the categories in the State variable).
- Hover between any two variables in Data Sources until a + symbol appears.
- Click the + and select Custom Code > R-Numeric.
- Enter code like the example below into the General > 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 in View Mode Filter menu box.
State %in% UserGroups
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 am using UserGroups since that is the Name of the custom calculation that I created in Part 3 above.
- 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. You should also use %in% if you suspect there is missing data for the variable being used.
- 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 pages.
- Holding CTRL on your keyboard, click on all of the tables and visualizations which need to be filtered. You can also select whole Pages or Folders to apply the filter to all outputs (if that is easier).
- In the Object Inspector, use Data > 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 Tools > 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 Share > Publish to web.
- 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