【问题标题】:How do I pass datetime as a parameter in Opencpu?如何在 Opencpu 中将日期时间作为参数传递?
【发布时间】:2019-05-14 13:42:43
【问题描述】:

我在 R 中创建了一个函数 Input,以根据日期参数 Date1 和 Date2 绘制图表。该函数在 R 中成功运行并提供所需的输出。接下来,我尝试通过向 URL 提供 curl 命令和函数参数来在 OpenCPU 上执行相同的操作。此时,指令失败并出现错误。

我正在尝试传递这样的参数:

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.

这是功能代码:

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()`

类似这样的:

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`

【问题讨论】:

  • 也许将字符串日期转换为函数开头的日期?
  • 嗨.. 感谢您的回复。我尝试过这种方式,但它不起作用。

标签: r curl opencpu


【解决方案1】:

您需要使用 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,]
  ...

然后在 http 请求中将日期作为字符串传递。

也不要在函数中使用library(),而是在你的包描述文件中将这些包声明为依赖项。

【讨论】:

  • 感谢您的回复杰罗恩。我这样做了,但仍然没有工作。我仍然遇到同样的错误。您需要我的任何意见来进一步诊断吗?
  • 您在示例中发布的数据既不是 json 也不是 http 帖子。请阅读如何使用curl 发布数据或使用测试页面:cloud.opencpu.org/ocpu/test
猜你喜欢
  • 1970-01-01
  • 2010-11-16
  • 2019-04-29
  • 1970-01-01
  • 2015-04-04
  • 2018-12-11
  • 2011-08-29
  • 1970-01-01
  • 2021-09-01
相关资源
最近更新 更多