我想将一个函数的可变数量的参数传递给C/C++,但是希望保留参数不被评估,同时不希望在R中进行任何计算(除了调用C/C++之外)功能),即我不想调用substitute我的R函数.我认为我可以使用的一个选项就是.External像这样做smth:
R_fn = function(...) .External("cpp_fn", ...)
...
# and in C code:
SEXP cpp_fn (SEXP arglist) {
}
Run Code Online (Sandbox Code Playgroud)
然而,.External正在评估参数...,所以如果我尝试类似的东西
rm(x, y) # just making sure these don't exist
R_fn(x*y)
Run Code Online (Sandbox Code Playgroud)
我收到错误,因为R x*y 在将其发送到函数之前尝试进行评估.
相比之下,R中的以下作品:
f = function(...) g(...)
g = function(x, ...) print(substitute(x))
f(x*y*z)
# x * y * z
Run Code Online (Sandbox Code Playgroud)
我还有其他选择吗?显然,它可以做,因为R本身为许多功能做了它,例如substitute它本身,但我不明白该怎么做.我添加了rcpp标签,因为我最终会使用这个标签Rcpp.
我仍然在努力寻找R范围和环境.我希望能够构造简单的辅助函数,这些函数可以从我的'main'函数中调用,这些函数可以直接引用这些主函数中的所有变量 - 但是我不想在每个main函数中定义辅助函数功能.
helpFunction<-function(){
#can I add a line here to alter the environment of this helper function to that of the calling function?
return(importantVar1+1)
}
mainFunction<-function(importantVar1){
return(helpFunction())
}
mainFunction(importantVar1=3) #so this should output 4
Run Code Online (Sandbox Code Playgroud)