Difference between shinyjs::runjs and htmlwidgets::JS functions

187 views
Skip to first unread message

Sushanta Deb

unread,
Apr 26, 2016, 10:31:56 AM4/26/16
to Shiny - Web Framework for R
Hi,

In the below code, shinyjs::runjs works but htmlwidgets::JS does not.  Should'nt they behave in a similar manner?

shinyjs::onclick("RiskButton",shinyjs::runjs('alert("Hello");'))   --- This works
shinyjs::onclick("RiskButton",htmlwidgets::JS('alert("Hello");'))  --- This does not.

Please suggest.

Also, would like to know if I have html widget (stored in a R variable VariableBeta) and I would like to make some changes to its property using Java, how can I do it

Joe Cheng

unread,
Apr 26, 2016, 11:41:20 AM4/26/16
to Sushanta Deb, Shiny - Web Framework for R
The JS call only marks a string as being a JavaScript expression that can be evaluated. It does not, itself, do anything else with that value. The runjs call, on the other hand, actually sends the code to the browser and executes it.

As for the second question, what exactly are you trying to do?
--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/16045c19-f526-4602-ae88-25394b48f1ae%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sushanta Deb

unread,
Apr 26, 2016, 12:21:03 PM4/26/16
to Shiny - Web Framework for R, sushant...@gmail.com
I have an S4 class R object called VariableBeta which is basically a chart.  So plot(VariableBeta) throws out a beautiful chart in the Viewer of RStudio.  

VariableBeta is basically the R equivalent of a JS instance of a chart object (some JS charting library like rCharts, Highcharts etc.)

Now I want to change some of the underlying properties of this object like chart color or chart title etc. using JS. How can I pass this S4 R object to a JS function like below and make it work

$( document ).ready(function() {
              $("#RiskButton").click(function (VariableBeta) {
                        VariableBeta .panels[0].stockGraphs[0].type = "column";
                        VariableBeta .validateNow();
                });
               })

Is this doable?

Sushanta Deb

unread,
Apr 26, 2016, 12:33:55 PM4/26/16
to Shiny - Web Framework for R, sushant...@gmail.com
The chart is an html widget created from a JS library using htmlwidgets package.  Currently every change to the chart requires me to build the chart once again which is time consuming and not user friendly.  The chart.validateNow() method in the JS library allows us to plot the chart post changes made to it without having to render and build it again from scratch every time some property within it is changed (be it the graphs or title or color etc.).

The creator of the package is not clear how to implement the chart.validateNow() method but my hunch is that if it can be done in JS it should be doable within R.  Appreciate your views.
Reply all
Reply to author
Forward
0 new messages