class: center, middle, inverse, title-slide # JavaScript for R ## Enhance your data science products with JavaScript ### John Coene --- class: center centerize .pull-left[ # The Book Available for [pre-order](https://www.routledge.com/Javascript-for-R/Coene/p/book/9780367680633) Out 19 July [javascript-for-r.com](https://javascript-for-r.com/) ] .pull-right[ ![](https://javascript-for-r.com/assets/img/cover.jpg) ] --- class: light # Content .pull-left[ Comprises of 24 Chapters Broken down into 6 parts: 1. Introduction 2. Data Visualisation 3. Web Development 4. Computations 5. Webpack & NPM 6. Closing Forthcoming CRC Press - R Series Available for free: [javascript-for-r.com](https://javascript-for-r.com) ] .pull-right[ ![](https://javascript-for-r.com/assets/img/homepage-free.png) ] --- class: center, centerize ### This aims to ### Give you the <span class='yellow'>urge</span> to learn JavaScript --- class: light .pull-left[ ## Plan Focus on (a tiny part of the) __web development__ chapter. 1. 20 min introduction 2. 10 min code along 3. 30 min workshop~ish excercise Using JavaScript in a Shiny application. ] .pull-right[ ## Resources - RStudio Cloud Project: [rstudio.cloud/project/2615039](https://rstudio.cloud/project/2615039) - Github: [github.com/JohnCoene/r-ladies-tunis](https://github.com/JohnCoene/r-ladies-tunis) - Slides: [john-coene.com/talks/r-ladies-tunis](https://john-coene.com/talks/r-ladies-tunis) ] --- class: center, centerize # Shiny <span class='yellow'>+</span> JavaScript --- class: light ## You're already using it You are probably already using JavaScript, but you may not be aware of it. #### Some examples .pull-left[ - htmlwidgets: - [plotly](https://plotly.com/r/) - [DT](https://rstudio.github.io/DT/) - [leaflet](https://rstudio.github.io/leaflet/) - shiny extensions: - [waiter](http://waiter.john-coene.com/) - [shinyMobile](https://github.com/RinteRface/shinymobile) - [shinyWidgets](https://github.com/dreamRs/shinyWidgets) - Rmarkdown: - [xaringan](https://github.com/yihui/xaringan) - [xaringanExtra](https://github.com/gadenbuie/xaringanExtra) - plenty more ] .pull-right[ ![](https://grapher.network/img/rstudio.png) <small><i>Example of grapher package</i></small> ] --- class: light ## Why JavaScript? There are hundreds of great reasons but mainly: 1. It can do something no other language can: __run in the browser.__ 2. It can easily __extend R.__ <img height="300" src='https://book.javascript-for-r.com/images/dt-crosstalk-intro.png' /> <small><i>DT & plotly with crosstalk</i></small> --- class: center, centerize <strong>Runs in the browser</strong> JavaScript does things only it can do! --- class: center, centerize <strong>It <span class='yellow'>extends</span> R</strong> You're <strong>NOT</strong> re-learning to do things you already know how to do in R. <br/> Use R for most things Use JavaScript elsewhere --- class: light # Websocket .pull-left[ It's new~ish (2010-2011) Shiny uses the _websocket_ that allow _bi-directionnal_ communication between clients (web browsers) and the server. __Two scenarios:__ 1. The server sends a message to the browser. 2. The browser sends a message to the server. We'll focus on __1__: server
client ] .pull-right[
] --- class: center, centerize ### Sending a message ### from the <span class='underline'>server</span> to the <span class='underline'>browser</span> --- class: light # Server-side From the server we send a message. .pull-left[ ```r server <- function(input, output, session) { session$sendCustomMessage( type = "say-hello", # to identify the message message = "hello!" # the message to send ) } ``` We use the `session` object to send message to the browser. The message can be: - A `character` string - A `list` - A `data.frame` - (Almost) any R object ] .pull-right[ The argument `type` is somewhat poorly named. It's essentially an identifier. ] --- class: light # Browser-side We create a handler for the message `type` sent from the server. .pull-left[ ### <span class='blue'>R</span> <span style='float:right;'>
</span> ```r server <- function(input, output, session) { session$sendCustomMessage( type = "say-hello", message = "hello!" ) } ``` ] .pull-right[ ### <span class='yellow'>JavaScript</span> ```js Shiny.addCustomMessageHandler( 'say-hello', // type function(msg){ // do something with msg } ); ``` ] The handler will run the `function` every time a message of the corresponding type is received. --- class: center # Code Along - Alert Display an alert in Shiny. __Useful links__ [rstudio.cloud/project/2615039](https://rstudio.cloud/project/2615039) [github.com/JohnCoene/r-ladies-tunis](https://github.com/JohnCoene/r-ladies-tunis) --- class: light ## List vs. JSON .pull-left[ #### JSON ```json { boolean: true, string: "hello", vector: [1, 2, 3] } ``` Access items ```js json.vector ``` ] .pull-right[ #### R List ```r list( boolean = TRUE, string = "hello", vector = c(1, 2, 3) ) ``` Access items ```r list$vector ``` ] --- class: light ## Dependencies No the luxury of calling a `library()`. "Source" files in the `<head>` of the document. .pull-left[ #### HTML ```html <head> <!-- JavaScript --> <script src="path/to/file.js"></script> <!-- CSS --> <link rel="stylesheet" href="path/to/file.css"> </head> ``` ] .pull-right[ #### R ```r tags$head( tags$script(src = "path/to/file.js"), tags$link( rel = "stylesheet", href = "path/to/file.css" ) ) ``` ] --- class: light # Code Along - Toast Display a toast with [toastify.js](https://github.com/apvarun/toastify-js) (github.com/apvarun/toastify-js) [rstudio.cloud/project/2615039](https://rstudio.cloud/project/2615039) [github.com/JohnCoene/r-ladies-tunis](https://github.com/JohnCoene/r-ladies-tunis) --- class: center, centerize # 👋 John Coene [john-coene.com](https://john-coene.com/) Book: [javascript-for-r.com](https://javascript-for-r.com) (Some) other packages: [waiter](https://waiter.john-coene.com/), [echarts4r](https://echarts4r.john-coene.com/), [cicerone](https://cicerone.john-coene.com/), [ambiorix](https://ambiorix.dev/)
[@jdatap](https://twitter.com/jdatap)
[JohnCoene](https://github.com/JohnCoene)