简体   繁体   English

可以通过对 OPENCPU 的 API 调用直接运行 R 片段吗?

[英]Can Directly run R snippet by an API call to OPENCPU?

I am trying to use opencpu server to run R code via API calls.我正在尝试使用 opencpu 服务器通过 API 调用运行 R 代码。 For R Markdown it works fine (as explained in opencpu example markdown app).对于 R Markdown 它工作正常(如 opencpu 示例 markdown 应用程序中所述)。 I want to directly pass R code in to opencpu and get output.我想直接将R代码传入opencpu并得到output。 Is that possible?那可能吗?

Some thing like http://localhost/ocpu/.../something {"text":"R code here"}http://localhost/ocpu/.../something {"text":"R code here"}

Yes, it is possible and well documented on the OpenCPU website, here是的,这是可能的,并且在 OpenCPU 网站上有详细记录,这里

Argument formats for R function calls (HTTP POST only) R 函数调用的参数格式(仅限 HTTP POST)

When calling a function, we need to pass arguments.调用函数时,我们需要传递参数。 OpenCPU accepts the following types of arguments: Argument:plain R code --- Example: paste("useR", "2013"), mtcars OpenCPU 接受以下类型的参数: Argument:plain R code --- Example: paste("useR", "2013"), mtcars

I have found three ways to do this:我找到了三种方法来做到这一点:

  1. Post a .r file to OpenCPU at ../library/base/R/source.r文件发布到 ../library/base/R/source 的../library/base/R/source
  2. Use do.call使用do.call
  3. Create your own R package and save on the server创建自己的 R package 并保存在服务器上

I'll demo the first two methods:我将演示前两种方法:

Method 1方法一

Post a file with parameter name file to ../library/base/R/source将带有参数名称file的文件发布到../library/base/R/source

Here is an example, in the image below you can see I am making a POST request using localhost:8004/ocpu , where I have OpenCPU monitoring port 8004 in a Docker container.这是一个示例,在下图中,您可以看到我正在使用localhost:8004/ocpu发出 POST 请求,其中我在 Docker 容器中使用 OpenCPU 监控端口 8004。

模拟发帖请求

Above, I inserted the file test.r.上面,我插入了文件test.r。 This is the contents of test.r:这是test.r的内容:

g = "hello world"
x = "yoyoi"

So it is just a two line file.所以它只是一个两行文件。 You can see OpenCPU returns the location parameter:可以看到 OpenCPU 返回 location 参数:
Location: http://localhost:8004/ocpu/tmp/x0ec7fa2f50f510/

If you go to that url, you will see the following:如果您将 go 转换为 url,您将看到以下内容:

console
files/DESCRIPTION
files/test.r
info
R/.val
R/source
source

R/.val is the output R object. R/.val是 output R object。 So if you send get request to the link http://localhost:8004/ocpu/tmp/x0ec7fa2f50f510/R/.val , you will see the output of running file test.r :因此,如果您向链接http://localhost:8004/ocpu/tmp/x0ec7fa2f50f510/R/.val发送 get 请求,您将看到运行文件test.r的 output.outputhttp://localhost:8004/ocpu/tmp/x0ec7fa2f50f510/R/.val

$value
[1] "yoyoi"

$visible
[1] FALSE

So basically, you can send a get request to the location url returned from your post request.所以基本上,您可以向从您的发布请求返回的位置 url 发送获取请求。 Then you will get the last line of test.r然后你会得到最后一行test.r

Method 2方法二

You can use do.call from package base to execute a function:您可以使用来自 package 基础的do.call来执行 function: do.call 示例

Above will return onetwo以上将返回onetwo

More Detailed Example:更详细的例子:
Here is a more detailed example.这是一个更详细的示例。 Since data persists on the OpenCPU server for a time, so you can use do.call in base R to execute a function on the output of utils::read.csv : Since data persists on the OpenCPU server for a time, so you can use do.call in base R to execute a function on the output of utils::read.csv :

Step 1: read a file with read.csv, in this example I'm reading the cars dataset from here第 1 步:使用 read.csv 读取文件,在此示例中,我从此处读取汽车数据集

读取 csv

Step 2: Apply do.call on the dataframe returned by read.csv :第 2 步:对 read.csv 返回的read.csv应用do.call

do.call 上的 csv

This outputs the first 6 rows of the dataframe:这将输出 dataframe 的前 6 行:

                  X  mpg cyl disp  hp drat    wt  qsec vs am gear carb
1         Mazda RX4 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
2     Mazda RX4 Wag 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
3        Datsun 710 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
4    Hornet 4 Drive 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
5 Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
6           Valiant 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

You can also return as JSON:您也可以返回为 JSON:

http://localhost:8004/ocpu/tmp/x0ecb9593c335da/R/.val/json

JSON 输出

I also just ran a test where I uploaded the cars data twice, which returned two different keys.我还刚刚进行了一次测试,我两次上传了汽车数据,返回了两个不同的密钥。 I then used the key::.val notation with do.call to rbind the two dataframes together.然后我使用key::.val表示法和do.call将两个数据帧rbind在一起。 So if you use that method you can even have an app where you upload 10 csv files and rbind them all together.因此,如果您使用该方法,您甚至可以拥有一个应用程序,您可以在其中上传 10 个rbind文件并将它们全部合并在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM