knitr::opts_chunk$set(echo = TRUE)
Using library(googleCloudRunner)
to deploy an Rmd file that is knit upon each request to a URL and parametrised based on the URL arguments.
Its not much to look at, but its a baseline for you to make amazing things with.
The point is this is dynamically generated by the URL parameters
# parameters are blank on first load if(is.null(params$cyl)){ the_data <- mtcars } else { the_data <- mtcars[mtcars$cyl == params$cyl, ] } knitr::kable(the_data)
Installs plumber, rmarkdown with pandoc system dependency
FROM gcr.io/gcer-public/googlecloudrunner:master RUN export DEBIAN_FRONTEND=noninteractive; apt-get -y update \ && apt-get install -y git-core \ libcurl4-openssl-dev \ libssl-dev \ make \ pandoc \ pandoc-citeproc \ zlib1g-dev RUN ["install2.r", "rmarkdown"] COPY ["./", "./"] ENTRYPOINT ["Rscript", "server.R"]
#api.R #' Plot out data from the mtcars #' @param cyl If provided, passed into Rmd rendering parameters #' @get / #' @serializer html function(cyl = NULL){ # to avoid caching a timestamp is added outfile <- sprintf("diamonds-%s.html", gsub("[^0-9]","",Sys.time())) # render markdown to the file rmarkdown::render( "diamonds.Rmd", params = list(cyl = cyl), envir = new.env(), output_file = outfile ) # read html of file back in and use as response for plumber readChar(outfile, file.info(outfile)$size) }
#server.r pr <- plumber::plumb("api.R") pr$run(host='0.0.0.0', port=as.numeric(Sys.getenv('PORT')), swagger=TRUE)
library(googleCloudRunner) # this example file only on GitHub version until next CRAN release the_folder <- system.file("cloudrun_rmarkdown", package = "googleCloudRunner") cr_deploy_plumber(the_folder)
Produces these logs:
ℹ 2021-11-26 11:16:53 > Using existing Dockerfile found in folder ℹ 2021-11-26 11:16:53 > Uploading inst/cloudrun_rmarkdown/ folder for Cloud Run ℹ 2021-11-26 11:16:53 > Dockerfile found in inst/cloudrun_rmarkdown/ ── #Deploy docker build for image: gcr.io/xxxx/cloudrun_rmarkdown ───── ── #Upload inst/cloudrun_rmarkdown/ to gs://xxxx/cloudrun_rm ℹ 2021-11-26 11:16:53 > Uploading cloudrun_rmarkdown.tar.gz to xxxxx/cloudrun_rmarkdown.tar.gz ℹ 2021-11-26 11:16:53 > File size detected as 1.5 Kb ℹ 2021-11-26 11:16:53 > Google Cloud Storage Source enabled: /workspace/deploy/ ℹ Cloud Build started - logs: <https://console.cloud.google.com/cloud-build/builds/4e9fd8b3-8cf5-4d4a-b81d-cfbd999a0d61?project=1080525199262> ✓ Build finished with status: SUCCESS ────────────────────────────────────────────────────────────────────────────────────── ℹ Last 10 lines of build log. Use cr_build_logs() to read more INFO[0031] Found cached layer, extracting to filesystem INFO[0031] COPY ["./", "./"] INFO[0031] Taking snapshot of files... INFO[0031] ENTRYPOINT ["Rscript", "server.R"] INFO[0031] No files changed in this command, skipping snapshotting. INFO[0031] Pushing image to gcr.io/xxxx/cloudrun_rmarkdown:4e9fd8b3-8cf5-4d4a-b81d-cfbd999a0d61 INFO[0033] Pushed image to 1 destinations PUSH DONE ℹ 2021-11-26 11:17:43 > gcr.io/xxxxx/cloudrun_rmarkdown:$BUILD_ID ℹ 2021-11-26 11:17:43 > Built Docker image: gcr.io/xxxx/cloudrun_rmarkdown:4e9fd8b3-8cf5-4d4a-b81d-cfbd999a0d61 ── #> Launching CloudRun image: gcr.io/xxxxx/cloudrun_rmarkdown:4e9fd8b3 ℹ Cloud Build started - logs: <https://console.cloud.google.com/cloud-build/builds/cbd87993-921f-45f5-9692-4cf184954ae3?project=1080525199262> ✓ Build finished with status: SUCCESS ────────────────────────────────────────────────────────────────────────────────────── ℹ Last 10 lines of build log. Use cr_build_logs() to read more Step #1 - "auth cloudrun": bindings: Step #1 - "auth cloudrun": - members: Step #1 - "auth cloudrun": - allUsers Step #1 - "auth cloudrun": role: roles/run.invoker Step #1 - "auth cloudrun": etag: BwXRrmmGqcU= Step #1 - "auth cloudrun": version: 1 Finished Step #1 - "auth cloudrun" PUSH DONE ── #> Running at: https://cloudrun-rmarkdown-ewjogewawq-ew.a.run.app ──────────────── ==CloudRunService== name: cloudrun-rmarkdown location: europe-west1 lastModifier: xxxxx2@cloudbuild.gserviceaccount.com containers: gcr.io/xxxx/cloudrun_rmarkdown:4e9fd8b3-8cf5-4d4a-b81d-cfbd999a0d61 creationTimestamp: 2021-11-26T09:43:06.142532Z observedGeneration: 6 url: https://cloudrun-rmarkdown-ewjogewawq-ew.a.run.app
sessionInfo()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.