https://github.com/keller-mark/anyhtmlwidget
Bringing core concepts from anywidget to R
https://github.com/keller-mark/anyhtmlwidget
hidivelab
Last synced: 25 days ago
JSON representation
Bringing core concepts from anywidget to R
- Host: GitHub
- URL: https://github.com/keller-mark/anyhtmlwidget
- Owner: keller-mark
- License: other
- Created: 2024-05-16T21:06:56.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-12-05T19:43:45.000Z (4 months ago)
- Last Synced: 2025-03-17T22:54:52.907Z (28 days ago)
- Topics: hidivelab
- Language: R
- Homepage: https://keller-mark.github.io/anyhtmlwidget/
- Size: 92.8 KB
- Stars: 15
- Watchers: 2
- Forks: 1
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - keller-mark/anyhtmlwidget - Bringing core concepts from anywidget to R (R)
README
# anyhtmlwidget
Bringing core concepts from [anywidget](https://github.com/manzt/anywidget) to R.
- Define widget as a JavaScript EcmaScript Module (ESM) string in R
- Access state from JS using the same AnyWidget `model` API.
- Bidirectional communication (R <-> JS)## Installation
```R
devtools::install_github("keller-mark/anyhtmlwidget")
```## Usage
```R
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:
```R
widget$count <- 2
```Access the latest values:
```R
widget$count
# [1] 2
```## Modes
- `dynamic`: Bidirectional communication via background WebSocket server. Does not block R console.
- `gadget`: Bidirectional communication via Shiny running as a Gadget. Blocks R console.
- `static`: Unidirectional communication (R -> JS). Does not block R console.
- `shiny`: Bidirectional communication. Use when embedding a widget in a Shiny app.