Search code examples
opencpu

How do you custom format opencpu output


I have a csv file like this:

DateTime        Server1 Server2 Server3
8/24/2014 15:20 6.09    5.7     5.21
8/24/2014 15:55 4.54    4.38    5.33
8/24/2014 16:04 5.03    4.52    4.92
8/24/2014 16:18 4.93    4.61    5.56
8/24/2014 16:27 6.27    4.18    5.62
8/24/2014 16:42 4.59    4.61    6.73
8/24/2014 16:56 5.91    4.37    4.76
8/24/2014 17:10 4.53    4.3     4.59

I used ocpu platfrom to upload it. The R function to read the csv file is this:

readcsvnew <- function(file, ...){
  if(!grepl(".csv$", file)){
    stop("Uploaded file must be a .csv file!")
  }
  read.csv(file, header=TRUE, ...);
}

Once I read this file using ocpu, the data is in the session. Now I need to make a call to update my html list. To do this, I need to make a call to the session object and retrieve the header in json format:

<legend>Plot Parameters</legend>
            <label>Y-Axis</label> 
            <select id="yaxis" multiple="multiple">

            </select> 
            <label>X-Axis</label> 
            <select name="xaxis" id="pollutant">

            </select>

Once I get the header in Json, I can populate it with javascript. Now that select list are updated, based on user picked values of x-axis (this will be DateTime) and Y-axis values (this could be Server1 or Server1 thru Server3), I need to make another call to session and retrieve the values in custom Json format as follows:

{"name":"Server1","data":[[1408893651000,6.09],[1408895711000,4.54]},{"name":"Server2","data":[[1408893651000,5.7],[1408895711000,4.38]},{"name":"Server3","data":[[1408893651000,5.21],[1408895711000,5.33]}

Solution

  • If you retrieve the output of an object, HTTP GET parameters are mapped to jsonlite::toJSON(). For example:

    See this email for a worked example of using the 2 step process to retrieve output from an RPC request using custom JSON.

    Alternatively you can make your function return a list instead of a data frame using as.list.