英文:
executing R scripts from golang using Rserve and Roger
问题
如何通过Roger在Golang中使用Rserve执行R脚本中的函数?
如果一个函数不需要参数或只需要一个参数,那么可以正常工作。问题出现在函数需要两个参数的情况下。
Golang代码:
// 使用反引号也可以正常工作
param := "'hello'"
param2 := "'World'"
jsonx, err := rClient.Eval("parse(as.character(" + param + "," + param2 + "))")
if err != nil {
s := fmt.Sprintf("%s %s", "发生错误:", err.Error())
log.Println(s)
return
}
R脚本:
// 简单示例
parse <- function(xx, nx) {
print(xx)
print(nx)
return(nx)
}
第一个参数被赋值为"hello",但第二个参数会报错,提示Rserve端没有设置默认值。如何在Golang中调用需要两个或更多参数的函数呢?
英文:
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
答案1
得分: 0
更改为:
jsonx, err := rClient.Eval("parse(as.character(" + param + "),as.character(" + param2 + "))")
对于像我这样刚开始接触R并与Go集成的人来说,可能会有些困惑。所以我希望这对某人有所帮助。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论