R Markdown is an authoring framework for creating documents with analyses and visualizations in R. Civis Platform does not have a built-in user interface specifically for R Markdown. However, as shown below, one can render R Markdown files in a Script and save them as outputs. It is also worth noting that R Notebooks in Platform, which let you interactively work with your data in R, are a potential alternative for R Markdown.
The Civis R Client has a function publish_rmd for processing R Markdown files, which typically have the file extension “.Rmd”.
Example: Rendering an HTML Report from R Markdown in a Civis File
For example, you can use the following code in an R Script to download an R Markdown file from a Civis File, render it, and then attach the resulting HTML report as a run output:
fileId <- 12345 # Enter your File ID here.
civis::download_civis(fileId, file = "example.Rmd")
reportId <- civis::publish_rmd("example.Rmd")
jobId <- as.numeric(Sys.getenv("CIVIS_JOB_ID"))
runId <- as.numeric(Sys.getenv("CIVIS_RUN_ID"))
civis::scripts_post_r_runs_outputs(jobId, runId, "Report", reportId)
Example: Rendering a PDF from R Markdown in a Civis File
You could also render R Markdown to a PDF file and attach it as a run output in an R Script, as follows:
fileId <- 12345 # Enter your File ID here.
civis::download_civis(fileId, file = "example.Rmd")
rmarkdown::render("example.Rmd", output_format = "pdf_document")
fileId <- civis::write_civis_file("example.pdf", expires_at = NULL)
jobId <- as.numeric(Sys.getenv("CIVIS_JOB_ID"))
runId <- as.numeric(Sys.getenv("CIVIS_RUN_ID"))
civis::scripts_post_r_runs_outputs(jobId, runId, "File", fileId)
Example: Rendering an HTML Report from R Markdown in GitHub
Alternatively, if your R Markdown file is in a GitHub repo, you can use a Container Script with the civisanalytics/datascience-r image to render it. The git repository will be cloned into the “/app” directory in the script. After configuring the Docker image and git repository settings for the script, you could include code like the following in the script’s command to render the R Markdown file and attach an HTML report as a run output:
Rscript -e '
reportId <- civis::publish_rmd("/app/path/to/example.Rmd");
jobId <- as.numeric(Sys.getenv("CIVIS_JOB_ID"));
runId <- as.numeric(Sys.getenv("CIVIS_RUN_ID"));
civis::scripts_post_containers_runs_outputs(jobId, runId, "Report", reportId)
'
Comments
0 comments
Please sign in to leave a comment.