Sending data from Rook server to Web App

54 views
Skip to first unread message

Jerry Palmer

unread,
May 3, 2014, 4:15:26 PM5/3/14
to rr...@googlegroups.com
Hi everyone,

I've been trying to solve this problem for weeks.  I want to send data from my Web app to the Rook server and then have R do some computations with the data I send to it, then I want Rook to send the results back to the Web App where the app will display it.  I've only gotten as far as my Rook receiving the given parameters and displaying it on the R console.

Question: How do I send data from Rook server back to Web App?

Here's my ajax function from the web app:

$.ajax({
               type: 'POST',
               url: 'http://127.0.0.1:24743/custom/compute',
               dataType:"json",
               data: { 'name : name, 'age' : age },
               success: function(data){
                   alert("SUCCESS" + data);
               },
           error: function(){
   alert(" AJAX ERROR ");
         }
       }); // END AJAX Call

Rook code:

library(Rook)
s <- Rhttpd$new()
s$start(quiet=TRUE)
s$add(
  name ="compute",
  app  = function(env) {
    req = Request$new(env)
    res = Response$new()
    name = unlist(strsplit(req$params()$name, ",")) 
    age = unlist(strsplit(req$params()$age, ","))

    results = list()
    results$fac = facets
    results$age = age
    
    #print() only displays to console, but I want to send this to my webapp
    print(toJSON(results)) #prints results to console
    res$finish()
  }
)
s$browse("summarize")


Sorry if the formatting looks bad, any input would be greatly appreciated!  What I'm looking for is probably just one line - something that can send the data to my ajax function.

Jeroen Ooms

unread,
May 3, 2014, 8:06:00 PM5/3/14
to rr...@googlegroups.com
Have a look at opencpu: a system that builds on rApache which takes care of this and other problems. Here and here are examples of how to upload a file. 




--
You received this message because you are subscribed to the Google Groups "rRook" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rrook+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

azfa...@gmail.com

unread,
May 4, 2014, 1:27:14 AM5/4/14
to rr...@googlegroups.com
Hi Jereon,

Thank you for the quick response!  Hmm does Rook not have any method built-in to send data back to AJAX?  I guess if not, then I can look into opencpu! Would I need Rook, rApache, and opencpu for my task? 

Jeroen Ooms

unread,
May 4, 2014, 3:36:50 PM5/4/14
to rr...@googlegroups.com
On Sat, May 3, 2014 at 10:27 PM, <azfa...@gmail.com> wrote:
Thank you for the quick response!  Hmm does Rook not have any method built-in to send data back to AJAX?  

It does, in rook you have to use the Response class. See the manual. But I think you'll find a more high level framework such as opencpu or shiny easier to use. 
 
I guess if not, then I can look into opencpu! Would I need Rook, rApache, and opencpu for my task? 

Just opencpu.

Jerry Palmer

unread,
May 5, 2014, 8:32:49 PM5/5/14
to rr...@googlegroups.com
opencpu sounds like a great choice, though the learning curve is a little steep (as expected).  Thanks for the link to the manual, I've done some research on sending data from R to AJAX and for some reason, nothing in the Response class works.  I believe write() is the appropriate method but it only writes to an html page and not quite send back to AJAX.  I feel like I am just one line of code away for finishing up this small project...but no luck...

Jeffrey Horner

unread,
May 6, 2014, 10:25:23 AM5/6/14
to rr...@googlegroups.com
HI Jerry, jumping in late here. Rather than using just write, use the json package to return meaningful objects to your AJAX queries. Scope out: 


Since you already have Rook installed, you can find that file and other example apps this way:

library(Rook)
system.file('exampleApps',package='Rook')

Best,

Jeff




--

Jerry Palmer

unread,
May 10, 2014, 10:56:21 PM5/10/14
to rr...@googlegroups.com
Thank you very much Jeroen and Jeffrey, this post helped out a lot.  Jeroen - you were right about opencpu being easy to use. Jeffrey, I'm glad I was finally able to find out what to do.
Reply all
Reply to author
Forward
0 new messages