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’,

  • compact’ 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 of type vname.oname as ‘outselect’ :

    value ‘view.top:wwdm.LAI’ to select the ‘LAI’ output data of the view named ‘view’.

    value ‘view.top:wwdm.ST’ to select the ‘ST’ output data of the view named ‘view’.

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

    Memo

    # R code :
    
    library('rjson')
    
    # content of simulation results (res) in 'compact' style of presentation,
    # according to plan values ('single', 'linear') and
    # restype values ('dataframe' or 'matrix')
    content_simulation_results_compact <- 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("(Output data name (ie selection_name) and value)", "\n")
            for (compact_outputname in names(res)){
                val = res[[compact_outputname]]
                cat("\n- ", compact_outputname, "\n")
                print(val)
            }
        } else if (restype=='dataframe' && plan=='linear'){
            for (res_a in res){
                cat("\n*** simulation :", "\n")
                cat("(Output data name (ie selection_name) and value)", "\n")
                for (compact_outputname in names(res_a)){
                    val = res_a[[compact_outputname]]
                    cat("\n- ", compact_outputname, "\n")
                    print(val)
                }
            }
        } else if (restype=='matrix' && plan=='single'){
            cat("(View name (ie selection_name) and 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 (ie selection_name) and 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("compact", "single", "dataframe"),
        # or mode=c("compact", "single", "matrix"),
        outselect=c("view.top:wwdm.LAI", "view.top:wwdm.ST"),
        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("compact", "linear", "dataframe"),
        # or mode=c("compact", "linear", "matrix"),
        outselect=c("view.top:wwdm.LAI", "view.top:wwdm.ST"),
        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 'compact' style of presentation
    ##########################################################
    
    content_simulation_results_compact( res=responsedata$res,
                                        plan=responsedata$plan,
                                        restype=responsedata$restype)