Reputation: 1525
I am trying to render an Rmarkdown file through an R script. (Code for both files below). What I would like to do is to pass information back to the render function depending on where the error is. This may be that the file can't read the input dataset. I would like to do this as I would like to run the script as a cron job and would like it to send me an email telling me why I might need to re run the code or what the error is.
I have read some of the other stackoverflow similar questions and couldn't see how it did what I wanted with some testing.
The r script: (I have attempted using something like the following)
rm(list = ls())
setwd("C:/Users/joel.kandiah/Downloads")
a <- print(try(rmarkdown::render("test.Rmd", quiet = T), TRUE))
#> [1] "C:/Users/joel.kandiah/Downloads/test.nb.html"
cat(eval(a))
#> C:/Users/joel.kandiah/Downloads/test.nb.html
The Rmarkdown document:
if(!exists("data_raw")) simpleError("Dataset has not been loaded")
#> <simpleError: Dataset has not been loaded>
What I would like is to see the simple error as an object in the R script. Something akin to an exit code might also be acceptable.
Upvotes: 0
Views: 560
Reputation: 1955
A possible approach, is the wrapping tryCatch
around render
in your R script
.
R Script
# Render the markdown document; ####
tryCatch(
expr = rmarkdown::render(
"markdown.Rmd",
clean = TRUE
),
error = function(cond) {
message(cond)
},
warning = function(cond) {
message(cond)
}
)
R Markdown
# Force an error;
stop("You do not have permission to render. Admin password is needed.")
This will return the same error
-message to your script
.
Upvotes: 1