天道酬勤,学无止境

Using OpenCPU to access custom R function

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

评论

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.

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.

受限制的 HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。

相关推荐
  • Rmpi、OpenCPU 和 Apparmor:拒绝对“/”的请求(Rmpi, OpenCPU, and Apparmor: DENIED request for "/")
    问题 我有一个 R 包,它向我通过 Rmpi​​ 包运行的 OpenMPI 集群发送作业。 在从控制台运行的 R 会话中,一切都按预期工作。 但是,当我尝试像这样从我的 OpenCPU 服务器执行相关功能时(细节已更改以保护无辜者): curl -XPOST http://99.999.999.99/ocpu/library/MyPackage/R/my_cluster_function 我收到此错误: R call failed: process died. (包中的其他非集群调用函数通过 OpenCPU 按预期工作)。 我注意到在/var/log/kern.log ,apparmor DENIED了各种请求,并且通过向/etc/apparmor.d/opencpu.d/custom添加条目以允许 OpenMPI访问它需要的文件。 但是,我无法解决与位置"/" “打开”请求相关的这两个问题(同样,IP 地址已更改): Oct 26 03:49:58 99.999.999.99 kernel: [142952.551234] type=1400 audit(1414295398.849:957): apparmor="DENIED" operation="open" profile="opencpu-main" name="/" pid=22486 comm="orted"
  • Rmpi, OpenCPU, and Apparmor: DENIED request for "/"
    I have an R package that sends out a job to the OpenMPI cluster I have running by means of the Rmpi package. All works as expected within an R session run from the console. However, when I try to execute the relevant function with from my OpenCPU server like this (details changed to protect the innocent): curl -XPOST http://99.999.999.99/ocpu/library/MyPackage/R/my_cluster_function I get this error: R call failed: process died. (Other, non-cluster calling functions within the package work as expected via OpenCPU). I noticed in /var/log/kern.log a variety of requests being DENIED by apparmor
  • Is there a mechanism to persist/record data from requests to an OpenCPU server?
    Firstly, I appreciate there are many good reasons NOT to do this, but for interest, my question is: is there a recommended (or tolerated) method to persist information from an http request to the R session which is running the server? As an example, a trigger in an external system fires an http request on update, which contains basic information (time of update). What is the best way to make that (now-updated) time variable available in R? As an example below, I tried using the ..\identity API to write a simple .csv file. I got a result which didn't appear to be a failure, but the file wasn't
  • 是否有一种机制可以将请求中的数据持久化/记录到 OpenCPU 服务器?(Is there a mechanism to persist/record data from requests to an OpenCPU server?)
    问题 首先,我明白有很多很好的理由不这样做,但出于兴趣,我的问题是:是否有推荐(或容忍)的方法将信息从 http 请求持久化到运行服务器的 R 会话? 例如,外部系统中的触发器在更新时触发 http 请求,其中包含基本信息(更新时间)。 在 R 中使(现在更新的)时间变量可用的最佳方法是什么? 作为下面的示例,我尝试使用..\identity API 编写一个简单的 .csv 文件。 我得到的结果似乎不是失败,但文件没有创建(我同意这是一件好事,默认情况下),但我想了解是否可以更新另一个状态服务器的一侧(即除了 \tmp 对象存储之外的任何地方),即使(例如)必须使用和安装自定义包? 抱歉,如果它记录在某处,但是当我单击管理指南 https://raw.github.com/jeroenooms/opencpu/opencpu-0.7/manual/document.pdf 的链接时,我得到了 404 谢谢。 require(opencpu) #Loading required package: opencpu #Initiating OpenCPU server... #OpenCPU started. #[httpuv] http://localhost:8810/ocpu #OpenCPU single-user server ready. require(RCurl) #
  • 如何在 CentOS6 上启动/停止/刷新 OpenCPU?(How to start/stop/flush OpenCPU on CentOS6?)
    问题 我已经在 CentOS6 上成功构建并安装了 OpenCPU 和 RStudio 服务器。 一切都按预期工作,我确实获得了本地 R 包的成功 GET/POST 响应。 然而,有几件事似乎不起作用: 我想不出一种干净的方法来启动/停止/刷新 OpenCPU。 我目前正在使用apachectl restart但我什至不确定是否会按预期重新加载 R 包。 我在/usr/lib/opencpu/scripts尝试了 Jeroen 的 bash 脚本,但它们似乎需要相当多的调整才能在 CentOS 上工作。 (可能与上述有关)我在/etc/opencpu/Renviron文件中使用load()和readRDS() (也在/etc/opencpu/Rprofile尝试过)将持久性数据集加载到全局环境中——但这并没有似乎工作。 我没有收到任何错误日志,但这些对象在后续请求中不可用。 在 Ubuntu OpenCPU 上似乎配置为在opencpu用户下运行。 在 CentOS6 上,它似乎在安装后在root下运行。 我想修改它,但不知道如何修改。 只是想知道是否有人在 CentOS6 上成功配置了 OpenCPU。 非常感谢,--梅尔。 回答1 一些答案 运行sudo apachectl restart将重新启动。 目前,rpm 中没有包含用于 opencpu 的特殊 init 或
  • 在 opencpu 会话中访问对象(Accessing objects in opencpu sessions)
    问题 如何在后续函数调用中访问 opencpu“会话”中的特定对象? 给定一个请求,说 /ocpu/library/base/R/eval 带参数 expr = "x <- 10; y <- 20" 这使 /ocpu/tmp/x08dfddafe3/R/.val /ocpu/tmp/x08dfddafe3/R/x /ocpu/tmp/x08dfddafe3/R/y /ocpu/tmp/x08dfddafe3/stdout /ocpu/tmp/x08dfddafe3/source /ocpu/tmp/x08dfddafe3/console /ocpu/tmp/x08dfddafe3/info 现在.val的值为 20。 假设我想将x传递给 ocpu/library/stats/R/rnorm 有没有一种快速的方法,比如 n = x08dfddafe3/R/x 在“伪”... 回答1 很好的问题。 从1.4.4版开始,有两种方法可以做到这一点: 您可以使用x08dfddafe3::x来引用会话x08dfddafe3的命名空间中的对象x 。 您可以传递完整的 URL http://your.server.com/ocpu/tmp/x08dfddafe3/R/x/rds。 如果网络允许,这将自动抓取对象并将其反序列化。
  • Accessing objects in opencpu sessions
    How do I access a specific object in an opencpu "session" in a subsequent function call? Given a request, say to /ocpu/library/base/R/eval with parameter expr = "x <- 10; y <- 20" Which gives /ocpu/tmp/x08dfddafe3/R/.val /ocpu/tmp/x08dfddafe3/R/x /ocpu/tmp/x08dfddafe3/R/y /ocpu/tmp/x08dfddafe3/stdout /ocpu/tmp/x08dfddafe3/source /ocpu/tmp/x08dfddafe3/console /ocpu/tmp/x08dfddafe3/info Now .val has the value 20. Suppose I wanted to pass x to ocpu/library/stats/R/rnorm Is there a quick way to that, something like n = x08dfddafe3/R/x in "pseudo"...
  • 中移M5311-NBIOT-OPENCPU开发-HTTP串口收发器
    中移M5311-NBIOT-OPENCPU开发-HTTP串口收发器实现功能:单片机串口发送HTTP-GET请求给M5311,M5311发送HTTP请求,接受返回数据,M5311串口发送回单片机opencpu_uart_demo.c#include "m5311_opencpu.h" #define OPENCPU_MAIN_UART HAL_UART_1 unsigned char uart_cmd = 0; char buffer[300]; char *pbuf; //uart中断回调函数 static void user_uart_callback(hal_uart_callback_event_t status, void *user_data){ pbuf = buffer; int temp1; if(status == HAL_UART_EVENT_READY_TO_READ) { memset(buffer,0,300); temp1 = opencpu_uart_receive(OPENCPU_MAIN_UART, pbuf, 300); uart_cmd = 'f'; } } /* 打印函数 */ void opencpu_printf (const char *str, ...){ static unsigned char s[600]; //This
  • 使用R构建RESTful API(Building RESTful API using R)
    问题 我正在考虑使用编程语言R构建RESTful API,主要是为了以API格式向用户展示我的机器学习模型。 我知道有一些选项,例如导出到PMML,PFA并使用其他语言来处理API部分。 但是,我想坚持使用相同的编程语言,并且想知道R中是否有类似Flask / Django / Springbook的框架? 我看了一下servr / shiny,但是我真的不认为RESTful是他们设计的。 R中是否有更易于使用的更好的解决方案? 回答1 我有两种选择供您选择: 水管工 plumber允许您通过使用特殊注释修饰现有R源代码来创建REST API。 一个小的示例文件: # myfile.R #* @get /mean normalMean <- function(samples=10){ data <- rnorm(samples) mean(data) } #* @post /sum addTwo <- function(a, b){ as.numeric(a) + as.numeric(b) } 在R命令行中: > library(plumber) > r <- plumb("myfile.R") # Where 'myfile.R' is the location of the file shown above > r$run(port=8000) 这样,您将获得如下结果: $
  • rJava fails while invoking RJDBC library from OpenCPU API
    Goal : To use RJDBC with OpenCPU AJAX Post call. Use case : I need to connect with Apache phoenix database and get some data. And on that data I am supposed to do analytics. So to connect with Phoenix i am using rjdbc(phoenix doesn't support any other option to connect). Environment : public.opencpu.org Here is what I am trying: html <textarea> library(RJDBC) .. do some data fetching and then r statistics </textarea> <br /> <button id="submitbutton" >Execute</button> <br /> <br /> <div id="output"> </div> javascript <script type="text/javascript"> //When Document is Ready $(function () { var
  • 数据对象在openCPU的R会话中是否持久化?(Are data objects persistent in openCPU's R session?)
    问题 我想提供一个需要访问大型数据集的Web工具。 优选地,该R数据对象应一次加载到内存中,然后可用于用户定义的查询。 openCPU框架是否以任何方式支持持久性数据对象,还是每个查询都以空白开头? 回答1 OpenCPU API是无状态的,无法在请求之间保持进程活动。 但是,您可以通过将数据集放入软件包中并将其安装在服务器上来轻松解决问题。 您可以使用服务器手册中介绍的preload选项,也可以使用/etc/opencpu/Rprofile脚本在服务器启动时使用内存中的数据集加载程序包。 这样,当请求进入时,数据将准备就绪可以使用。
  • 如何在opencpu中链接两个函数调用(How to chain two function calls in opencpu)
    问题 据说 OpenCPU 支持链接函数调用以计算例如 f(g(x), h(y)) 关于参数格式的文档:https://public.opencpu.org/api.html#api-arguments 包含一个示例,通过计算来说明这一点 summary(read.csv("mydata.csv")) 在这个例子中,f 是通用函数摘要,它将一个对象作为参数。 我需要计算类似的东西: mycalc(read.csv("mydata.csv")) 或者 myplot(read.csv("my data.csv")) 其中 f 将数据帧作为参数。 当将 read.csv 函数返回的 sessionid 或哈希键作为对象参数提供时,这似乎不起作用。 如何解决这种两个非泛型函数的链接? 这是一个完整的例子: 准备包在 R 中测试 f(g(x)) 这里的 plotcars(dfcars()) dfcars<-function(){ data(cars); cars } plotcars<-function(df){ matplot(1:nrow(df),df) } plotcars(dfcars()) # test the two chained functions are working package.skeleton(list = c("dfcars", "plotcars")
  • Are data objects persistent in openCPU's R session?
    I would like to provide a web tool that requires access to a large dataset. Preferentially, this R data object should be loaded into memory once and then be available for user-defined queries. Does the openCPU framework support persistant data objects in any way or does every query begin with a blank slate?
  • 从 OpenCPU API 调用 RJDBC 库时 rJava 失败(rJava fails while invoking RJDBC library from OpenCPU API)
    问题 目标:将 RJDBC 与 OpenCPU AJAX Post 调用一起使用。 用例:我需要连接 Apache phoenix 数据库并获取一些数据。 我应该对这些数据进行分析。 因此,为了与 Phoenix 连接,我使用的是 rjdbc(phoenix 不支持任何其他连接选项)。 环境: public.opencpu.org 这是我正在尝试的: html <textarea> library(RJDBC) .. do some data fetching and then r statistics </textarea> <br /> <button id="submitbutton" >Execute</button> <br /> <br /> <div id="output"> </div> javascript <script type="text/javascript"> //When Document is Ready $(function () { var baseurl = "https://public.opencpu.org"; //because identity is in base ocpu.seturl(baseurl+"/ocpu/library/base/R"); //actual handler $("#submitbutton").on(
  • How to chain two function calls in opencpu
    OpenCPU is said to support chaining of function calls to calculate e.g. f(g(x), h(y)) The docs about argument formats: https://public.opencpu.org/api.html#api-arguments includes an example that illustrates this by calculating summary(read.csv("mydata.csv")) In this example f is the generic function summary that takes as an argument an object. I need to calculate something like: mycalc(read.csv("mydata.csv")) or myplot(read.csv("my data.csv")) where f takes as an argument a dataframe. This doesn't seem to work when giving as object argument the sessionid or hash key returned by the read.csv
  • OpenCPU error HTTP 400 Not a graph object
    I m using Angularjs and OpenCPU to calculate some data through Igraph R Package Graph is a adjacency matrix/Array generate by java script [[0,1,1,0,1,1,0,0,0,1,1,1,1,1],[1,0,1,1,1,1,1,1,1,1,0,1,1,1],[1,1,0,1,0,1,1,1,1,1,1,1,1,1],[0,1,1,0,1,0,0,0,0,1,0,1,0,0],[1,1,0,1,0,1,0,0,0,1,1,0,1,0],[1,1,1,0,1,0,0,1,0,1,1,1,1,1],[0,1,1,0,0,0,0,1,0,0,1,1,1,1],[0,1,1,0,0,1,1,1,1,1,0,1,0,1],[0,1,1,0,0,0,0,1,0,1,1,0,0,0],[1,1,1,1,1,1,0,1,1,0,0,0,0,1],[1,0,1,0,1,1,1,0,1,0,0,0,1,1],[1,1,1,1,0,1,1,1,0,0,0,0,1,1],[1,1,1,0,1,1,1,0,0,0,1,1,0,0],[1,1,1,0,0,1,1,1,0,1,1,1,0,0]] My code is ocpu.call("graph_from
  • How to start/stop/flush OpenCPU on CentOS6?
    I've successfully built and installed OpenCPU and RStudio Server on a CentOS6. Everything is working as expected, and I do get successful GET/POST responses for my local R packages. A couple of things that don't seem to work however: I can't figure a clean way to start/stop/flush OpenCPU. I'm currently using apachectl restart but I'm not even sure that does reload R packages as expected. I tried Jeroen's bash scripts at /usr/lib/opencpu/scripts but they seem to require quite a bit of tweaking to work on CentOS. (maybe related to above) I am using load() and readRDS() in /etc/opencpu/Renviron
  • 通过 openCPU localy 测试 R 应用程序(Test R app via openCPU localy)
    问题 我写了一个 R 包,现在我想提供一个以 web 应用程序为前端的小演示。 为此,我已经编写了一个 index.html 文件,并将其保存到<pkgRoot>/inst/www/文件夹中。 我还安装了 opencpu 包,当我通过localhost:<portNum>/ocpu/test/ “Interface”使用它时,它运行良好,我已经可以在其中看到我的包并使用 Ajax POST 和 GET 请求来查看包正在做它应该做的事情。 但是现在我陷入了某种意义上,我不知道如何在本地测试我自己的应用程序 (index.html)。 localhost:<portNum>/ocpu/<pkgName>/www/index.html不起作用。 据我了解,opencpu 随 Apache 2 一起提供,因此不需要设置额外的网络服务器? 当我检查已安装包的路径时, www文件夹以及 index.html 似乎正是它们应该的样子,据我所知。 我是否必须通过以下方式安装该应用程序 opencpu::install_apps("pkgName") R里面? 因为如果我尝试这样做,似乎无法安装本地应用程序,而只能安装在 github 上托管的应用程序。 ....我只想测试本地应用程序! 真的很高兴有任何建议 回答1 只需使用 opencpu::ocpu_start_app("pkgName")
  • OpenCPU 管理员 opencpu.demo(OpenCPU admin opencpu.demo)
    问题 我在运行时收到错误 我的 OpenCPU 服务器上的管理应用程序错误消息:未找到 The requested URL /Radmin/call/opencpu.demo/install.opencpu/json was not found on this server. 我有opencpu repo的R文件夹和安装的包但我不知道install.opencpu函数文件在哪里 回答1 我不确定您在哪里找到/Radmin/ url,但该功能从未进入 opencpu。 尽量坚持使用手册页中的内容:www.opencpu.org。
  • OpenCPU admin opencpu.demo
    I am recieveing an error when I run the admin app on my OpenCPU Server Error Message : Not Found The requested URL /Radmin/call/opencpu.demo/install.opencpu/json was not found on this server. I have the R folder of the opencpu repo and the package installed yet I dont know where the install.opencpu function file is