You can insert JS code in a R Markdown document using a js
engine like in this example
Otherwise, you can use includes to insert code in the HTML templates
But usually, JS library to be used in Shiny apps or HTML R Markdown are packaged as htmlwidgets or HTML dependencies with htmltools. See more at
- https://www.htmlwidgets.org/
- Tools for HTML • htmltools
- JavaScript for the R package developer - R-hub blog
- Chapter 4 Handle HTML dependencies with {htmltools} | Outstanding User Interfaces with Shiny and more in this book.
- This JS for R book https://book.javascript-for-r.com/
You can then package this JS library in a R package to share its usage across document and users for examples