简体   繁体   中英

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. The function runs successfully in R and gives desired output. Next I try to execute the same on OpenCPU by providing the URL with the curl command and function parameters. 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:

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.

Also don't use library() in the function but instead declare those packages as dependencies in your package DESCRIPTION file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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