Javascript for R
eBook - ePub

Javascript for R

  1. 312 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Javascript for R

Book details
Book preview
Table of contents
Citations

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

Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
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 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

Year
2021
ISBN
9781000408171
Edition
1

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
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
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

  1. Cover
  2. Half Title
  3. Series Page
  4. Title
  5. Copyright
  6. Contents
  7. List of Figures
  8. Preface
  9. I Basics and Roadmap
  10. II Data Visualisation
  11. III Web Development with Shiny
  12. IV JavaScript for Computations
  13. V Robust JavaScript
  14. VI Closing Remarks
  15. Bibliography
  16. Index