Ashley Baldry
{shiny} + JS = ❤️tags$script(HTML("$(document).ready(function() {$('[data-toggle="tooltip"]').tooltip()})"))tags$script(src="custom.js")Shiny.inputBindings.register(inputBinding, "your.inputBinding")Latest version: https://ashbaldry.shinyapps.io/designer/
Structure of the JavaScript code in the {designer} package for version 0.1.0
srcjs.Rbuildignoresrcjs directory up in your IDEnpm init in the terminal and initialise your projectnpm init in VS Codenpm, within its installation
npm install github:rstudio/shinynpm install @types/jquery@3.5.14Important
Create a .gitignore in your JavaScript directory and include node_modules
JS library structure after installing required dependencies
import { build } from 'esbuild'
build({
entryPoints: ['index.js'],
bundle: true,
sourcemap: true,
outfile: '../inst/app/www/designer.min.js',
platform: 'node',
minify: true
}).catch(
() => process.exit(1)
)npm install esbuild --save-dev
--save-dev only includes package for development purposesJS library structure after installing bundling library
npm install eslint --save-dev
.eslintrc.ymlJS library structure after installing linting
"type": "module" to package.json to enable import to work// component/component.js
export class Component {
html = '<div></div>'
constructor () {
// runs when class is created
}
createComponent () {
return this.html
}
}
// component/button.js
import {Component} from './Component'
class Button extends Component {
html = '<button ...>...</button>'
constructor () {
// runs Component constructor
super()
}
}Component class contains several methods accessible to individual components// component/component.js
export class Component {
html = '<div></div>'
constructor () {
// runs when class is created
}
createComponent () {
return this.html
}
}
// component/button.js
import {Component} from './Component'
class Button extends Component {
html = '<button ...>...</button>'
constructor () {
// runs Component constructor
super()
}
}JS library structure after modularising code
__tests__ directory.test.jsnpm install --save-dev jestnpm install --save-dev @babel/plugin-transform-modules-commonjs
// component/button.js
import Component from 'Component'
class Button extends Component {
html = '<button ...>...</button>'
constructor = {
// runs Component constructor
super()
}
}
// component/__tests__/Button.test.js
import { Button } from '../Button'
test('sanity test - button constructs successfully', () => {
const button = new Button()
expect(button.html).toBe('<button ...>...</button>')
})npm run test in VS CodeInclude JavaScript unit tests as part of your GitHub Actions

JavaScript unit test results of the {designer} package
Test Suites: 32 passed, 32 total
Tests: 38 passed, 38 total
Snapshots: 0 total
Time: 5.948 s
Ran all test suites.
Done in 6.82s.
Structure of JavaScript code of the {designer} package
Minified JavaScript file in the dev branch of the {designer} package