keller-mark / anyhtmlwidget

Bringing core concepts from anywidget to R
https://keller-mark.github.io/anyhtmlwidget/
Other
5 stars 0 forks source link
hidivelab

anyhtmlwidget

Bringing core concepts from anywidget to R.

Installation

devtools::install_github("keller-mark/anyhtmlwidget")

Usage

library(anyhtmlwidget)

esm <- "
function render({ el, model }) {
  let count = () => model.get('count');
  let btn = document.createElement('button');
  btn.innerHTML = `count button ${count()}`;
  btn.addEventListener('click', () => {
    model.set('count', count() + 1);
    model.save_changes();
  });
  model.on('change:count', () => {
        btn.innerHTML = `count is ${count()}`;
  });
  el.appendChild(btn);
}
export default { render };
"

widget <- AnyHtmlWidget$new(.esm = esm, .mode = "dynamic", count = 1)
widget$render()

Setting a value will cause a re-render:

widget$count <- 2

Access the latest values:

widget$count
# [1] 2

Modes