Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在VBA函数中使用RInterface.GetArrayToVBA和数组_R_Excel_Rexcel_Vba - Fatal编程技术网

在VBA函数中使用RInterface.GetArrayToVBA和数组

在VBA函数中使用RInterface.GetArrayToVBA和数组,r,excel,rexcel,vba,R,Excel,Rexcel,Vba,让Excel电子表格通过连接到R,您需要编写一个VBA函数来调用一些R函数 在Excel电子表格中,有两个简单的数组,如下所示: 代码可以是这样的: Function foo(x As Range, y As Range) As Variant RInterface.StartRServer If IsNumeric(x) = True Then RInterface.PutArrayFromVBA "x", x End If If IsN

让Excel电子表格通过连接到R,您需要编写一个VBA函数来调用一些R函数

在Excel电子表格中,有两个简单的数组,如下所示:

代码可以是这样的:

Function foo(x As Range, y As Range) As Variant

    RInterface.StartRServer

    If IsNumeric(x) = True Then
        RInterface.PutArrayFromVBA "x", x
    End If

    If IsNumeric(y) = True Then
        RInterface.PutArrayFromVBA "y", y
    End If

    foo = RInterface.GetArrayToVBA("cbind(x, y, y ^ x)")

End Function
它的明显目的是返回一个矩阵,其中包含Excel中的
cbind(x,y,y^x)

我无法得到它,当我稍微修改代码时,我得到了奇怪的结果:有时输出等于
1
,有时等于
#值。。。但是,它不起作用,我无法理解这种情况下需要的语法

Function foo(x As Range, y As Range) As Variant

RInterface.StartRServer
RInterface.PutArrayFromVBA "x", x.Value 'you were missing this .Value'
RInterface.PutArrayFromVBA "y", y.Value
foo = RInterface.GetArrayToVBA("cbind(x, y, y ^ x)")

End Function

我省略了您的错误检查,因为它们只会导致其他错误。如果要执行错误检查,请在出现错误时执行操作转到

不确定
R
如何工作,但不确定
GetArrayToVBA(“cbind(x,y,y^x)”)
应该是
GetArrayToVBA(cbind(x,y,y^x))
考虑到引号中的任何内容都被视为字符串这一事实?
编译错误:未定义子函数或函数
…它突出显示了
y
的语法/参数是什么
Rinterface.GetArray()
?刚刚在网上快速搜索了一下。它类似于
Rinterface.GetArray(“某物”,范围)
?不幸的是,这就是我对
R
的了解显示的地方:pgetaraytovba(Repression As String)为什么这个答案在被接受后不被接受?你有什么需要我解释/补充的吗?