Running Crunch Automation within R can be convenient and beneficial for a number of reasons:
- You already have R scripts, and you only want to migrate certain parts of those scripts into Crunch Automation
- You need to do other tasks using R anyway, outside the scope of Crunch Automation
- You like to use the R interface to pull up diagnostic information about variables and artifacts
- You prefer the short-hand list of error messages in R Studio vs. the web app (much more efficient to correct)
- You like to use the R Studio interface as a text editor to write Crunch Automation
- R itself a convenient way to generate Crunch Automation code (eg; running 'loops').
Running Crunch Automation within R is easy. You simply use the runCrunchAutomation() function (crunch package documentation here). Below we give a worked example and a few tips.
Remember, after running any Crunch Automation, including via R, the script is stored with the dataset.
Worked example
In the following example, we are looking at the multiple selection transformation that is part of the Mobile Broadband and Home Technology 2019 example study (datafile and script here).
The Crunch Automation script for deriving the "Race" multiple selection variable is as follows:
CREATE MULTIPLE SELECTION
race1, race2, race3, race4
NOT SELECTED "(VOL) Don't know"
AS race_mr
TITLE "Race - Multiple Response"
DESCRIPTION "Which of the following describes your race?";
This can be run within R using the following.
First, you need to log in and access your dataset with R - this article notes that ds in the above refers to the dataset.
Important:
- The Crunch Automation script is enclosed in the above with 'single quotation marks' - this is so R knows that everything in between those is a string. It doesn't confuse R with the "double quotation marks" that Crunch Automation uses. That's why all the Crunch Automation is highlighted yellow in the above picture.
- In the above, the example starts a new line (line 2) with just the Crunch Automation code, leaving the 'single quotation mark' on line 1. You can put the line breaks wherever you like - this was simply done in the above so that lines 2 to 7 reflect exactly the Crunch Automation code that gets stored with the dataset, with lines 1 and 8 representing the R function that houses it.
- In the case of the label (VOL) Don't know - because this already has a 'single quotation mark' within it, when using the R in above, a \backslash tells R to treat this particular apostrophe as part of the string and not the end of the string text.
You can also use a text file if you were using another text editor to store the script. The text file will be in your R working directory.
ds <- runCrunchAutomation(ds, 'my_ca.text')
Notes and tips
- You can use as many commands as you want in the string - the above only shows one example, but you could have as many or few commands as you want.
- Using the ds <- means that the dataset representation in R is updated so that you don't need to run ds <- refresh(ds)
- If your Crunch Automation Script is saved as a text file and has errors in it, the command
showScriptErrors()
uses RStudio’s “Markers” pane to give you a quick way to jump to the file and line where the error occurred by clicking on the error messages. This is captured in the video below.