yuvipanda / notebooksharing.space

BSD 3-Clause "New" or "Revised" License
84 stars 10 forks source link

Support uploading rendered .Rmd #21

Open yuvipanda opened 3 years ago

yuvipanda commented 3 years ago

The typical workflow for 'publishing' .Rmd files is (very, very roughly):

  1. Do all your work on an .Rmd file
  2. knit it. This will run the code in the file, and generate non-inline outputs, in a directory nearby
  3. knitr can then render your code + output files into a self-contained, such as HTML. Images are inlined as data attributes, etc.

For a similar experience as ipynb files, we want to get rendered .Rmd files, rather than just the code as we do now.

There are a few options here:

  1. Just accept rendered .html notebooks from R. We can validate that these are in fact R notebooks and not random HTML, but given how flexible R notebooks can be this can be a bit whack-a-mole.
  2. Write a custom format for rmarkdown that outputs .ipynb files.

I spent some time working on (2). Since pandoc now has support for ipynb, and rmarkdown uses pandoc for all conversions, it should theoretically be easy to make this. However, for export to ipynb, pandoc expects code blocks to be:

``` code

```

while the intermediate .md produced by rmarkdown is like:

``` {.r}

```

This is valid pandoc markdown, but pandoc particularly seems to want the code fence to have code to denote ipynb code cell rather than the more specific { .r}.

However, thinking about this some more, (1) is clearly the more 'first class' R experience.

psychemedia commented 3 years ago

In part this makes me think of https://stackoverflow.com/questions/28972614/jupyter-ipython-notebook-convert-an-html-notebook-to-ipynb/47138762#47138762, which converts notebooks previewed as HTML in some conventional way back to ipynb.

I don't know if the .Rmd HTML outputter has a regular structure that could be parsed back into basic .ipynb doc somehow (assumptions may be required!) If nothing else, I guess you could just convert the html to .md and then run it through Jupytext to .ipynb? Not sure how brittle this would be though.

Ideally, though, there'd be some sort of rich Rmd that could capture an equivalent of .ipynb code output cell, though it's not clear how eg RStudio would render it?

RStudio must internally have some sort of notion of a code output cell when you work with Rmd in an interactive way in RStudio, and a way of persisting it, even if only in an embedded display way. Eg I just opened a previously closed project in RStudio and the output from previously interactively run cells in an Rmd doc (in this case, a table, but it could be an image or an embedded html widget etc) is still there:

image