How to call an R script from within a JS code

LabKey Support Forum (Inactive)
How to call an R script from within a JS code Leo Dashevskiy  2012-05-01 19:00
Status: Closed
 
Hello!

Would someone be able to tell me, please, how to just call an R code from withing JavaScript?
The code does not return an image (may be some kind of a return status indicator), it does need get the input parameters from the JS side.

Thanks.
 
 
Ben Bimber responded:  2012-05-01 19:10
Hi Leo,

Here's a similar example you can adapt. You'll use a Report webpart. The partConfig section is where you pass config to the R report.


            new LABKEY.WebPart({
                partName: 'Report',
                renderTo: 'myDiv',
        //these are passed on the URL to your script
                partConfig: {
                    title: 'Population Change',
                    schemaName: 'study',
                    showSection: 'barchart&tsvfile',
                    reportId : 'module:ehr/schemas/study/colonyPopulationChange/Bar Chart.r',
                    'query.queryName': 'colonyPopulationChange',
                    'query.Id/dataset/demographics/species~neq': 'Unknown'
                },
                failure: function(error){
            alert('there was an error');
        },
        success: function(response){
                console.log(arguments);
        },
                scope: this
            }.render();
 
Leo Dashevskiy responded:  2012-05-02 10:09
So yes, that is what I have been using, when the R script returns something back and it needs to be displayed (an image in my case).

Here I don't have anything returned (except may be for a status code) and also I DO NOT have any 'myDiv' element to render to, but seems like renderTo is a required part of a WebPart, cannot have a declaration/definition without it, is that right? Should I then have a dummy/empty div in this case?
How do I suppress any kind of output from R script to not get back to the JS, so that nothing gets really rendered to that div?

What is the param "showSection"? What kind of values can it take up?

Thanks.
 
Ben Bimber responded:  2012-05-02 11:04
Hi Leo,

Sorry, misunderstood part of your first post. The showSection param allows you to specify which sections of the R report are displayed. For example, your report might have a PNG, some text, and the console output. In your R script, you might have created an output like:

write.table(theTable, file = "${tsvout:tsvfile}", sep = "\t", qmethod = "double", col.names=NA)

or

png(filename="${imgout:barchart}",
    width=1100,
    height=(500),
    #type="cairo"
    );


You'll see I gave names to these outputs. In the webpart config, I provided a comma separated list:

showSection: 'barchart&tsvfile',

which causes only those 2 sections to be shown.

To get at your problem: the best idea we've come up with would be to render the R report to a hidden DIV, and to use showSection to render only the minimal section(s). If you need to pass information from R to JS, you could get your script to output some text when complete, like 'Report Succeeded' or 'Report Failed'. In the success callback from this report webpart, you could use JS to inspect the contents of the DIV where the R report was rendered, and look for these strings. This leaves something to be desired, but may unblock your scenario. Let me know if you'd like more detail on any of this.

The idea of a more direct way to call scripts from JS (and perhaps to parse their results) has come up before. It would be nice to be able do this better.
 
Leo Dashevskiy responded:  2012-05-04 15:52
Yes, the approach seems to work. Thanks.
 
Leo Dashevskiy responded:  2012-05-23 15:34
Is it possible to pass to an R script in a JS config option of a webPart a JS array as an argument? (Currently resorting to converting an array to a concatenated string, which then has to be tokenized in the R script...)

Thanks.
 
jeckels responded:  2012-05-23 16:11
Hi Leo,

We don't have any direct mechanism for the browser to pass R code to be invoked in that way. This is important for security reasons, since R does not run inside of sandbox that limits its access to the underlying system. We can't trust the code that gets submitted by general site users. It sounds like your current approach is a good one for these kind of parameters.

Thanks,
Josh
 
Leo Dashevskiy responded:  2012-05-24 11:30
Ok, thanks.