--- title: "Literate JavaScript Programming" output: js4shiny::html_document_js vignette: > %\VignetteIndexEntry{Literate JavaScript Programming} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{css echo=FALSE, eval=identical(Sys.getenv("IN_PKGDOWN"), "true")} div > pre:empty { display: none; } h1 > code, h2 > code, h3 > code { background: none; color: currentColor; } pre.r, pre.js, pre.html { position: relative; } pre.r::after, pre.js::after, pre.html::after { position: absolute; bottom: 0; right: 0; color: #888; text-align: right; padding-right: 5px; width: 50px; } pre.r::after { content: "r"; } pre.js::after { content: "js"; } pre.html::after { content: "html"; } .btn-copy-ex { z-index: 100; } ``` ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) js4shiny::register_knitr_js_engine() htmltools::tagList(js4shiny::html_dependency_js4shiny(stylize = "none")) ``` **js4shiny** includes several components that enable the use of R Markdown for literate programming with JavaScript. The first of these is the R Markdown HTML format, `js4shiny::html_document_js()`. ````markdown --- output: js4shiny::html_document_js --- ```` Alternatively, you can override the `js` knitr chunk engine of any HTML-based R Markdown format. This function also registers [json](#json-knitr-engine) and [html](#html-knitr-engine) knitr engines. ```{r eval=FALSE} js4shiny::register_knitr_js_engine() ``` ## Introducing `html_document_js` The `html_document_js` renders R Markdown to HTML and uses a new JavaScript knitr engine that runs each JavaScript chunk in the browser and writes the output of any `console.log()`, or the return value of the chunk, into the HTML document, interactively in the browser. In other words, it renders JavaScript chunks that kind of like R chunks. Here's a standard R chunk. ```{r} x <- 10 message("multiplying x times 20...") x * 20 ``` And here's a JavaScript chunk. ```{js} const x = 10 console.log("multiplying x times 20...") x * 20 ``` When the document is viewed in a browser, each JavaScript chunk is evaluated in its own block scope and any `console.log()` statements and the return value of the block are automatically printed to the chunk's output `
Hello, world!
' ``` ## Interactive Documents There are a lot of interesting things you can do when you blend your JavaScript and HTML together. For instance, you've clicked the button below zero times. Go ahead and click it! ```{=html} ``` ```{js} const btn = document.getElementById('click-me') btn.addEventListener('click', function() { const val = ++btn.value btn.value = val if (val > 9) console.log("Okay, that's enough!") const text = document.getElementById('n_btn_clicks') text.textContent = val === 1 ? '1 time' : `${val} times` const textCTA = document.getElementById('click-again') if (val === 1) { textCTA.textContent = ' again.' } else if (val >= 10) { textCTA.textContent = ` again. Okay, that's enough, thank you!` } }) ``` ## Other `console` methods You can also use other JavaScript `console` methods, like `console.table()`. ```{js people-js} const p1 = {first: 'Colin', last: 'Fay', country: 'France', twitter: '@_ColinFay'} const p2 = {first: 'Garrick', last: 'Aden-Buie', country: 'USA', twitter: '@grrrck'} console.table(p1) console.table(p2) ``` ```{js echo=FALSE} [...document.querySelectorAll('#out-people-js table')] .forEach(el => el.classList.add('table')) ``` ## JSON Chunks {#json-knitr-engine} To visualize or use JSON-formatted data in your document, **js4shiny** also provides a `json` knitr engine. This next chunk is a JSON chunk named `people` that renders as an interactive list. ```` ```{json people}`r ''` [ { "first":"Colin", "last":"Fay", "country":"France", "twitter":"@_ColinFay" }, { "first":"Garrick", "last":"Aden-Buie", "country":"USA", "twitter":"@grrrck" } ] ``` ```` ```{json people, echo=FALSE} [ { "first":"Colin", "last":"Fay", "country":"France", "twitter":"@_ColinFay" }, { "first":"Garrick", "last":"Aden-Buie", "country":"USA", "twitter":"@grrrck" } ] ``` And the data from the JSON becomes a global variable named `data_This text is green, right?
```