Search code examples
rgorserve

executing R scripts from golang using Rserve and Roger


how can i execute a function from a R script via Rserve from golang using Roger...

If a function requires no arguments or just one argument, it works fine.. The problem comes when the function take two arguments.

Golang

//using backticks works fine too
param := "'hello'"
param2 := "'World'"
jsonx, err := rClient.Eval("parse(as.character(" + param + "," + param2 "))")
if err != nil {
    s := fmt.Sprintf("%s %s", "Error occured : ", err.Error())
    log.Println(s)
    return
}

R script

//simple
parse <- function(xx, nx) {
    print(xx)
    print(nx) 
    return(nx)
}

the first parameter is assigned hello but the second give an error that no default is set from the Rserve side.. How can i call a function that requires two or more parameters from golang


Solution

  • Change:

    jsonx, err := rClient.Eval("parse(as.character(" + param + "," + param2 "))")
    

    To:

    jsonx, err := rClient.Eval("parse(as.character(" + param + "),as.character(" + param2 + "))")
    

    For anyone like myself getting into R and integrating with Go, it can be a bit daunting. So I hope this helps someone.