JavaScript

Lastly, let's look at the JavaScript. As previously mentioned, shinyjs.buttonClick is a function that can access the arguments of js$buttonClick(). With that in mind, let's look at the code:

shinyjs.buttonClick = function(params) {
// boilerplate code
var defaultParams = {
color : "black",
size : "medium"
};
params = shinyjs.getParams(params, defaultParams);

// rest of code
var elem = document.getElementById('selection');

elem.innerHTML = document.getElementById('year').value;

elem.style.color = params.color;

elem.style.fontSize = params.size;
}

Note that it is not necessary to include the <script type="text/javascript">...</script> script tag around the JavaScript in this file. The first part is boilerplate code, and it's designed to handle the use of named and unnamed lists within the R arguments of the function call. Both are accepted by extendShinyjs(), so this code ensures that they play nicely as JavaScript. It also allows you to add default values. You can see the parameters from R being read into the params variable with the shinyjs.getParams function. 

The rest of the code is very simple, merely grabbing the selection element, adding text to it, and changing the color and size using params.color and params.size, both brought through from R and placed as named elements within params. You will recall the input$color and input$size values being passed as arguments within the js$buttonClick() function.

And that's it for shinyjs. A very simple, powerful way of using either canned JavaScript or incorporating your own JavaScript and reading R input very easily.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset