ramnathv / htmlwidgets

HTML Widgets for R
http://htmlwidgets.org
Other
788 stars 208 forks source link

Add support for Observable Plot? #484

Open ArthurAndrews opened 7 months ago

ArthurAndrews commented 7 months ago

I don't know if this is possible, but it would be really neat to have a tighter integration between R and Observable Plot as an htmlwidget.

It's currently possible to pass data to Observable in Quarto, and this is really powerful, but an htmlwidget would allow users to use Observable Plot from R code alone.

timelyportfolio commented 7 months ago

Agreed @juba started https://github.com/juba/obsplot but has appears to have shifted focus to maintaining the pyobsplot. Perhaps we should revive his efforts.

ArthurAndrews commented 7 months ago

I just took a look at the obsplot package. Seems very ambitious! in terms of R function interfaces.

I can only speak for myself, but I would be happy with a very plain R list() input to Plot that's up-to-date with Observable and on CRAN.

The ojs chunks in Quarto are nice, but they limit its use to notebook chunks - not apps, html table cells, etc. And actually the ojs chunks don't yet render in RStudio, so the current workflow is not that nice.

htmlwidgets is great tool and Observable Plot seems like a worthy addition :)

juba commented 7 months ago

I can confirm that obsplot is not developed anymore and I don't think I will get back to it. That said I'm not sure I would make the same design choices if I had to start over now.

timelyportfolio commented 7 months ago

In a very raw and most basic form, here is a quick little attempt at an htmlwidget wrapper oplot. The design of Observable Plot does not translate well into htmlwidgets, since Plot uses functions rather than a json spec. I am going to try to think through how we might better support in R without needing an endless stream of htmlwidgets::JS or just an inelegant JavaScript eval.

juba commented 7 months ago

In pyobsplot I sort of make a custom serialization of Plot declaration in Python to json. Plot function calls are converted to objects with a property py-obsplot-type : "function". The parsing and serialization code is here:

https://github.com/juba/pyobsplot/blob/main/src/pyobsplot/parsing.py

It is then "deserialized" and run in JavaScript. Code is here:

https://github.com/juba/pyobsplot/blob/main/packages/pyobsplot-js/parsing.js

I also added a special handling of data attributes, to avoid duplication as they are often repeated in a Plot declaration. Data frames are serialized via Arrow IPC format.