How to access Olap-Cubes from R on Linux - r

I want to access an Olap-Cube from R on Linux. Specifically I have a SQL-Server 2016 with Analysis Services and Cubes that I would like to access.
We have been accessing the cubes from R on windows using microsoft R and the olapR-package. And while there is a linux version of microsoft-R, it does not include the olapR-package.
We have installed the R-Services for our SQL-Server. And we did install the olapR package there (using the R-Server Binding), but it seems that it doesn't come with support for mrsdeploy.
What other options do I have to access the cubes from R on linux? We heard about the Revoscaler Package, but don't know how to use it on Cubes. Maybe we could post an sql-script containing r-code to the server and have the sql-server execute the r-code? I didn't find any help on this approach though.
Any help is appreciated.

In our case it does work by embedding the olapR-code within T-SQL within RODBC-Code.
library(RODBC)
my_server="<server>"
my_db="<database>"
my_username="<myusername>"
my_pwd="<mypassword>"
db <- odbcDriverConnect(paste0("DRIVER={SQL Server};
server=",my_server,";
database=",my_db,";
uid=",my_username,";
pwd=",my_pwd))
sql="exec sp_execute_external_script
#language =N'R',
#script=N'
library(olapR)
cnnstr <- \"Data Source=<server>; Provider=MSOLAP; initial catalog=<AnalysisService>; User Id=<domain>\\\\<user>; Password=<myPassword>\"
olapCnn <- OlapConnection(cnnstr)
mdx <- \" <MDX-Query> \"
OutputDataSet <- execute2D(olapCnn, mdx)';"
df <- sqlQuery(db,sql)
Please note the quadruple backslash in domain\user.
Please note that the analysis services login is not necessarily the same as the SQL-login
Please note that the SQL user must have the rights to execute external scripts:
GRANT EXECUTE ANY EXTERNAL SCRIPT TO [<user>]
It could be improved some more by using a "with result sets" statement, but it works.

Related

Ask user crendentials when running R in Terminal

I am running an R script from Terminal (not RStudio) and at some point I need the user to write the username and password to access an API. For this, I am using a non-safe approach with the following code:
cat('Write username: ')
username<-readLines("stdin", n=1)
cat('Write password: ')
password<-readLines("stdin", n=1)
I have seen there is a nice implementation in RStudio using the rstudioapi package and the askForPassword function but since I am running my script from Termianl, I wonder if there is any alternative or more secure way to ask for this information
getPass is your best solution.
It supports RStudio and fallsback to tcltk and other backends if missing.
password <- getPass::getPass() is your call.

External Scripting and R (Kognitio)

I have created the R script environment (used this command to create it "create script environment RSCRIPT command '/usr/local/R/bin/Rscript --vanilla --slave'") and tried running the one R script but it fails with the below error message.
ERROR: RS 10 S 332659 R 31A004F LO:Script stderr: external script vfork child: No such file or directory
Is it because of the below line which i am using in the script ?
mydata <- read.csv(file=file("stdin"), header=TRUE)
if (nrow(mydata) > 0){
I am not sure what is it expecting.
I have one more questions to ask.
1) do we need to install the R package on our unix box ? if not then the kognitio package has it
I suspect the problem here is that you have not installed the R environment on ALL the database nodes in your system - it must be installed on every DB node involved in processing (as explained in chapter 10 of the Kognitio Guide which you can download from http://www.kognitio.com/forums/viewtopic.php?t=3) or you will see errors like "external script vfork child: No such file or directory".
You would normally use a remote deployment tool (e.g. HP's RDP) to ensure the installation was identical on all DB nodes. Alternatively, you can leverage the Kognitio wxsync tool to synchronise files across nodes.
Section 10.6 of the Kognitio Guide also explains how to constrain which DB nodes are involved in processing - this is appropriate if your script environment should not run on all nodes for some reason (e.g. it has an expensive per-node/per-core licence). That does not seem appropriate for using R though.

Reading a access database (mdb) in 64 bit in R

I have a database and I need to read that in R. I found some packages such as Hmisc and RODBC which have the functions to do that. I am using windows and was not able to use Hmisc because you need to have mdb-tools package and I found no tutorial or way to install mdb-tools on windows.
Now, I was trying to start with RODBC. I found this question "How to connect R with Access database in 64-bit Window?" which shows how to have a connection in windows. I tried to use the command similar to what was accepted answer in that question.
odbcDriverConnect("Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=E:/Projects\Newdata/2013 Database/Data/pgdabc_SW.mdb")
It gives the following error :
1: In odbcDriverConnect("Driver={Microsoft Access Driver (*.mdb, *.accdb)}, DBQ=E:/Projects\Newdata/2013 Database/Data/pgdabc_SW.mdb") :
[RODBC] ERROR: state 01S00, code 0, message [Microsoft][ODBC Driver Manager] Invalid connection string attribute
2: In odbcDriverConnect("Driver={Microsoft Access Driver (*.mdb, *.accdb)}, DBQ=E:/Projects\Newdata/2013 Database/Data/pgdabc_SW.mdb") :
ODBC connection failed
I am not sure how to check and start diagnosing what's going on here. I went to administrative tools and checked the options on "Data Sources (ODBC)". . I changed the target to sysWOW.
Then I created a new data source as follows:
I am not sure if I need to select database or not. I found Brian Ripley's http://cran.r-project.org/web/packages/RODBC/vignettes/RODBC.pdf RODBC tutorial but still I am not able to make it work.
This works fine for me & might work for you, too:
require(RODBC)
conn <- odbcConnectAccess2007(path.expand("~/Database.accdb"))
subset(sqlTables(conn), TABLE_TYPE == "TABLE")
df <- sqlFetch(conn, "Table1")
close(conn)
My sessionInfo():
# R version 3.1.1 (2014-07-10)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
#
# other attached packages:
# [1] RODBC_1.3-10
#
# loaded via a namespace (and not attached):
# [1] tools_3.1.1
I have had issues with this (trying to query Access 32bit from R 64bit) from a long time. I think it has been fixed in windows 10.
I made a kludge by modifying something I found in this post:
How to connect R with Access database in 64-bit Window?
I made a function that saves a script (which in turn connects to the database and saves the result of the query), run it using R32, and load the data into the R64 work environment.
I prepared it for Access 2007, but something analogue could be done for Access2003 (just using odbcConnectAccess instead of odbcConnectAccess2007) or other 32 bit databases
MysqlQueryAccess2007<-function(filename,query){
tempdir=gsub('\\\\','/',tempdir())
txt<-paste("if (!'RODBC' %in% installed.packages()) install.packages('RODBC')
require(RODBC)
channel<-odbcConnectAccess2007('",filename,"')
data<-sqlQuery(channel,\"",query,"\")
save(data,file=paste('",tempdir,"','tempRODBCquery.Rdata',sep='/'))
close(channel)",sep="")
writeLines(txt,con=paste(tempdir,'RODBCscripttemp.r',sep='/')->tempscript)
system(paste0(Sys.getenv("R_HOME"), "/bin/i386/Rscript.exe ",tempscript))
tt<-get(load(paste(tempdir,'tempRODBCquery.Rdata',sep='/')))
return(tt)
}
Then you only have to do the queries this way:
dat<-MysqlQueryAccess2007("samplefile.accdb","SELECT TOP 5 * FROM TableI")
Have been trying to figure it out for a while myself.
Solution given in the accepted answer here
Reading data from 32-bit Access db using 64-bit R, credits to #erg,
as well as here
How to connect R with Access database in 64-bit Window?, credits to #JATT.
The bottom line:
Install 64-bit Microsoft Access drivers https://www.microsoft.com/en-us/download/details.aspx?id=54920
Setup appropriate System DSN in ODBC Data Sources (64-bit)
In R 64-bit read .mdb file by using odbc package: dbConnect(odbc(), 'your_64bit_dsn').

Using sqldf & RPostgrSQL in opencpu app

I'm creating a very simple sqldf app where I want to query postgresql database and return results. Here is my R code:
getproperties <- function(name="") {
options(sqldf.RPostgreSQL.user ="user",
sqldf.RPostgreSQL.password ="password",
sqldf.RPostgreSQL.dbname ="db",
sqldf.RPostgreSQL.host ="server",
sqldf.RPostgreSQL.port =5432,
sqldf.driver = "PostgreSQL")
property <- sqldf("select name, url from aston_hotels.property")
return(property)
}
When I call this using OpenCpu
curl http://localhost/ocpu/user/kesav/library/mylib/getproperties/json --data name=kk
I get the following error
R call failed: process died.
Don't know what's I'm doing wrong. Can anyone point me to an example on how to use DBI from OpenCpu?
Thanks in advance
If the function works when running it on the same machine the terminal but not within OpenCPU, it is likely a security problem. Please check in /var/log/kern.log if there are any lines printed containing DENIED when using the application.
If this is the case, you can add the appropriate privileges the the security profile by editing
/etc/apparmor.d/opencpu.d/custom
Also see the server manual section titled Customizing the security profile on this topic.

Unexpected behavior of R after install on another EC2 instance

I'm fighting this problem second day straight with a completely sleepless night and I'm really starting to lose my patience and strength. It all started after I decided to provision another (paid) AWS EC2 instance in order to test my R code for dissertation data analysis. Previously I was using a single free tier t1.micro instance, which is painfully slow, especially when testing/running particular code. Time is much more valuable than reasonable number of cents per hour that Amazon is charging.
Therefore, I provisioned a m3.large instance, which I hope should have enough power to crunch my data comfortably fast. After EC2-specific setup, which included selecting Ubuntu 14.04 LTS as an operating system and some security setup, I installed R and RStudio Server per instructions via sudo apt-get install r-base r-base-dev as ubuntu user. I also created ruser as a special user for running R sessions. Basically, the same procedure as on the smaller instance.
Current situation is that any command that I issuing in R session command line result in messages like this: Error: could not find function "sessionInfo". The only function that works is q(). I suspect here a permissions problem, however, I'm not sure how to approach investigating permission-related problems in R environment. I'm also curious what could be the reasons for such situation, considering that I was following recommendations from R Project and RStudio sources.
I was able to pinpoint the place that I think caused all that horror - it was just a small configuration file "/etc/R/Rprofile.site", which I have previously updated with directives borrowed from R experts' posts here on StackOverflow. After removing questionable contents, I was able to run R commands successfully. Out of curiosity and for sharing this hard-earned knowledge, here's the removed contents:
local({
# add DISS_FLOSS_PKGS to the default packages, set a CRAN mirror
DISS_FLOSS_PKGS <- c("RCurl", "digest", "jsonlite",
"stringr", "XML", "plyr")
#old <- getOption("defaultPackages")
r <- getOption("repos")
r["CRAN"] <- "http://cran.us.r-project.org"
#options(defaultPackages = c(old, DISS_FLOSS_PKGS), repos = r)
options(defaultPackages = DISS_FLOSS_PKGS, repos = r)
#lapply(list(DISS_FLOSS_PKGS), function() library)
library(RCurl)
library(digest)
library(jsonlite)
library(stringr)
library(XML)
library(plyr)
})
Any comments on this will be appreciated!

Resources