【问题标题】:Does RInterface.GetArrayToVBA() always return an array?RInterface.GetArrayToVBA() 是否总是返回一个数组?
【发布时间】:2014-08-27 08:39:19
【问题描述】:

参考来自 Wilmott 论坛的this 问题,我刚刚编写了以下函数:

Public Function KmeansPrice(ByVal priceArray As Range, _
                            ByVal clustersNumber As Integer) As Double

    ' Following rows are reproducible only if RExcel has been installed
    ' on your Excel!

    Dim y() As Double

    RInterface.StartRServer
    RInterface.PutArrayFromVBA "x", priceArray
    RInterface.PutArrayFromVBA "n", clustersNumber
    RInterface.RRun "x = as.numeric(x)"
    RInterface.RRun "cluster = kmeans(x, n)$cluster"
    RInterface.RRun "bestBid = rep(NA, n)"
    RInterface.RRun "for(i in 1:n)" & _
                    "{" & _
                    "  assign(paste('group.', i, sep = ''), " & _
                    "         x[cluster == i]);" & _
                    "  bestBid[i] = max(get(paste('group.', i, sep = '')))" & _
                    "}"
    RInterface.RRun "y = min(bestBid) + 0.01"
    y = RInterface.GetArrayToVBA("y")
    KmeansPrice = y(0, 0)

End Function

当然我之前在R做过原型,而且工作正常,那么我猜是这个错误的原因:

Error -2147220501
in Module RExcel.RServer

Error in variable assignment

RInterface.GetArrayToVBA() 的错误用法有关,涉及从 R 到 VBA 的数组的维度和索引。

是否有人能够使上面的代码工作?一个只有五个或十个元素的数组的工作示例priceArrayclustersNumber 等于 2 或 3 就足够了。

【问题讨论】:

    标签: r excel rexcel vba


    【解决方案1】:

    我对聚类函数不熟悉,但这会返回结果而不会中断。

    我更喜欢在 R 编辑器中创建我的函数,然后获取代码,所以我在 R 中进行了此操作,然后获取了我的 R 函数。

    kmeansPrice <- function(priceArray,clustersNumber)
    {
      `[` <- function(...) base::`[`(...,drop=FALSE) #in case we have a 1 dimensional table
      x<-priceArray
      n<- clustersNumber
      x<-matrix(as.numeric(x),nrow=dim(x)[1],ncol=dim(x)[2])
      cluster = kmeans(x, n)$cluster
      bestBid = rep(NA, n)
      for(i in 1:n)
      {
        assign(paste('group.', i, sep = ''),
        x[cluster == i])
        bestBid[i] = max(get(paste('group.', i, sep = '')))
      }
      return(min(bestBid) + 0.01)
    }
    

    那么你就可以了

    Public Function KmeansPrice(ByVal priceArray As Range, _
                                ByVal clustersNumber As Integer) As Double
    
    rinterface.PutArrayFromVBA "priceArray", priceArray.Value 'I think this ".Value" was your problem'
    rinterface.PutArrayFromVBA "clustersNumber", clustersNumber
    rinterface.RRun "theResult <- kmeansPrice(priceArray,clustersNumber)"
    y = rinterface.GetRExpressionValueToVBA("theResult") 'preferred to GetArrayToVBA for single-value results'
    KmeansPrice = y
    End Function
    

    并使用示例数据运行它:计算结果为的 2x4 表

         [,1] [,2]
    [1,]    5    9
    [2,]    6   10
    [3,]    7   11
    [4,]    8   12
    

    有 3 个“集群”

    Sub runkmeans()
    theResult = KmeansPrice(Range("BH2:BI5"), 3)
    MsgBox (theResult)
    End Sub
    

    产生 6.01

    【讨论】:

    • 感谢您的解决方案建议,我目前无法尝试,但我会尽快尝试并接受您的回答(如果有效)。之前有个问题:当您说“您更喜欢在 R 编辑器中制作函数 然后获取代码”时,如何在 RExcel 中获取 R 函数代码应该工作吗?
    • 在 VBA 中,Rinterface.rrun("source('myfile.r')") 您可以将其放在 auto_open() 中以在 excel 启动时获取源代码,或者仅将其放在 /your r 文件夹/etc 中 RProfile.site 中的 source("myfile.r") 中在 R 启动时获取它
    猜你喜欢
    • 2011-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-20
    相关资源
    最近更新 更多