使用 OpenCPU 访问自定义 R 函数

Using OpenCPU to access custom R function

我有一个加载 RandomForest 模型的 R 代码,我想创建一个函数

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

我需要在网络服务器中启用此功能,外部应用程序在其中提供数据并检索输出。

问题是,模型需要预加载,无法为每个请求加载到 R env 中。

该功能需要支持并联

我尝试在 R 中安装 opencpu。

以上代码在 R 中应该是 运行 并且可以在 http://localhost:1234/ocpu/

我现在对 opencpu.js 进行了更改以指向此 URL 并在下面使用了 jquery 中的函数。 ocpu.r_fun_call("randomforest_func",parameters)

然而这似乎不起作用..

ocpu.r_fun_call 似乎没有访问 R 脚本。

我的问题是如何正确配置opencpu才能访问randomforest_func

This 应该有助于将其部署为应用程序,使任何外部应用程序更容易使用服务。

This 应该有助于包含模型。

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

没有。您需要创建一个包,在其中放置您的自定义函数。如果程序包名为 foo,则应用程序将在

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

(其中 xxxx 是端口的随机值,在您 运行 opencpu$browse() 时给出)。 此外,您必须使用 ocpu.call,而不是 ocpu.r_fun_call