Passing data from server.R to javascript

1,059 views
Skip to first unread message

Maher Harb

unread,
Dec 1, 2014, 2:57:41 PM12/1/14
to shiny-...@googlegroups.com
I built a shiny App with an html ui that has an auto-completion text box powered by jquey-ui. I'm able to pass input from the html page to the server.R and display output back to the html page. However, there's also a javascript code that is loaded within index.html that currently is based on hard coded data. Few lines are shown below:

<script>
  $(function() {
    var availableTags = [
"text 1",
"text 2",
"text 3",
"text 4",
"text 5"
    ]; 
// some javascript code here
  });
  </script>

Is there a way of having values "text1", "text2", etc. passed by the server.R as the input changes? 

Thanks-



Herman Sontrop

unread,
Dec 1, 2014, 6:34:54 PM12/1/14
to shiny-...@googlegroups.com
I don't know if I understand your problem correctly but this may help:

http://ryouready.wordpress.com/2013/11/20/sending-data-from-client-to-server-and-back-using-shiny/

Best Herman

Charles McGuinness

unread,
Dec 1, 2014, 11:58:21 PM12/1/14
to shiny-...@googlegroups.com
Hello Maher!

I use an htmlOutput() in my ui.R and a renderUI in server.R to dynamically and completely replace UI and script components reactively.  

The code in renderUI returns HTML( .... lots of HTML and <script> stuff ... ) and seems to work perfectly.

Perhaps that would be useful for you?

Maher Harb

unread,
Dec 2, 2014, 2:59:51 AM12/2/14
to shiny-...@googlegroups.com
Hi Charles,

I'm trying to integrate the jQuery-ui auto-complete with my shiny App, see example here:

http://jqueryui.com/autocomplete/#multiple

I'm able to pass input from the text box to the server and display the results on the client side in some html element (e.g. by renderTable). But I haven’t figured out how to pass the result to the JavaScript function that handles the popup drop-down box underneath the input text box. The problem as I see it is the javascript that is loaded as part of my index.html file inside a <SCRIPT> tag. 

Note I did not use a ui.R file, but an html ui. Would using a ui.R be advantageous in this case?

Maher Harb

unread,
Dec 2, 2014, 4:55:04 AM12/2/14
to shiny-...@googlegroups.com
UPDATE:

I figured out how to pass data to the javascript code based on the article recommended by Herman. I basically wrap the javascript in my index.html file within a Shiny.addCustomMessageHandler function:

<script>
  Shiny.addCustomMessageHandler("myCallbackHandler",
  function(availableTags) {
.
.
.
  });
 </script>

And on the server side I pass data to the function using:
  observe({
    words <- as.data.frame(out())[,1]
    session$sendCustomMessage(type = "myCallbackHandler", words)
  })

It works beautifully, but I still have to tune few thins because it seems that I need to type double spaces to get it to react as supposed to. You can see the end result here:





raphael...@gmail.com

unread,
Apr 18, 2015, 7:49:50 AM4/18/15
to shiny-...@googlegroups.com
Hi Maher

First, thanks for your useful posts, very helpful. I was wondering whether you had been able to sort out the double space issue? I ran into the exact same issue. I guess I need to refresh the list everytime something is selected, but I haven't figured out how so far.

Cheers

r
Reply all
Reply to author
Forward
0 new messages