This FAQ lists frequently asked questions about how to use R and what its capabilities are in Displayr. Questions range from basic to more technical in nature.
- Where do I put the R script I use to format and analyze my data?
- How can I use an R script to do various things in my document?
- How can I insert an R Output into my Report?
- Why do I need to specify how many R variables to create?
- Can I use Shiny in Displayr?
- How do I get my R code to run automatically?
- Why isn't my R code working as I expect?
- Can I use my own functions or R package in Displayr?
- How can I read or save a csv to my computer?
- How do I access the values or labels of my Nominal variable?
- How can I reference my entire data set within R?
- How can I use R to modify one of my variables?
- When should I create an R variable versus creating a variable differently?
- When should I NOT create an R variable versus a different type of variable?
- How do I get my data into R?
- When should I use R to customize my table versus a different method?
- What is the best way to create a customized table using R?
- Why isn't my dplyr or tidyverse code working as expected?
- When should I use a data.frame versus a matrix in R?
- How can I access other metadata about an output or variable, such as variable set name, underlying values, and labels?
General questions
Where do I put the R script I use to format and analyze my data?
There isn't an "overall" or "master" R script for a document. In Displayr, you use R code to create individual variable sets, outputs (Calculations), and data sets, see How to Use R in Displayr. There are also many ways that you can modify and analyze data without using R code within Displayr that are processed more efficiently. You will need to assess what parts of your R code can be setup without R code, and with the remaining code, create the variable sets/outputs individually. Note that you will also need to modify your code to reference the data/items in Displayr vs the names in your original script, see How to Reference Different Items in Your Document in R.
How can I use an R script to do various things in my document?
In Displayr, R is used to create individual variable sets, outputs (Calculations), and data sets, see How to Use R in Displayr. So you can do various things within an individual item, but not across items with a single script. Displayr uses QScripts to automate creating and modifying data and outputs, see How to Use QScripts in Displayr. These can create individual R-based items automatically.
How can I insert an R Output into my Report?
R Outputs in Displayr are called Calculations. You can insert a new R Output by clicking Calculation > Custom Code in either the right click menu in the Report tree (to make the output standalone) or the toolbar (to put on a Page). Note that many visualizations and analyses created via other features in the menu are also based off R code. You can check this by clicking the Show Advanced Options in the object inspector to see if there is an Edit R code button present.
Why do I need to specify how many R variables to create?
R is used within individual items to perform calculations, so it doesn't have the ability to actually create a new variable object into the Data Sources pane. You need to manually create the R variable(s) via the menu, then use the R code to "fill" them with the appropriate values, see How to Create a Custom Numeric R Variable and How to Create Multiple R Variables.
Can I use Shiny in Displayr?
Shiny is not compatible within Displayr for various technical reasons. Despite that, Displayr was created to build interactive data apps and dashboards, and thus has many of the same features available, for example How to Use a Control Box as an Input for Calculations shows you how to create inputs users can modify to change the data used in analyses.
How do I get my R code to run automatically?
All R-based items will update automatically when the data they are dependent on is updated and the item is accessed (i.e. a page that uses the data is loaded) or document is Republished. However, you can also update an R item merely referencing a variable that will be updated (even though you don't use it in your final output) or by adding it to a schedule, see How to Automatically Update R Outputs on a Schedule.
Why isn't my R code working as I expect?
Keep in mind How R Works Differently in Displayr Compared to Other Programs. Given that, many times this has to do with how variables are referenced and the appropriate syntax for how data is structured. For example, R code uses the Labels of categories for Nominal variables and not the underlying Value. Also, referencing a cell in a single column table is [row]
, multiple row/column table is [row, column]
, and multiple row/column/statistic table is [row, column, statistic]
. These issues can be uncovered quickly in the Code Editor by hovering over a highlighted variable name in the R code to see what data is shown in the preview and by clicking on an item or cell in a table to automatically insert a reference to it in the code. If more troubleshooting is needed, there is an entire article that goes through How to Troubleshoot R Code in Displayr in more detail, as well as an article that explains Common Error Messages in R Code. There can also be issues with how variables are labeled when working with data using dplyr or tidyverse functions, see Why isn't my dplyr or tidyverse code working as expected? below.
Can I use my own functions or R package in Displayr?
Yes, you can create standalone functions or a "pseudo" package in R. How to Create Custom R Functions and How to Share Custom R Functions in Displayr outline those methods. Note that if you need an open source package on CRAN to be installed, you can send in a request to support@displayr.com.
Working with data
How can I read or save a csv to my computer?
Because Displayr uses remote R server to process R code, see How R Works Differently in Displayr Compared to Other Programs, you cannot read or save data files to your computer directly using R. Most commonly, users will read/save the csv to their Displayr Cloud Drive, see How to Access the Displayr Cloud Drive Using R. Otherwise, you will need to write custom R code to access and possibly login to whatever other remote server you need.
How do I access the values or labels of my Nominal (categorical) variable using R?
Nominal variables have a value assigned to each category in its Value Attributes (also known as a code frame). This is useful when you have variables that you may want to analyze as a number as well as category (i.e. an Income variable where each income value is assigned a category like lower, middle, higher). When you want to use the underlying values of a category in your R code, you cannot use the code as.numeric(thevariable)
to get the actual values in the data of each category (this will give you the order of the categories in your tables). The easiest way to get those is to Duplicate the variable, change the Structure > Numeric, and use this new variable directly in your code to access the actual values assigned to each category for each case. If you prefer to do this via code see below.
If you have renamed or combined categories on tables directly, you will first need to use the Combined Categories for Weighting automation to save a copy of the variable set with the combined and renamed categories. Next, manually assign a value to each category in the Value Attributes. Then you can use the code below to get the values you've manually assigned.
If you haven't renamed or combined any categories on tables directly, you can get the underlying values by accessing the values attribute and pulling off the value for each respondent using the category label:
attr(thevariable, "values")[as.character(thevariable)]
How can I reference my entire data set within R?
You cannot reference your entire data set in R in Displayr, see How R Works Differently in Displayr Compared to Other Programs. You have to reference variable sets by name directly in your code to be able to use them, see How to Reference Different Items in Your Document in R. This is because Displayr has to send the data along with the R code to our R servers for processing, and to send entire data sets would be too slow. If you want to perform an operation that loops across all the variables in your data set and modifies/creates lots of new variables etc, you will want to write a custom QScript to do that, see How to Create a Custom QScript.
How can I use R to modify one of my variables?
R cannot modify variables directly (though QScripts can for specific edits). You will need to create a custom R variable or Multiple R variables variable set to create a modified copy of your variable(s).
When should I create an R variable versus creating a variable differently?
- You need calculations that require statistical functions, more complex calculations, calculations on subgroups, or lots of data manipulation. That is, the built-in Math functions in JavaScript are more basic/akin to buttons on a calculator (i.e. there isn't an Average function).
- You need to reference tables, combo boxes, controls, or other outputs in your Report.
- You do not want your filter to become invalid if a new category appears in updated data for a variable that is used.
- You're creating a filter has logic that requires lots of intermediate calculations.
- You need calculations that uses data across all cases in your data set. JavaScript variables can do this, it is just harder code to write and less efficient than R.
- You have million+ cases in your data set. JavaScript variables tend to be slower on data sets with millions of cases because they do not do vectorized operations like R.
- You need to convert a variable with dates in a very custom date format into a Date/Time variable. However, if your date format is basic you may be able to use JavaScript, see How to Create a Date Variable from a Categorical Variable Using JavaScript
When should I NOT create an R variable versus a different type of variable?
Because R items are processed on remote R servers, most times it's faster and more efficient to use other methods of creating variables. Creating tons of R variables in a document may even slow down the document. You should try to avoid creating R variable when:
- You need to recode values on a variable. Do this in the variable's Value Attributes or via a JavaScript variable.
- You're creating a filter variable or recode a variable using straightforward logic. Create a variable using the filtering tool or JavaScript for this.
- You want to use a variable set to filter outputs. There are many automations that can help convert your variable set into one that can be used to filter outputs. See How to Tag a Variable as a Filter for a table based on the structure of your data and required filtering.
- You need to create variables that only need simple math and logic, use a JavaScript variable for this.
- You want to rebase a variable set. There are automations that do most rebasing needs, see How to Rebase a Table in Displayr.
- You need to identify duplicate cases based on a variable(s), see How to Identify Duplicates. However, for more complex logic, see How to De-duplicate Raw Data Using R.
How do I get my data into R?
The easiest way to reference data in your Data Set or and item in your Report in R is by 1) clicking into the Code Editor and 2) clicking on the output/variable/row/column/cell you want to reference, see How to Use Point and Click Inside R Code for a demo. There are also various syntax to use to reference items in your document, see How to Reference Different Items in Your Document in R.
When should I use R to customize my table versus a different method?
There are many options built-in to format tables and create calculations on them, see How to Customize the Formatting of Tables in Displayr and How to Use Rules in Displayr. However, depending on your calculation or formatting needs you may need to create a table with R code. See How to Select the Type of Table that is Right for You for a full list of functionality for the different table types. Notable use cases are below:
You may need to use the Table with Custom Formatting (which is R-based but has a menu with basic options) if you need to:
- Select rows or columns in a table based on a combo box or other control, and do basic data/row/column manipulations (you can also use a Table > Paste or Enter Table for this).
- Hide all the row or column headers.
- Align the table with items on the page.
- Set specific column widths.
- Change alignment of headers and cells.
In addition to the features above, you may need to create a table from scratch using Displayr's CreateCustomTable() function in R if you need to do any of the following (all customizations are listed here):
- Dynamically change formatting on a cell-by-cell, row-by-row, column-by-column basis. This could be font styling and colors based on values (or other things) in the table. Note there are rules to highlight cells in drag and drop tables, but the logic for highlighting is usually pretty basic.
- Combine separate tables with different data types (i.e. text values & percentages) or statistics (i.e. percentage from one table and count from another).
- Include bullets, links or images in your table. This includes aligning images with bars on a graph or other items on a page.
Note that the downside to working with these tables is that they work with one statistic, don't show stat testing unless you write code for it, and having limited span functionality.
What is the best way to create a customized table using R?
Basic R tables (data.frames or matrices that you create via R code) offer cell formatting like drag and drop tables and some table rules can be applied. If you are looking for a more dynamic or polished look Displayr's CreateCustomTable() function is the best balance of having easier syntax while still offering a ton of features and customizations (see the section above for a few examples). This function was also created by the Displayr team so our Support team is able to offer more coding support for it than other packages. That said, there are a couple other examples of using DT, formattable, and creating an html table within R, but the code is a bit more complex.
Why isn't my dplyr or tidyverse code working as expected?
The most common reason why dplyr or tidyverse functions may not work as expected is when variable names from your data set match column headers/variables in the data used in those functions. For example, you have a variable id in your data set and you use group_by(id)
in your R code. When you create a data.frame of variables to analyze using those packages, you will need to ensure the column names in the data set you use do not match any variable names in your data set, such as thedata = data.frame(theid=id)
.
When should I use a data.frame versus a matrix in R?
There is more detail about this in How to Work with Data in R, but some reasons to use a data.frame over a matrix when coding is:
- You can combine data of different formats - i.e. percentages and counts or text and numbers.
- You can create new variables using the $ syntax instead of having to use the cbind function (which can sometimes mess up the data formatting).
- You can reference variables using the $ and [] syntax.
How can I access other metadata about an output or variable, such as variable set name used in table, stat testing results shown in a table, a variable's underlying code frame (values and labels), and raw table of data from a nicely formatted output?
Variables and most outputs in Displayr include metadata along with them. These are listed as attributes of the item. To see all the metadata available, you can use the code str(youritem)
inside of a Calculation > Custom Code. You can use the code attr(youritem,
"the attribute")
or similar to pull off metadata that you want. The article How to Extract Information From an Item Using R goes into more detail on how to do this.