问题
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 created (and I agree that's a good thing, by default), but I'd like to understand if it's possible to update state on the other side of the server (i.e. anywhere but the \tmp object store), even if (for instance) a custom package would have to be used and installed?
Apologies if it's documented somewhere, but when I clicked the link to the admin guide https://raw.github.com/jeroenooms/opencpu/opencpu-0.7/manual/document.pdf, I got a 404
Thanks.
require(opencpu)
#Loading required package: opencpu
#Initiating OpenCPU server...
#OpenCPU started.
#[httpuv] http://localhost:8810/ocpu
#OpenCPU single-user server ready.
require(RCurl)
# no problem getting data
getForm("http://localhost:8810/ocpu/library/datasets/R/mtcars/print")
# ... returns data
# or querying with the identity function
postForm("http://localhost:8810/ocpu/library/base/R/identity/print",x="mtcars[1,]")
# [1] " mpg cyl disp hp drat wt qsec vs am gear carb\r\nMazda RX4 21 6 160 110 3.9 2.62 16.46 0 1 4 4\r\n"
# but if I try a 'write' function...
postForm("http://localhost:8810/ocpu/library/base/R/identity/print",x="write.csv(matrix(1:10,2),file='test2.csv')")
# I get a NULL return (but no failure)
#[1] "NULL\r\n"
#attr(,"Content-Type")
#charset
#"text/plain" "utf-8"
回答1:
All of the info, including the latest PDF manual, is available at OpenCPU.org. Specifically the page about the JavaScript library has a chapter on state.
What you need to do remove the /print
and perform a POST do a function. The result will be a HTTP 201
with a key to the created resources (a file in your case) in the Location
header of the response.
> library(httr)
> req <- POST("http://localhost:1659/ocpu/library/base/R/matrix", body=list(data="1:10", nrow="2"))
> req
Response [http://localhost:1659/ocpu/library/base/R/matrix]
Status: 201
Content-type: text/plain; charset=utf-8
/ocpu/tmp/x0975a128/R/.val
/ocpu/tmp/x0975a128/stdout
/ocpu/tmp/x0975a128/source
/ocpu/tmp/x0975a128/console
/ocpu/tmp/x0975a128/info
> req$headers$location
[1] "/ocpu/tmp/x0975a128/"
> req$headers["x-ocpu-session"]
$`x-ocpu-session`
[1] "x0975a128"
来源:https://stackoverflow.com/questions/21398584/is-there-a-mechanism-to-persist-record-data-from-requests-to-an-opencpu-server