Search code examples
opencpu

Using an output file stored on the OpenCPU server as input to a subsequent function call


Question: how can I use an output file stored on the OpenCPU server as input to another function?

Background: I am attempting to use knitr and markdown within openCPU to generate html that I can use to update a webpage with statistical information on the page load.

The basic workflow is as follows:

  1. Generate an .Rmd file, store locally.
  2. Access a webpage that uses AJAX to upload the .Rmd file to an OpenCPU instance on the server.
  3. Use the knit function via openCPU to turn the function into a *.md file stored on the server.
  4. Use the markdownToHTML function on the file stored on the server (by passing in the appropriate hash generated via the call to knit) and receive an AJAX reply that contains the generated HTML.
  5. Update web page with new HTML.

As it stands, I have this process working up to step 4. I can call knit passing in an .Rmd file via a form request POST, and I receive the following reply from OpenCPU:

{
"object" : "xa9eaea44e1",
"graphs" : [
    "xf31dcfe7f3"
],
"files" : {
    "figure" : "xfc55396fd8",
    "test.md" : "x7821c69f79"
}
}

where "test.md" is the output file generated via the knit function. Now, I attempt to use the hash (in this case "x7821c69f79" by POSTing to /R/pub/markdown/markdownToHTML/ascii with the following parameters:

file     /R/tmp/x7821c69f79/bin

This returns an HTTP 400 error with the following message:

cannot open URL 'http://localhost/R/store/R/tmp/x7821c69f79/bin/rds'

However, when I make a GET request to /R/tmp/x7821c69f79/bin, the contents of test.md are returned. So I know the file is being stored correctly on the call to knit.

So, what's going on here? In other words, how can I use an output file stored on the OpenCPU server as input to another function?


Solution

  • Hmz the /store error looks like a bug, I'll look into that.

    Maybe in step 3 you can have the function return the contents of test.md, e.g. end with return(readLines(test.md))? Or better yet, don't output to test.md but to a tmpfile() and return the contents of that. This way the output is stored as an R object in the store, rather than a raw file, and you can just pass an argument e.g. file=x7821c69f79 in step 4.

    Did you have a look at the markdown example app? See source here and here.