i developing application using opencpu , r, totally new both. using mongolite package connect mongodb. there multiple calls db , connecting every time, takes long. plus data processing, plotting etc. takes quite lot of time load page generated plot. in many cases, have plot fetching data multiple collections.
i noticed able save 3-4 seconds (per connection) if don't connect db each , every time, rather use existing connection.
will great if can guide me best way check if connection established db.
let me brief on have done far!
here connect_to_db.r file
library(mongolite) dbconnection <- null connect_mongodb = function() { db={ if(is.null(dbconnection)){ # connect db if connection null, else return global connection object m <- mongo(collection = mycollection, db = mydb, url = myurl) assign("dbconnection", m, envir = .globalenv) } return(dbconnection) } }
it serves purpose on sourcing file , running r console. but, when use in opencpu server, make call connect_mongodb method r method use plotting. call plotting method javascript file follows.
var req = $("#plot").rplot(myplottingmethod, options).fail(function(){ alert("error loading results"); })
this way, variable "dbconnection" unknown method.
i tried few other ways of using <<- read isn't way it. tried using exists() in place of is_null.
i tried option of calling connect_mongodb method javascript file using ocpu.rpc call idea of passing argument r methods in rplot calls.
var req = ocpu.rpc("connect_mongodb", {})
since connecting mongolite doesnt return json object, attempt failed below error failed json response http://localhost:xxxx/ocpu/tmp/x07c82b16bb/ sadly, tojson of jsonlite , rjson did not in converting db object json
Comments
Post a Comment