The AI R Code Writer uses AI to write R code directly in the Code Editor and allows you to reference items in your document. This article describes:
Where this feature is located
The AI R Code Writer is accessible in the toolbar of the Code Editor whenever editing R code. That is, it can be used when editing code in:
- Calculations.
- Cells in Calculation Grids.
- R Variables.
- Data sources imported using R.
The feature appears as the "sparkle" button that looks like two stars in the Code Editor toolbar above the code:
How to use the feature
- Find the place in your R Code where you'd like the AI to insert its response and add a new line.
- Start the new line with
#!
to indicate it is a prompt for the AI. - Write a prompt to the AI on what you'd like it to do on that same line (see below for Examples).
- [Optional] Repeat steps 1-3 if you'd like the AI to do multiple things in the code.
- Click on the sparkle icon
in the Code Editor toolbar.
- The AI will insert commented code for you along with an explanation to review before you hit Calculate to run the code.
You may refer to other data in the code, including:
- Variable Set names
- Variable Labels
- The names of calculations
- Table names
- Text box names
- The names of controls
Note that currently the AI isn't able to see the metadata (code frame, table attributes, etc) for a particular variable or item so you may need to also reference the article How to Extract Information From an Item Using R to see how to reference these things.
Examples
One of the features/bugs of modern AI is that when you ask the same question twice, you can get different answers. You should not be concerned if you get different answers when you try the examples below; this is how AI works.
Example 1: Capital of France in code
This prompt asks the type of question that you may use if using a tool like ChatGPT:
#! What's the capital of France?
You will hopefully (hallucinations being kind) get something like what's shown below. Note that it hasn't just given us the answer. Instead, it's written R code that generates the answer. If you press Calculate and run the R code below, it gives the correct answer of Paris.
# Start of prompt response: What's the capital of France?
# Code to find the capital of France
# Create a named vector of European countries and their capitals
capitals <- c("France" = "Paris", "Spain" = "Madrid", "Germany" = "Berlin", "Italy" = "Rome")
# Access the capital of France using the name as an index
capital_of_france <- capitals["France"]
# Print the capital of France
print(capital_of_france)
Example 2: Rewriting code
This example asks the AI to rewrite code:
#! Please rewrite my code below in a more elegant way.
a = 1
a = c(a, 2)
a = c(a, 3)
When rewriting code, the original code is not replaced. Rather, the AI's version is inserted wherever you have created your prompt, meaning that to run the code you will typically need to edit the code (e.g., delete your previous code).
In this example, the AI improves the original code, replacing three inefficient lines with a single more efficient line. However, as is often the case, there is an even better answer, which the AI didn't identify, which is to use a <- 1:3. The AI R Code Writer has been trained on what people commonly do. Common and awesome aren't always the same thing.
# Start of prompt response: Please rewrite my code below in a more elegant way.
# Create a vector 'a' with the initial value
# 1, 2, and 3 using the c() function.
a <- c(1, 2, 3)
# End of prompt response: Please rewrite my code below in a more elegant way.
a = 1
a = c(a, 2)
a = c(a, 3)
Example 3: Querying the data
This example asks Displayr to answer a question about the data in a study.
#! Are their more men or women in the data?
The answer we get back (below) illustrates a few things:
- The AI has searched through the data and identified that there is a variable set called Gender and referenced it explicitly in the code.
- The AI has examined the data and identified that gender has two categories, Male and Female.
- Rather than merely answer the question with a yes or no, the AI has instead created code that automatically re-analyzes the input Gender data, and will, when run, advise the user that either:
- There are more men in the data
- There are more women in the data
- The number of men and women are equal in the data
- If the gender data is changed, this code will re-analyze the data and change it's conclusion.
- It's not perfect code. The print function that's called does nothing and can just be removed.
# Start of prompt response: Are there more men or women in the data?
# Use the table function to count the occurrences of each level in the Gender factor
# Determine if there are more men or women by comparing their counts in the Gender variable
gender_counts <- table(Gender)
# Check if there are more men or women using if-else statement and print the result
if (gender_counts['Male'] > gender_counts['Female']) {
print("There are more men in the data.")
} else if (gender_counts['Male'] < gender_counts['Female']) {
print("There are more women in the data.")
} else {
print("The number of men and women are equal in the data.")
}
# End of prompt response: Are there more men or women in the data?
Example 4: Using multiple prompts
This example gives a sequence of related prompts to the user.
#! Crosstab data on when people will next have a a holiday by age, income, education, and race.
#! For each crosstab, calculate the statistical significance.
#! Create a summary of the results, in order of most significant to least significant.
The result, below, shows that the AI has followed the instructions in a coherent way, producing the requested R code:
# Start of prompt response: Crosstab data on when people will next have a a holiday by age, income, education, and race.
# Create crosstabs for each demographic variable with 'Time for next holiday'.
crosstab_age <- table(Age, `Time for next holiday`)
crosstab_income <- table(`Household income`, `Time for next holiday`)
crosstab_education <- table(Education, `Time for next holiday`)
crosstab_race <- table(Race, `Time for next holiday`)
# End of prompt response: Crosstab data on when people will next have a a holiday by age, income, education, and race.
# Start of prompt response: For each crosstab, calculate the statistical significance.
# Calculate chi-squared tests for each crosstab to determine statistical significance.
chisq_age <- chisq.test(crosstab_age)
chisq_income <- chisq.test(crosstab_income)
chisq_education <- chisq.test(crosstab_education)
chisq_race <- chisq.test(crosstab_race)
# End of prompt response: For each crosstab, calculate the statistical significance.
# Start of prompt response: Create a summary of the results, in order of most significant to least significant.
# Collect the p-values from the chi-squared tests.
p_values <- c(Age = chisq_age$p.value,
Income = chisq_income$p.value,
Education = chisq_education$p.value,
Race = chisq_race$p.value)
# Order the p-values in ascending order (most significant to least significant).
summary_results <- sort(p_values)
# Print the summary of results.
summary_results
# End of prompt response: Create a summary of the results, in order of most significant to least significant.
When the code is run, it produces the output below on the data set that was used. Notice there is no data for Education, even though it was in the instructions and in the code.
What's gone wrong is that the crosstab for education, shown below, contains a row with no data, and R, in this situation, returns a missing value for the significance, and the code that was written above hasn't thought about this potential. This illustrates a more general point about using AI for data analysis: data analysis is usually an iterative process where you check things as you go and fix things. When AI is used, errors that a human would spot and rectify often accumulate and become hard to identify and even harder to fix. (These examples illustrate that you can use AI to do an analysis like this; a better approach in Displayr is to just create crosstabs the usual way and sort them based on statistical significance.)
Tips on using the AI for coding
The AI uses the open source R code available on the internet so does not have access to most Displayr-built R functions (such as those used in R-based items created from the menu). Thus, it is better used when requesting help for custom items. Breaking down big requests into individual steps and being as specific as possible with the names of items in your Document are two of the most impactful tips. There are more General programming and R-specific tips outlined in more detail in the Getting Started with OpenAI Prompting in Displayr article.
Similarly, the Code Editor AI does not know how R works differently in Displayr vs the standard R program (for example, the Shiny package is not compatible with Displayr and R cannot access your local drives). If you are struggling with getting the R code you need, there are other resources that you can try:
- This Help Center has general FAQs on R in Displayr and more customized examples of R code for Displayr such as: how to edit the R code in a visualization to customize the hover and how to use our in-house CreateCustomTable function to format tables.
- The Displayr AI Assistant
is trained on the Help Center so may be able to get you the code you need or tell you if something is not possible in Displayr.
- While they can't support writing a lot of custom or complex code, our Support Team can help with basic R requests and at least provide guidance on the best solution. You can contact them directly via Displayr using the Help
> Contact Support.
Next
Common Error Messages in R Code
How to Troubleshoot R Code in Displayr