mbh86
mbh86

Reputation: 6378

Accessing a value from Server.R into .js file

I think's it's quite an easy one but I can't find a way to solve it myself. I'd like to access the value of a variable in my Server.R file (I'm using Shiny) in my javascript file. My var "i" in myFile.js should take the value of my R variable "number". How should I proceed ?

Example :

Server.R

...
number <- 5
...

myFile.js

...
var i = ??? // var i = number *is not working* 
...

Thanks for your help,

Matt

Upvotes: 4

Views: 1196

Answers (1)

mbh86
mbh86

Reputation: 6378

Thanks to jdharrison, it works when I add the following code in myFile.js :

Server.R

number <- 5
observe({
   session$sendCustomMessage(type='myCallbackHandler', number) 
})

myFile.js

var i ;
Shiny.addCustomMessageHandler("myCallbackHandler",     
    function(number) {
     i = number;
    }
);

var i now takes the value 5 in my javascript file.

Upvotes: 4

Related Questions