简体   繁体   English

如何在Opencpu中将datetime作为参数传递?

[英]How do I pass datetime as a parameter in Opencpu?

I have created a function Input in R to plot graph against the date parameters Date1 and Date2. 我在R中创建了一个函数Input,以针对日期参数Date1和Date2绘制图形。 The function runs successfully in R and gives desired output. 该函数在R中成功运行,并提供所需的输出。 Next I try to execute the same on OpenCPU by providing the URL with the curl command and function parameters. 接下来,我尝试通过为URL提供curl命令和函数参数在OpenCPU上执行相同的操作。 At this point, the instruction fails with error. 此时,指令失败并出现错误。

I am trying to pass parameters like this: 我试图传递这样的参数:

curl http://localhost:5656/ocpu/library/InputParam/R/Input -d '{"Date1": "2018-04-25 05:45:00" & "Date2": "2018-05-27 21:45:00 AM"}'

error : unused argument (`'{Date1:` = NA)
In call:
Input(`'{Date1:` = NA)
curl: (3) URL using bad/illegal format or missing URL
'"Date2":' is not recognized as an internal or external command,
operable program or batch file.

This is the function code: 这是功能代码:

Input <- function (Date1,Date2){

library('dplyr')

library('lubridate')

test <- data[data$ShiftStartTime >= Date1 & data$ShiftEndTime <= Date2,]


library('plotly')
p <- plot_ly(test, x = ~test$Equip, y = ~test$DTDuration, name = test$Description) %>%
    add_trace(y = ~test$DTDuration, name = test$Description) %>%
    layout(yaxis = list(title = 'DTDuration'), barmode = 'stack')

p

}

library('opencpu')
ocpu_start_server()`

Something like this: 像这样:

curl https://cloud.opencpu.org/ocpu/tmp/x0468b7ab/graphics/last/png
curl https://cloud.opencpu.org/ocpu/tmp/x0468b7ab/graphics/1/png?width=1000
curl https://cloud.opencpu.org/ocpu/tmp/x0468b7ab/graphics/last/svg
curl https://cloud.opencpu.org/ocpu/tmp/x0468b7ab/graphics/last/pdf?width=8`

You need to coerce the string to a date in your R function using 2018-04-25 05:45:00 like this: 您需要使用2018-04-25 05:45:00在R函数中将字符串强制转换为日期,如下所示:

input <- function (Date1,Date2){
  Date1 <- as.POSIXct(Date1)
  Date2 <- as.POSIXct(Date2)
  test <- data[data$ShiftStartTime >= Date1 & data$ShiftEndTime <= Date2,]
  ...

And then just pass the date as a string in the http request. 然后只需在http请求中将日期作为字符串传递即可。

Also don't use library() in the function but instead declare those packages as dependencies in your package DESCRIPTION file. 也不要在函数中使用library() ,而是在程序包Description文件中将这些程序包声明为依赖项。

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

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