Inline output in raw R Markdown

R Markdown is fantastic for knitting to output formats where there is inline output. The code is written in the R Markdown file and the results appear inline in the output files.

But I'm wondering whether there could be a way to have the same chunk results appear inline in the raw R Markdown file, not just the output formats. We do have a solution for this within the RStudio IDE where, if you run chunks, the output will be displayed inline by default. But this is temporary; the only way of sharing them with the results is to knit the document or rely on collaborators running the code themselves in RStudio. Knitting the document to an output format is fine, of course, but sometimes for the sake of simplicity and ease it would often be better to work with just a single file.

An idea would be to have a special kind of output format, which didn't knit a new file, but would be recognised by RStudio and add outputs as plain text below the chunk when executed interactively. Plots would be saved behind the scenes and code to display them then added below the line. If the document was also rendered into another format, that would need to be detected and the inline outputs removed when knitting (to avoid the outputs appearing twice in the output file).

My inspiration for this idea comes from Org Mode in Emacs, which implements something similar. Org Mode can also knit to other output formats, but still gives you the option of using just a single file, but one with inline outputs.

I'm curious as to whether people think this would be a good idea? And whether there are drawbacks which I'm not considering? I don't think this would be a use case for complicated documents nor do I think it should be the default. It does seem a little at odds with the usual R Markdown way of doing things. But it also seems like a good solution for many simple use cases.

In what way does this differ from an R notebook?
In RStudio this can be generated by File | New File | R Notebook .
It differs from other type of reports by the outputand editor_options lines that are generated in the YAML :

---
title: "R Notebook"
output: html_notebook
editor_options: 
  chunk_output_type: inline
---

The notebook interface is the closest approximate, but the output still only exists in the .nb.html file that is created. My suggestion is one where: when you execute chunks interactively, the output would appear as actual new lines in your .Rmd file, without the need for knitting.

Practically, this would probably look like a new output format (let's call it "raw" for the moment) and a new chunk type (let's call it "output" for the moment). When you have "raw" as your output format and you executed an R chunk interactively, a new "output" chunk would be created in your .Rmd file directly below your R chunk, and it would contain the verbatim output of the code you just executed.

Care would then need to be taken to make sure that the R Markdown engine ignored all the "output" chunks when knitting to all other output formats, so as not to duplicate the results of each chunk.

This topic was automatically closed 21 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.