Will create a build to run an R script in Cloud Build with an optional schedule from Cloud Scheduler

cr_deploy_r(
  r,
  schedule = NULL,
  source = NULL,
  run_name = NULL,
  r_image = "rocker/verse",
  pre_steps = NULL,
  post_steps = NULL,
  timeout = 600L,
  ...,
  schedule_type = c("pubsub", "http"),
  schedule_pubsub = NULL,
  email = cr_email_get(),
  region = cr_region_get(),
  projectId = cr_project_get(),
  serviceAccount = NULL,
  launch_browser = interactive()
)

Arguments

r

R code to run or a file containing R code ending with .R, or the gs:// location on Cloud Storage of the R file you want to run

schedule

A cron schedule e.g. "15 5 * * *"

source

A Source object specifying the location of the source files to build, usually created by cr_build_source

run_name

What name the R code will identify itself as. If NULL one is autogenerated.

r_image

The R docker environment executing the R code

pre_steps

Other cr_buildstep to run before the R code executes

post_steps

Other cr_buildstep to run after the R code executes

timeout

Amount of time that this build should be allowed to run, to second

...

Arguments passed on to cr_buildstep_r

name

The docker image that will run the R code, usually from rocker-project.org

r_source

Whether the R code will be from a runtime file within the source or at build time copying over from a local R file in your session

escape_dollar

Default TRUE. This will turn $ into $$ within the script to avoid them being recognised as Cloud Build variables. Turn this off if you want that behaviour (e.g. my_project="$PROJECT_ID")

rscript_args

Optional arguments for the R script run by Rscript.

r_cmd

should `Rscript` be run or `R`?

prefix

prefixed to name - set to "" to suppress. Will be suppressed if name starts with gcr.io or *-docker.pkg.dev

schedule_type

If you have specified a schedule, this will select what strategy it will use to deploy it. See details

schedule_pubsub

If you have a custom pubsub message to send via an existing topic, use cr_schedule_pubsub to supply it here

email

The email that will authenticate the job set via cr_email_set

region

The region usually set with cr_region_set

projectId

ID of the project

serviceAccount

service account email to be used for the build

launch_browser

Whether to launch the logs URL in a browser once deployed

Value

If scheduling then a Job, if building immediately then a Build

Details

The R script will execute within the root directory of whichever Source you supply, usually created via cr_build_source representing a Cloud Storage bucket or a GitHub repository that is copied across before code execution. Bear in mind if the source changes then the code scheduled may need updating.

The r_image dictates what R libraries the R environment executing the code of r will have, via the underlying Docker container usually supplied by rocker-project.org. If you want custom R libraries beyond the default, create a docker container with those R libraries installed (perhaps via cr_deploy_docker)

Scheduling

If schedule=NULL then the R script will be run immediately on Cloud Build via cr_build.

If schedule carries a cron job string (e.g. "15 5 * * *") then the build will be scheduled via Cloud Scheduler

If schedule_type="pubsub" then you will need googlePubsubR installed and set-up and scheduling will involve:

  1. Creating a PubSub topic called "{run_name}-topic" or subscribing to the one you provided in schedule_pubsub. It is assumed you have created the PubSub topic beforehand if you do supply your own.

  2. Create a Build Trigger called "{run_name}-trigger" that will run when the PubSub topic is called

  3. Create a Cloud Schedule called "{run_name}-trigger" that will send a pubsub message to the topic: either the default that contains just the name of the script, or the message you supplied in schedule_pubsub.

Type "pubsub" is recommended for more complex R scripts as you will have more visibility for debugging schedules via inspecting the PubSub topic, build trigger and build logs, as well as enabling triggering the script from other PubSub topics and allowing to pass dynamic parameters into your schedule scripts via the PubSub message.

If schedule_type="http" then scheduling will involve:

  1. Create a Cloud Build API call with your build embedded within it via cr_schedule_http

  2. Schedule the HTTP call using the authentication email supplied in email or the default cr_email_get

This is the old default and is suitable for smaller R scripts or when you don't want to use the other GCP services. The authentication for the API call from Cloud Scheduler can cause opaque errors as it will give you invalid response codes whether its that or an error in your R script you wish to schedule.

See also

If you want to run R code upon certain events like GitHub pushes, look at cr_buildtrigger

Other Deployment functions: cr_deploy_docker_trigger(), cr_deploy_docker(), cr_deploy_packagetests(), cr_deploy_pkgdown(), cr_deploy_run_website(), cr_deploy_run()

Examples


r_lines <- c(
  "list.files()",
  "library(dplyr)",
  "mtcars %>% select(mpg)",
  "sessionInfo()"
)
source <- cr_build_source(RepoSource("googleCloudStorageR",
  branchName = "master"
))
if (FALSE) {
cr_project_set("my-project")
cr_region_set("europe-west1")
cr_email_set("123456@projectid.iam.gserviceaccount.com")

# check the script runs ok
cr_deploy_r(r_lines, source = source)

# schedule the script
cr_deploy_r(r_lines, schedule = "15 21 * * *", source = source)
}