
- 312 pages
- English
- ePUB (mobile friendly)
- Available on iOS & Android
eBook - ePub
Javascript for R
About this book
Little known to many, R works just as well with JavaScript—this book delves into the various ways both languages can work together. The ultimate aim of this work is to put the reader at ease with inviting JavaScript in their data science workflow. In that respect the book is not teaching one JavaScript but rather we show how little JavaScript can greatly support and enhance R code. Therefore, the focus is on integrating external JavaScript libraries and no prior knowledge of JavaScript is required.
Key Features:
? Easy to pick up.
? An entry way to learning JavaScript for R.
? Covers topics not covered anywhere else.
? Easy to follow along.
Frequently asked questions
Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn more here.
Perlego offers two plans: Essential and Complete
- Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
- Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Yes, you can access Javascript for R by John Coene in PDF and/or ePUB format, as well as other popular books in Economics & Statistics for Business & Economics. We have over one million books available in our catalogue for you to explore.
Information
Part III
Web Development with Shiny
11
Bidirectional Communication
DOI: 10.1201/9781003134046-11
Shiny is the web framework of choice for the R programming language. Since JavaScript and Shiny both run in web browsers it follows that they can run alongside one another as one can include JavaScript in such applications. However, often disregarded is the ability for Shiny’s R server to communicate to the front end and vice versa. This collection of chapters aims to show precisely how this works. In this first part, we brush up on the essentials, so we understand how to include JavaScript in shiny applications.
Then again, the goal is not to write a lot of convoluted JavaScript. On the contrary, with little knowledge of the language the aim is to write as little as possible but demonstrate to the reader that it is often enough to vastly improve the user experience of Shiny applications.
11.1 WebSocket an Shiny
Shiny applications have two components: the user interface (UI) and the server function. These two components communicate via a WebSocket: a persistent connection that allows passing messages between the server and clients connecting to it. In the R server, this connection is managed by Shiny using the httpuv (Cheng and Chang, 2021) and WebSocket (Chang et al., 2021 b) packages, while in clients connecting to the server this connection is managed with JavaScript, as depicted in 11.1.

FIGURE 11.1: Websocket visualised
With that in mind, we can put together a Shiny application, which though simple. Exploits bi-directional communication. The application takes a text input, sends the value of the input to the R server, which sends it back to the UI.

Drawing a diagram of the communication between the UI and the server (Figure 11.2) reveals that though this is a simple application a lot is happening.

FIGURE 11.2: Shiny websocket visualised
Communicating between the R server and the user interface requires JavaScript and thus makes a reasonable chunk of this part of the book on web development with Shiny.
11.2 Sessions
Note that shiny isolate each client connecting to the server in what it refers to as “sessions.” This means that when a user visits a shiny application and interacts with the inputs, like clicking a button, or moving a slider this only happens for their session, in their browser, and not in any other users’.
It would indeed be strange if when one of two concurrent users enters text in a box and that is reflected on the other user’s screen.
This is good to know because WebSocket are in fact often use for precisely that effect. For instance, in a chat application where someone posting a message to a group chat is sent to the server which then, via the WebSocket, broadcasts the message to all other users in the group chat.
Shiny does not allow this, users are isolated from one another.
11.3 Alerts, an example
Let us exploit an external library to demonstrate how this works: jBox1 allows displaying “notices,” similar to vanilla JavaScript alerts but much better looking and with additional functionalities.
The grand scheme is to build an application that displays a notification at the click of an actionButton and “tells” the server when it is closed. Though the introduction of this book includes best practices on how to include dependencies and JavaScript files, much of that will be disregarded in this section (and only in this section). That is only so it does not get in the way of explaining bidirectional communication through the WebSocket in Shiny.
More...
Table of contents
- Cover
- Half Title
- Series Page
- Title
- Copyright
- Contents
- List of Figures
- Preface
- I Basics and Roadmap
- II Data Visualisation
- III Web Development with Shiny
- IV JavaScript for Computations
- V Robust JavaScript
- VI Closing Remarks
- Bibliography
- Index