rstudio / bookdown

Authoring Books and Technical Documents with R Markdown
https://pkgs.rstudio.com/bookdown/
GNU General Public License v3.0
3.71k stars 1.27k forks source link

Feature request: interactivity within rendered Bookdown cells #851

Open VickyRampin opened 4 years ago

VickyRampin commented 4 years ago

Hi! I love Bookdown and use it for basically all my teaching nowadays. One feature that I would love to see (but don't know enough about the R codebase to contribute :frowning:) is a feature recently implemented in JupyterBook, where you can run individual cells in a rendered (HTML) book. It looks like this (from their demo book):

Screenshot_20200114_160209

When you click the pencil icon, you enter "interactivity" mode where you can run that particular cell and see the output below. It does this by connecting to JupyterHub.

For a Bookdown implementation, I would imagine that this might be achieved perhaps by using the holepunch R package, which allows whole projects to be opened in JupyterHub. This would be a more micro-scale; so instead of rendering the whole book in JupyterHub, I would want to the individual cells within my book to be runnable from the original page (usually I build and host my books with GitLab pages but any CI applies).


By filing an issue to this repo, I promise that

I understand that my issue may be closed if I don't fulfill my promises.

yihui commented 4 years ago

That's a marvelous feature request! I absolutely love it, and will consider it seriously in the next couple of months. Thanks for bringing it to us!

BTW, I tried the demo book you mentioned, and editing and running code doesn't seem to work:

Screen Shot 2020-03-04 at 5 10 35 PM

I think the tricky thing is how to make the cells "stateful". It is easy to execute a single cell that doesn't depend on previous cells (I can probably implement that in a couple of hours), but it is much harder to run cells that are interdependent. I wonder if Jupyter Book or JupyterHub has any magic of storing the state of a whole book on the server.

cderv commented 4 years ago

@yihui I also found this feature awesome, so I looked into it a bit already.

Currently, the feature in jupyter book is powered trough thebelab and it is mainly based upon mybinder.org using jupyter kernel. That means that a kernel is started (take some time at this point) then it is available for all the runnable code chunk. This is how they insure the stateful part. See this small example of three chunks.

Binder can be used with R, and jupyterlab also using a R kernel. I am already trying in my spare time to make a working example with R using this jupyterlab API and this thebelab tool. Unfortunately not working with R kernel currently...

I am just following this trail because I think this could be easy to use if it works as-is with the same tooling. Otherwise, the idea could be improve for R use using similar tooling, or even RStudio product.

This is definitely motivating and challenging ! I'll share my progress.

cderv commented 4 years ago

Here is the proof of concept I wanted to build and show: https://rpubs.com/cderv/581740 This is a small Rmd document based on thebelab example where the chunk can be turned to interactive and then be executed, even modified. The key to make it work was to find the correct kernel name for Jupyterlab API. It is pretty simple and very quick (and dirty) but it shows was it possible to do.

I'll plan to do a try with bookdown demo to see how it could integrate. From there, a lot can be improve to make it work seamlessly with R contents. It will depend on your feedback and what you have in mind for this.

ttimbers commented 3 years ago

@cderv @yihui - I would also like to add my vote for this feature! We are using thebelab in Jupyter book, and it's simply amazing. I have tried to replicate this interactivity with R Bookdown & Shiny based on Desirée De Leon's R Markdown site: https://tinystats.github.io/teacups-giraffes-and-statistics/01_introToR.html, and I can get learn R to work inside bookdown, however you need a separate shiny app per learnr chunk (it seems)...

@cderv - do you have a GitHub repo for your rpubs demo you linked to above? I'd love to poke around!

cderv commented 3 years ago

I did not put the Rmd code inside a github repo because, well it is just an Rmd file, and I included it inside the html notebook. You can download using the code button on the upper right.

This is something I hope to continue working on when I have time as I think it could be awesome to have that!

ttimbers commented 3 years ago

OK, I have taken my book, and pared it to a simpler example to test this. I can replicate what @cderv did when I take one of my book Rmd's and render to html_document:

screenshot:

Screen Shot 2020-08-14 at 10 36 40 PM

The exact same file when output via bookdown::html_book or bookdown::gitbook does not work. The button shows up, but has no functionality:

screenshot:

Screen Shot 2020-08-14 at 10 39 18 PM

I am a bit out of my depth here as to how bookdown makes this different... @yihui ?