When I render()
an *.Rmd
file locally in RStudio, the output from the render()
function is displayed in the console:
Basic .Rmd
file:
---
title: "test"
output: html_document
---
```{r setup, include=FALSE}
sink("./output.txt", type = 'output')
knitr::opts_chunk$set(echo = TRUE)
```
## Summary
```{r cars}
summary(cars)
```
## Error
```{r}
dbGetQuery()
```
To build:
library(rmarkdown)
render('./test.rmd')
Output:
This is great when I'm creating reports locally and I can see the progress and errors thrown (if any). I need to monitor this output in stdout (or stderr) but I can't sink this output to that location because knitr
is using capture.input
which uses sink()
(see first comment). I even tried sinking to a file instead but though the file output.txt
is created, there is nothing recorded in that file.
This is an issue for me because I'm using render()
in a Docker container and I can't send the chunk output from the .Rmd
file in the Docker container to stderr or stdout. I need to monitor the chunk output for errors in the container R code inside the .Rmd
file (to diagnose connection db connection errors) and sending those chunks to stdout or stderr is the only way to do that (without logging in to the container which, in my use case (i.e., deployed to AWS) is impossible)
I've reviewed the knitr chunk options and there doesn't seem to be any option I can set to force the chunk output to a file or to stdout or stderr.
Is there some way I can write all of the chunk output to stdout or stderr inside of the render()
function? This several-years old question is similar to mine (if not identical) but the accepted answer does not fit my use case