
In this chapter, we will first review some JavaScript basics which can be used “client-side” only, i.e. only in your browser. This is what this section of the book aims at: giving you just enough JavaScript knowledge to lighten your shiny app, in order to improve the global user and developer experience. Moreover, the number of JavaScript libraries available on the web is tremendous and the good news is that shiny has everything it needs to bundle external JavaScript libraries inside your application. Knowing just enough JavaScript can improve the quality of your app: especially when you have been using R to render some complex UIs: think conditional panels, simulating a button click from the server, hide and show elements, etc.Īll these things are good examples of where you should be using JavaScript instead of building more or less complex renderUI or insertUI patterns in your server. What this also implies is that getting (even a little bit) better at writing HTML, CSS, and especially JavaScript will make your app better, smaller, and more user-friendly, as JavaScript is a language that has been designed to interact with a web page: change element appearances, hide and show things, click somewhere, show alerts and prompts, etc. Once you have realized that, the possibilities are endless: in fact almost anything doable in a “classic” web app can be done in shiny with a little bit of tweaking.
#Runjs reviews code
In fact, when we write a piece of code like sliderInput("first_input", "Select a number", 1, 10, 5), what we are doing is creating a binding between JavaScript and R, where the JavaScript runtime (in the browser) listens to any event happening on the slider with the id "plop", and whenever it detects that something happens to this element, something (most of the time its value) is sent back to R, and R does computation based on that value.Įven if everything is written in R, we are writing a web application, i.e. It’s important to note here that the communication happens in both directions: from R to JavaScript, and from JavaScript to R.

On the R end of the websocket, i.e. when R receives data from the web page, a value is fetched, and something is computed. Most of the time, when the JavaScript side of the websocket receives one of these events, the page the user sees is modified (for example, a plot is drawn).
#Runjs reviews software
What happens under the hood is a little bit complex and out of scope for this book, but the general idea is that R talks to your browser through a web socket (that you can imagine as a small “phone line” with both software listening at each end, both being able to send messages to the other), 66 and this browser talks to R through the same web socket. Later on, when the app is launched, shiny binds events to UI elements, and these JavaScript events will communicate with R, in the sense that they will send data to R, and receive data from R. That being said, note also that every inclusion of external JavaScript code or library can present a security risk for your application, so don’t include code you don’t know/understand in your application unless you are sure of what you are doing.Īs a rule of thumb, always go for an existing and tested solution when you need JavaScript widgets/functionalities, instead of trying to implement them yourself. 2020), when you get better at writing JavaScript. It can also help you extend shiny with other JavaScript libraries, for example, using htmlwidgets (Vaidyanathan et al.

We chose to include this chapter in this book as it will help you get a better understanding on how shiny works at its core, and show you that getting at ease with JavaScript can help you get better at building web applications using R in the long run.

2020) application without ever writing a single line of JavaScript code.Įven more when you can use a lot of tools that already bundle JavaScript functionalities: a great example of that being shinyjs (Attali 2020), which allows you to interact with your application using JavaScript, without writing a single line of JavaScript. Note you can build a successful, production-grade shiny (Chang et al.
