In my previous HANA and R blogs, I have been forced to create .csv files from HANA and read them on R...an easy but also boring procedure...specially if your R report is supposed to be run on a regular basis...having to create an .csv file every time you need to run your report it's not a nice thing...
After spending some time reading and researching R...I finally came to a library that can read data from any relational database and being HANA, ODBC capable, the work is just a piece of cake -;)
For this examples, we must install two libraries: RODBC and Plotrix and create the DSN connection as shown here...
Here we're going to "Add..." a new "User DSN"
HANA already provides us a driver, so we're cool
Assign a name for the "Data Source Name", "Description" is optional and "Server:Port" should be of course filled.
Now...we're ready to go to our HANA studio an create a table and a stored procedure...
CREATE PROCEDURE GetTicketsByYearMonth
(IN var_year NVARCHAR(4),IN var_month NVARCHAR(2))
LANGUAGE SQLSCRIPT AS
BEGIN
select count(bookid), carrid
from sflight.snvoice
where year(fldate) = VAR_YEAR
and month(fldate) = VAR_MONTH
group by carrid
into TICKETS_BY_YEAR_MONTH;
END;
CALL P075400.GetTicketsByYearMonth('2011','12');
After we run our Stored Procedure...we have all the information in the table...Ok...only two fields...today was a hard day...I'm tired -:P
Finally...we can code some R! First, we're going to create a Fan Plot (The Plotix library is needed for that one) and then a Bar Plot...I used the same code for both, so just replace the comment on one by the other one and run it again...I know...I'm being lazy again...but at least I'm not reinveting the wheel -;) Two codes with only 1 different line? No thanks...
library("plotrix")
library("RODBC")
ch<-odbcConnect("HANA",uid="P075400",pwd="***")
res<-sqlFetch(ch,"P075400.TICKETS_BY_YEAR_MONTH")
fan.plot(res$TICKETS,labels=res$CARRIER,
main="Tickets for December 2011")
#barplot(res$TICKETS,names.arg=res$CARRIER)
odbcClose(ch)
The code is very simple...we call the libraries we need, we stablish a communication to our DSN, we fetch the data from the table, we create the graphics and finally we close the connection.
And here come the graphics...
I will keep investigating on this way to connect HANA and R...more blogs should be on the way -;)
Greetings,
Blag.
1 comentario:
Hi Blag,
I have been following your career since I started in ABAP programming. It's an inspiration your blogs and how you have achieved a lot. Good to know you're working for SAP Labs. Enjoy summer.
Greetings from Colombia. BTW where can I find more sound information about R.
Publicar un comentario