Redirect specific Rmarkdown chunks to console

Hi

Please consider the following (simplistic) use case where a .Rmd file creates an html output and contains 2 R chunks that print some character strings. I would like to be able to print one chunk to the html output and the other one to the console (or the .Rout file if a separate .R script is used to rmarkdown::render the .Rmd file).

Is that possible?

Thanks in advance for your help

---
title: "dummy"
author: "pomchip"
date: "5/3/2022"
output: html_document
---

```{r echo= TRUE}
print('to console')
```

```{r echo= TRUE}
print('to html output')
```

Printing one chunk means printing a log message using message()for example ?
What is the real case usage behind this. Usually when you render() there is no console. Chunk content are executed and their results catched for printing or hiding in the document.

Hi @cderv

Printing one chunk means printing a log message using message() for example ?

Yes, that would be one example.

Usually when you render() there is no console.

Agreed, that's why I mentioned the .Rout file.

What is the real case usage behind this?

Really, the one I mentioned. I would like to be able to define which chunks are printed to the rendered document and which one are printed to the console (for an interactive session) or .Rout file (for a batch execution). I am hopeful that this is possible, since the render function is able to print to both rendered document (apparent for the chunks) and the console/log file (for the execution-related diagnostic messages).

Using message or warnings you can control if they are catched or not by the rendering on a per-chunk basis using the respective option message and warning - you have also error. Some details in

Is message = FALSE what you are looking for ?

Is message = FALSE what you are looking for ?

This seems to work, obviously for messages only. I can work with that... although it would be good to be able to redirect all outputs from an entire chunk

Thanks @cderv

I don't think this would be straightforward but it worth discussing. You can open an issue on knitr repo about that. That would be the place place to discuss with developers that really know the knitr internals.
Maybe this is possible and I am missing how.

Just in case you don't know, you can hide some results from the renderend output , or completely ignore execution of chunk. but that probably not help you for your case.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.