R Scripting
R packages are managed from the Admin console under Scripting Environments. Simply select the required environment from the relevant drop down in the Properties panel; click the Packages button see which packages have been downloaded to the given environment.
Configure the R Scripting Node
Script
There are three ways in which you can provide the R script:
- Marketplace: download a script from the Pyramid Marketplace (red arrow below). Once downloaded, the script will appear in the script window.
- Pick a Script: open the content manager folder tree to select a script that was built and saved in Pyramid (green arrow below). Once the script is selected, it will appear in the script window.
- Write or Paste a Script: write or paste a script directly into the script window (blue highlight below).
Script Type
You can select a regular script, or a learn and predict script (orange highlight below). Learn and predict scripts are trained on a given data set, and can then be used to make predictions.
- Click here to learn about learn and predict scripts.
Environment
Choose the virtual R environment that uses the required R version and packages (green highlight above).
Pyramid enables Admins to create multiple virtual environments, where each of these environments can use a different R version and different 3rd party packages.
Packages
View the list of packages that have been downloaded to the currently selected virtual R environment.
- Click here to learn about virtual scripting environments.
Inputs and Outputs
The input window is used to configure the column(s) that will be injected into the script. The output window is used to configure the new column(s) that will be produced by the script. You can also determine whether the new column(s) will be added to the existing table (the table to which the R node is connected), or stored in a new a table.
When you download a script from the Marketplace, Pyramid automatically detects the inputs and outputs. When writing a script of choosing a shared script, you'll need to configure the input and output columns yourself. You also have the option to use to let Pyramid auto detect the output from the script.
- Click here to learn more about scripting inputs and outputs.
- Click here to learn more about the auto detect function.
Preview
Click the preview icon from the script properties to load run the script and preview the results in the Preview panel. Any errors will be displayed in the Error panel.
Say we're a retailer with an online store, and we want to predict the gender of visitors to our website based on some information that we collect about them. In this example, we'll use a logistical regression to make the prediction based on the visitor's income and number of children. The predictions will be added as a column to our table.
After adding the R node; under Input, click the plus sign and select From: Gender, and To: Gender. Note that here we are adding a Gender input, so that we can compare the results with the actual gender.
Click Apply, and add two more columns under Input: one for Income, and another for Children.
Add the following script in the script window:
customerProfile=data.frame(Gender,Income,Children) lm=glm(customerProfile$Gender~customerProfile$Income+customerProfile$Children,family = binomial,data = customerProfile) prediction=predict(lm, type = 'response') resultProbability=as.data.frame(prediction) resultProbability[,2]="female" resultProbability[resultProbability[,1]<0.5,2]="male"LRR =resultProbability[,2]
Click the plus sign under Output to add the output column:
Preview the table to see the new column.