Using X4R package in R to connect to an SSAS (local) data cube?

删除回忆录丶 提交于 2020-01-16 00:44:43

问题


I am trying to figure out how to use X4R package to load a local data cube into R. I can load the cube into Excel, but am failing to get this to work with R.

My cube filename is "\scrsvr\Users\\Documents\Projects\Raw data\data.cub".

I tried the following code:

library(X4R)
handle<-xmlaConnect(url="\\scrsvr\\Users\\<MyName>\\Documents\\Projects\\Raw data\\data.cub")

which produces no error. However, trying to use xmlaDiscover results in the following error:

first argument is not an open XMLA handle

Can anyone help me to connect to my local cube? I am totally new to this, so perhaps someone has a working example that I could follow (with example data cube file)?

Thanks!


回答1:


Ok, just in case anyone else needs to do this in future, I managed to solve my problem

# use some necessary packages
require(RDCOMClient)
require(data.table)
library(plyr)
library(Hmisc)

#create connection to SSAS datacube
con <- COMCreate("ADODB.Connection")
con[["ConnectionString"]] <- paste(
  "Provider=MSOLAP.5",
  "Data Source=\\\\scrsvr\\Users\\<MyName>\\Documents\\Projects\\Raw data\\data.cub",
    "Persist Security Info=True",
  sep = ";")
con$Open()

# define the MDX query here:
query = "SELECT ..."

rs <- COMCreate("ADODB.RecordSet")

# submit the MDX query to the cube
rs$Open(query, con)

rs$MoveFirst()    # move to the first row of the record set
nc <- rs$Fields()$Count()     # define number of columns

# get the data into a data array:
dd <- vector("list", length=nc)
dd <- rs$GetRows()  # get the raw data from the results

For the connection, I used the Provider ("MSOLAP.5") that was configured when I set up a data connection from MS Excel to the data cube. It all works and I can now query the database using MDX directly.



来源:https://stackoverflow.com/questions/36456123/using-x4r-package-in-r-to-connect-to-an-ssas-local-data-cube

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!