Example of POST vpz/output in R language

For the simulator ‘wwdm.vpz’ whose Id is 266, enter the R code/instructions in a R interpreter.

Example illustrating :

  • modifying begin,

  • modifying duration,

  • modifying some parameters by ‘cname.pname’,

  • tree’ as style of presentation,

  • json’ as format,

  • value ‘single’ as plan, or value ‘linear’ in case of multiple simulation

  • value ‘dataframe’ as restype (or ‘matrix’)

  • value ‘all’ as ‘outselect’,

  • with ‘application/json’ as ‘Content-Type’

    Memo

    # R code :
    
    library('rjson')
    
    # content of simulation results (res) in 'tree' style of presentation,
    # according to plan values ('single', 'linear') and
    # restype values ('dataframe' or 'matrix')
    content_simulation_results_tree <- function(res, plan, restype){
    
        res = fromJSON(res)
        cat( "plan, restype :", plan, restype, "\n")
        #cat( "res :", "\n")
        #print(res)
        cat("\nDetailing the results :", "\n")
        if (restype=='dataframe' && plan=='single'){
            cat("(view name, output data name, value)", "\n")
            for (viewname in names(res)){ 
                outputs = res[[viewname]]
                for (outputname in names(outputs)){ 
                    val = outputs[[outputname]]
                    cat("\n- ", viewname, outputname, "\n")
                    print(val)
                }
            }
        } else if (restype=='dataframe' && plan=='linear'){
            for (res_a in res){
                cat("\n*** simulation :", "\n")
                cat("(view name, output data name, value)", "\n")
                for (viewname in names(res_a)){ 
                    outputs = res_a[[viewname]]
                    for (outputname in names(outputs)){ 
                        val = outputs[[outputname]]
                        cat("\n- ", viewname, outputname, "\n")
                        print(val)
                    }
                }
            }
        } else if (restype=='matrix' && plan=='single'){
            cat("(view name, value)", "\n")
            for (viewname in names(res)){ 
                v = res[[viewname]]
                cat("\n- ", viewname, "\n")
                print(v)
            }
        } else if (restype=='matrix' && plan=='linear'){
            for (res_a in res){
                cat("\n*** simulation :", "\n")
                cat("(view name, value)", "\n")
                for (viewname in names(res_a)){ 
                    v = res_a[[viewname]]
                    cat("\n- ", viewname, "\n")
                    print(v)
                }
            }
        } else {
            cat("error (unexpected)", "\n")
        }
    }
    
    
    
    # R code :
    
    library('RCurl')
    library('rjson')
    
    #######################
    # request and response
    #######################
    
    header = c('Content-Type'='application/json', Accept='application/json')
    
    options(RCurlOptions=list(followlocation=TRUE))
    
    # case of single plan (single simulation)
    postfields = toJSON(list(
        vpz=266,
        duration=6,
        begin=2453982.0,
        cond_wwdm.A=0.0064,
        cond_wwdm.Eb=1.86,
        mode=c("tree", "single", "dataframe"),
        # or mode=c("tree", "single", "matrix"),
        outselect="all",
        format="json"))
    
    # case of linear plan (multiple simulation)
    postfields = toJSON(list(
        vpz=266,
        duration=6,
        begin=2453982.0,
        cond_wwdm.A=c(0.0064,0.0065,0.0066),
        cond_wwdm.Eb=c(1.84,1.85,1.86),
        mode=c("tree", "linear", "dataframe"),
        # or mode=c("tree", "linear", "matrix"),
        outselect="all",
        format="json"))
    
    res = postForm(uri="http://erecord.toulouse.inra.fr:8000/vpz/output/",
        .opts=list(postfields=postfields, httpheader=header))
    
    responsedata = fromJSON(res)
    #responsedata
    
    #######################################################
    # responsedata in case of 'tree' style of presentation
    #######################################################
    
    # id as VpzOutput
    id = responsedata$id
    cat("id :", id, "\n")
    
    content_simulation_results_tree( res=responsedata$res,
                                     plan=responsedata$plan,
                                     restype=responsedata$restype)