Search code examples
rr-markdownsnakemake

How can I redirect R Markdown errors to a log file when running .Rmd scripts from a pipeline?


I generate reports at the end of my Snakemake pipeline using a series of R markdown scripts. If one of these markdown files has an error and fails, the error is not saved to a log file making it impossible to investigate afterwards.

From my research I have not found a way to do this, but: is there a way to redirect errors in R markdown to a log file when running the markdown file in a pipeline? (My pipeline uses SnakeMake for the pipeline, but I assume this would apply to any pipeline).


Solution

  • With the current Snakemake version (7.8.0), it is possible to redirect to log files also from R code inside an R markdown.

    In your rule, you can do

    rule create_report:
        log:
            log_file = <path/to/logfile>
        script:
            "my_report.Rmd"
    

    and then in the my_report.Rmd file, add a sink instruction in an R setup block, something like:

    ```{R setup, include = FALSE}
    log <- file(snakemake@log$log_file, open = "wt")
    sink(log)
    sink(log, type = "message")
    ```
    

    Followed by the code blocks from which the output you want to redirect.