An example of using Google's Lit library of Web Components.
Create an R package and scaffold a leprechaun application.
usethis::create_package('litter')
leprechaun::scaffold()
JavaScript is better handled with packer, we therefore create a packer scaffold.
packer::scaffold_leprechaun()
Then we add the packer plugin.
leprechaun::use_packer()
This creates boiler plate code to use JavaScript with webpack, which we will change to use Lit.
Install the Lit npm package.
packer::npm_install("lit", scope = "prod")
Replace the content of srcjs/index.js
with:
import {html, css, LitElement} from 'lit';
export class SimpleGreeting extends LitElement {
static get styles() {
return css`p { color: blue }`;
}
static get properties() {
return {
name: {type: String}
}
}
constructor() {
super();
this.name = 'Somebody';
}
render() {
return html`<p>Hello, ${this.name}!</p>`;
}
}
customElements.define('simple-greeting', SimpleGreeting);
Then use the above registered simple-greeting
component
in the UI of the application.
# somewhere in shiny UI
tag("simple-greeting", list(name = "John"))
That is it.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.