Using OpenCPU to access custom R function

試著忘記壹切 提交于 2019-12-12 05:08:31

问题


I have an R code which loads the RandomForest model, I am looking to create a function which

load(model)
randomforest_func = function(data) 
{
  data$pred = predict(model,data,type="prob")
  output = data.frame(data$customerid,data$pred[,2])
  return(output)
 }

I need to make this function enabled in webserver, where an external application feeds data and retrieves the output.

The problem is, the model needs to be preloaded and cannot load into R env for each request.

The function needs to support parallel connections.

I tried installing opencpu in R.

The above code should be running in R and available at http://localhost:1234/ocpu/

I now made changes to the opencpu.js to point to this URL and used the function in jquery to below. ocpu.r_fun_call("randomforest_func",parameters)

However this is seems to be not working..

ocpu.r_fun_call does not seem to be accessing the R script.

My question is how to correctly configure the opencpu to be able access the randomforest_func


回答1:


This should help with deploying it as an app, making it easier for any external application to consume the services.

This should help with including the model.




回答2:


The above code should be running in R and available at http://localhost:1234/ocpu/

No. You need to create a package in which you put your custom functions. If the package is called foo, then the app will be available at

http://localhost:xxxx/ocpu/library/foo/www

(where xxxx is a random value for the port, given when you run opencpu$browse()). Also, you have to use ocpu.call, not ocpu.r_fun_call.



来源:https://stackoverflow.com/questions/35501293/using-opencpu-to-access-custom-r-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!