Civis users can write their own R code via Scripts. From the top navigation menu, select Code, and then R.
If you do not see this option under scripts and would like to use R scripts, please contact support@civisanalytics.com to have it added.
Import the Client
To make using R within Civis simpler, Civis offers an open-source Civis API R client that allows users to interact with the Platform from R Scripts in Platform, or from their local console. While you are free to write R code that does not use the client, we recommend using it to streamline your workflows. Civis contains tools for typical workflows aimed at optimizing user efficiency.
In order to use the civis package in your scripts, you will need to run the following line at the beginning of each script:
library(civis)
Invoking Parameters
If you want to make use of parameters within your R script, they will be stored as environment variables. You can reference these using the command Sys.getenv('PARAMETER_NAME').
api_key = Sys.getenv('CIVIS_API_KEY')
my_table = Sys.getenv('my_table')
Default Environment Variables
In addition to the environment variables generated by parameters, Scripts run in Civis automatically inherit several environment variables at run time. The relevant variables provided by Civis are:
Environment Variable | Description |
CIVIS_JOB_ID | The ID of the Script |
CIVIS_RUN_ID | The ID of the Run for this Script |
CIVIS_API_KEY | The API Key generated at run time that you can use to make calls to the Civis API during this script run |
Packages
To view the list of installed packages, run the following within your script:
ip = as.data.frame(installed.packages()[,c(1,3:4)])
ip = ip[is.na(ip$Priority),1:2,drop=FALSE]
ip
Docker Images
New R Scripts use the latest available version of the public civisanalytics/datascience-r Docker image. See this page for information about Civis Docker images.
Scheduling Your Script
If you'd like to set your script to run on a schedule, click on the clock icon in the top-right corner of the page. From there you can have the export run on a schedule of your choosing, or else incorporate it into a workflow. For more information about job automation, see the Automate and Workflow help documentation.
Variables in Notification Emails
You can embed variables you’ve defined in your script in the job success notification email. To leverage this feature, upload any values or tables you want to include in your email as a run output JSONValue named "email_outputs".
Open the Notify pane (paper airplane icon) and include {{variable}} in the Email Body text box. If you are embedding a table, you need to include line breaks before and after the {{md_table}}.
Comments
0 comments
Please sign in to leave a comment.