Summary

A custom function for calling gcloud SDK from Rmarkdown via OS command system2()

Create function

Create function for calling gcloud SDK from Rmarkdow:

library(withr)
library(glue)

sh <- function(cmd, args = c(), intern = FALSE) {
  with_path(path.expand("~/google-cloud-sdk/bin/"), {
    if (is.null(args)) {
      cmd <- glue(cmd)
      s <- strsplit(cmd, " ")[[1]]
      cmd <- s[1]
      args <- s[2:length(s)]
    }
    ret <- system2(cmd, args, stdout = TRUE, stderr = TRUE)
    if ("errmsg" %in% attributes(attributes(ret))$names) cat(attr(ret, "errmsg"), "\n")
    if (intern) return(ret) else cat(paste(ret, collapse = "\n"))
  })}

Authenticate with Google Cloud and Vertex AI

Authenticate with Google Cloud and Vertex AI via googleAuthR package and googleCloudVertexAIR:

library(googleAuthR)
library(googleCloudVertexAIR)

options(googleAuthR.scopes.selected = "https://www.googleapis.com/auth/cloud-platform")

gar_auth_service(json_file = Sys.getenv("GAR_SERVICE_JSON"))

Set gcloud project:

sh("gcloud config set project {gcva_project_get()}")
## Updated property [core/project].

List gcloud configuration to verify:

sh("gcloud config list")

Acknowledgements

Special thanks to [Fabian Hirschmann](https://github.com/fhirschmann, the original author of this source code.