The first time I heard about R, was about 4 years ago...a couple of week after I joined SAP. At that time I read in one of our internal documents that SAP HANA was going to be able to interact with the R programming Language.
At first, I was totally clueless about R...I had never heard from it before...so I of course start looking for some more information, download R and RStudio and start learning how to use it...
After some time...I posted my first blog talking about R...that was on November 28, 2011...the blog was Dealing with R and HANA...
After that I kept learning and using it whenever it was suitable...and I end up writing my most successful blog on the SAP Community Network...that was on May 21, 2012.
That up to now, has 21,879 views and 62 comments, 16 likes and 32 bookmarks.
R is huge...it really is...there's thousands of packages that solve thousands of issues...so that's when I start reading R related books from my good friends at Packt Publishing...
As I was improving my R skills...I knew that something was missing...I really didn't knew much about Statistics and my Machine Learning skills were pretty dull as well...that's when I read another awesome book...and I think my favorite book on R so far...
If you're wondering how Social Media can be used with R, then please take a look at my blog Getting Flexible with SAP HANA where I use SAP HANA, R, Twitter and Schema Flexibility to analyze hashtags that can be further explored using for example SAP Lumira.
If you are into the Bioinformatics world...which I'm not -:( You should appreciate this book...
It's an event that happens every Thursday per week. An hour and little bit more of "hands-on" session where not so common programming languages (ex: Lua, Julia, R, Haskell) and Programming Paradimgs (Functional, Stack Based, Logical, Object-Oriented) are being presented.
Why Geeky Thursday?
Because Developers...because knowing just a couple of programming languages is not enough...because learning another paradigm can expand your horizons and make a better developer.
Geeky Thursday is hands-on...do I need my laptop?
Well...it really depends on you...the source code is going to be available...it's going to be explained and run on-site...there's not going to be live typing...but it's always better if you can compile and run the examples yourself...
Is Geeky Thursday only about Programming?
No...without any aims to become a "Brown Bag Session"...Geeky Thursday looks forward to become a "Mini Meetup" space...where cool things can be explored.
Is Geeky Thursday held only on Silicon Valley?
For now...it is...but...if you manage or you are in touch with a d-shop manager, ask him/her to get in touch with me (a.tejada.galindo@sap.com)...I would be more than happy to share my slides and anything needed to expand Geeky Thursday to other locations.
Is any space limitation?
Yes, we can invite 10 people from SAP and we have space for 5 people that external to SAP. Depending on the attendance level we might increase this values.
Can I host a Geeky Thursday event?
Of course! You're more than welcome! Just get in touch with me and will be setup an slot for you!
Scheduled Geeky Thursdays
R Programming Language --> October 8 from 1:00 to 2:00pm (d-shop on Building 9 at SAP Labs Silicon Valley)
Julia is a high-level dynamic programming language designed to address the requirements of high-performance numerical and scientific computing while also being effective for general-purpose programming, even web use or as a specification language.
Clojure is a dynamic programming language that targets the Java Virtual Machine (and the CLR, and JavaScript). It is designed to be a general-purpose language, combining the approachability and interactive development of a scripting language with an efficient and robust infrastructure for multithreaded programming. Clojure is a dialect of Lisp, and shares with Lisp the code-as-data philosophy and a powerful macro system.
Racket is a full spectrum programming language that goes beyond Lisp and Scheme with dialects that support objects, types, laziness and more. And that more means Functional, Procedural, Object Oriented, Logic, Reflective and Meta.
Basically…Racket is the new name of PLT Scheme. And PLT Scheme comes from the Lisp family.
So, before we move on…I want to share an XKCD comic that brings some light about Lisp -:)
First, we need to create a Calculation View and call it “FLIGHTS_BY_CARRIER”. It will be composed of two tables, SCARR and SFLIGHT.
First, we need to create a Join object and link the table by MANDT and CARRID. From here select the following fields as output MANDT, CARRID, CARRNAME, PRICE and CURRENCY.
Then create an Aggregation object selecting the fields CARRNAME, PRICE (As Aggregated Column) and CURRENCY. Filter the CURRENCY field by ‘USD’.
Then create a Projection object and select only PRICE and CARRNAME.
On the Semantics object make sure to select “CROSS CLIENT” as the Default Client.
Now, switch to the SAP HANA Development View and create a new repository. Call it “Flights”.
Create a new “XS Engine” project and call it “Flights” as well. Link it to the “Flights” repository.
Create an empty “.xsapp” file.
Create a file called “.xsaccess” with the following code.
Now you see it…Racket is all about parenthesis -:) Anyway…press the run button and you will have this…
I have been interfacing SAP HANA OData with a lot of languages…and so far…Racket has been one of my fastest implementations…while it’s hard to get used to all these weird parenthesis…it’s a very nice and powerful language…and thing get done faster that one could imagine…
Following my tradition...here's my LED Numbers app using Falcon...I used my Julia code as a base start...and I gotta say...the code is pretty much the same...which is nice for now...but I hope to find some hidden and weird features as I learn more about Falcon -;)
Here's the source code...
LED_Numbers.fal
leds = ["0" => [" _ ","| | ","|_| "],
"1" => [" ","| ","| "],
"2" => [" _ "," _| ","|_ "],
"3" => ["_ ","_| ","_| "],
"4" => [" ","|_| "," | "],
"5" => [" _ ","|_ "," _| "],
"6" => [" _ ","|_ ","|_| "],
"7" => ["_ "," | "," | "],
"8" => [" _ ","|_| ","|_| "],
"9" => [" _ ","|_| "," _| "]]
print("Enter a number: ")
number = input()
len = number.len()
for i in [0:3]
for j in [0:len]
print(leds[number[j]][i])
end
printl("")
end
I discovered Falcon a couple of months ago...but didn't have the time to start playing with it...
Falcon is an open source, multi-paradigm programming language. While it looks like a new programming language, it's actually kind of old having it's first appearance on 2003...
Syntactically, it's pretty similar to Lua...which means is kind of similar to Go or C++...but of course it has some pretty and unique cool features -;)
For now...I just ported my Lua version of the Fibonacci Numbers...and beside the fact that Falcon doesn't use "then" and the fact that Falcon can't concatenate numbers with string (but can concatenate string with numbers)...the code is pretty much the same...
Let's see how it goes when I code my LED Numbers app -;)
Here's the code...
fibonacci.fal
function fib(num,a,b)
result = ""
if a > 0 and num > 1
result = result + (a+b) + " " + fib(num-1,a+b,a)
elif a == 0
result = "" + a + " " + b + " " + (a+b) + " " + fib(num-1,a+b,b)
end
return result
end
print("Enter a number: ")
num = int( input() )
print(fib(num,0,1))
Haskell is a general-purpose purely functional programming language with non-strict semantics and strong static typing.
What does that means? Nothing…just that Haskell is the most pure programming language around…which means that there are not and cannot be side effects…like Haskellers like to say…”If it compiles…it will always work”…
There’s an XKCD cartoon that nicely resumes what Haskell is about -;)
Well…I will run it -:P What I love about Haskell is that it really makes you forget everything you learned before…once assigned, a variable can’t never change its value…recursion is a must…printing on the terminal is considered impure, but there’s of course a work around it…everything is a function…forget about Classes and Objects…pattern matching is your best friend…and so many more cool things -;)
So…no example or demonstration would be complete if we didn’t hook it up with SAP HANA, right? So…let’s go and do it -;)
First, we need to create a Calculation View and call it “FLIGHTS_BY_CARRIER”. It will be composed of two tables, SCARR and SFLIGHT.
First, we need to create a Join object and link the table by MANDT and CARRID. From here select the following fields as output MANDT, CARRID, CARRNAME, PRICE and CURRENCY.
Then create an Aggregation object selecting the fields CARRNAME, PRICE (As Aggregated Column) and CURRENCY. Filter the CURRENCY field by ‘USD’.
Then create a Projection object and select only PRICE and CARRNAME.
On the Semantics object make sure to select “CROSS CLIENT” as the Default Client.
Now, switch to the SAP HANA Development View and create a new repository. Call it “Flights”.
Create a new “XS Engine” project and call it “Flights” as well. Link it to the “Flights” repository.
Create an empty “.xsapp” file.
Create a file called “.xsaccess” with the following code.
Finally create a file called “flights.xsodata” with the following code
flights.xsodata
service {
"Blag/FLIGHTS_BY_CARRIER.calculationview" as "FLIGHTS" keys
generate local "Id";
}
Activate your project and launch it on your browser, you should see something like this…
The SAP HANA part is done…so we can move into the Haskell part…
On a terminal session type this…
•cabal update
•cabal install --j --disable-tests wreq
•In later versions of wreq, lens and aeson are already included…but just in case…
•cabal install lens
•cabal install aeson
To code for Haskell I use one of the most awesome editors I have ever found…Geany. It comes almost pre-configured to handle a lot of programming languages, so you will be ready to code for Haskell.
Basically you only need to press the “Run or view the current file”, last image that shows some engines…which basically will bring up GHCi which is the Glasgow Haskell Compiler Interactive.
With that, we should be more than ready to start coding -:) Copy and paste the following code…
Haskell_HANA.hs
{-# LANGUAGE OverloadedStrings #-}
import Network.Wreq
import Control.Lens
import Data.Aeson
import Data.Aeson.Lens
import Data.Aeson.Types
import Data.Text
import Data.Vector
import Prelude as P
import qualified Data.ByteString.Lazy as B
get_info :: B.ByteString -> Text -> Text -> Text -> Text -> Int -> Int -> String
get_info json d results carrname price len ctr
| ctr < len = unpack(json ^. key d . key results . nth ctr . key carrname . _String) P.++
": " P.++ unpack(json ^. key d . key results . nth ctr . key price . _String) P.++ "\n"
P.++ get_info json d results carrname price len (ctr+1)
| ctr >= len = []
main :: IO ()
main = do
let d = pack "d"
let results = pack "results"
let carrname = pack "CARRNAME"
let price = pack "PRICE"
let opts = defaults & auth ?~ basicAuth "SYSTEM" "YourPassword"
r <- getWith opts "http://YourHANAServer:8000/Flights/flights.xsodata/FLIGHTS?$format=json"
let json = r ^. responseBody
let results_array = json ^. key d . key results . _Array
let len = Data.Vector.length results_array
putStr $ get_info json d results carrname price len 0
Save your file and press the “Run or view the current file” button.
You will need to use the format “:l name_of_your_file” to load it into memory and then you can just call the main function to execute it -;)
I have to say…Haskell is not an easy language…and pulling out this blog wasn’t an easy task either…I need to do a lot of research…but gladly the Wreq and Aeson packages are simply awesome!
If you are an SAP Employee, please follow us on Jam.
Even when Google Glass is no more...at least not for individuals…I thought that connecting it to SAP HANA would make for a nice blog...because after all we have one at the d-shop…so here we go -;)
First, we need to create a Calculation View and call it “FLIGHTS_BY_CARRIER”. It will be composed of two tables, SCARR and SFLIGHT.
First, we need to create a Join object and link the table by MANDT and CARRID. From here select the following fields as output MANDT, CARRID, CARRNAME, PRICE and CURRENCY.
Then create an Aggregation object selecting the fields CARRNAME, PRICE (As Aggregated Column) and CURRENCY. Filter the CURRENCY field by ‘USD’.
Then create a Projection object and select only PRICE and CARRNAME.
On the Semantics object make sure to select “CROSS CLIENT” as the Default Client.
Now, switch to the SAP HANA Development View and create a new repository. Call it “Flights”.
Create a new “XS Engine” project and call it “Flights” as well. Link it to the “Flights” repository.
Create an empty “.xsapp” file.
Create a file called “.xsaccess” with the following code.
We’re almost ready…we need to connect our Google Glass and make sure it’s on Debug On.
Navigate to “Settings” and enter it.
Navigate to “Device info” and click it
Navigate to “Turn on debug” and click it. It should become “Turn off debug”
With that we will be able to link our Google Glass to our Android Studio project. Simply run it and you will see this on the Glass.
Click to enter it and you will see the main screen
Say a carrier name like “American Airlines”
Wait and the application will connect to our SAP HANA OData and display the result
Now, to not make you believe that I’m just making this up…try another one…like “Lufthansa”
Wait to see the result…
Cool, huh? You could also ask for “Delta Airlines” or “United Airlines”…if you ask for something else, it might give you back the result for the most approximate name or just the default one.
If you are an SAP Employee, please follow us on Jam.
Amazon Alexa (Formerly Amazon Echo) is an awesome technology that allows you to give voice commands and get cool answers. Yes…it’s like Siri but from Amazon. Also, it doesn’t live on your cellphone, as Alexa is black cylinder packed up with seven microphones and very loud speakers.
We just get one for the d-shop almost a week ago…so of course…I needed to hack it and make it work with SAP HANA -;)
First, we need to create a Calculation View and call it “FLIGHTS_BY_CARRIER”. It will be composed of two tables, SCARR and SFLIGHT.
First, we need to create a Join object and link the table by MANDT and CARRID. From here select the following fields as output MANDT, CARRID, CARRNAME, PRICE and CURRENCY.
Then create an Aggregation object selecting the fields CARRNAME, PRICE (As Aggregated Column) and CURRENCY. Filter the CURRENCY field by ‘USD’.
Then create a Projection object and select only PRICE and CARRNAME.
On the Semantics object make sure to select “CROSS CLIENT” as the Default Client.
Now, switch to the SAP HANA Development View and create a new repository. Call it “Flights”.
Create a new “XS Engine” project and call it “Flights” as well. Link it to the “Flights” repository.
Create an empty “.xsapp” file.
Create a file called “.xsaccess” with the following code.
In order to code for Amazon Alexa, we can use a new and awesome service from Amazon called Amazon Lambda…which basically allows you to define a NodeJS service that will run only when you need it to run…and also allows you to use the first million requests per month for free…
First…log into your Amazon Web Services Account and get into Amazon Lambda. Make sure you are on the North Virginia region…otherwise this will not work…
Once in there…create a new lambda function and select alexa-skills-kit-color-expert
Then, select the “Alexa Skills
Kit” Event Source Type…
Then, we need to configure our
function…meaning, we need to name it…
Leave the code entry type for later and choose a role…
Choose Basic execution role and a
pop up will be shown…
Press the “Allow it” button at the bottom. Continue and finally press the “Create Function” button. A ARN number will be generated for your function, take note of it as we will need it on the next step.
Press
the “Add New Skill” button and enter the Name, Invocation Name (How are we
going to call it from Alexa), version and the Endpoint (In this case, the ARN
number from our Lambda Function)…
When we click on next, an
Application Id number will be generated. Take note of it as we will need it on
the coding part…
The Interaction Model section is
very important as here we’re going to define the “Intent Schema” and “Sample
Utterances”…the first will define the parameters that we’re going to send to
Alexa and the second is how we are going to call our application.
Our
variable is going to be called “airline” and it’s going to be a LITERAL…other
types are NUMBER, DATE, TIME and DURATION. The intent is the method that we’re
going to call in our code…
Sample Utterances
GetSAPHANAIntent get total amount for airline {American Airlines|airline}
GetSAPHANAIntent get total amount for airline {United Airlines|airline}
GetSAPHANAIntent get total amount for airline {Delta Airlines|airline}
GetSAPHANAIntent get total amount for airline {Lufthansa Airlines|airline}
GetSAPHANAIntent get amount for {Delta Airlines|airline}
GetSAPHANAIntent get amount for {Lufthansa|airline}
GetSAPHANAIntent get amount for {United Airlines|airline}
GetSAPHANAIntent get amount for {American Airlines|airline}
GetSAPHANAIntent get {United Airlines|airline}
GetSAPHANAIntent get {Lufthansa|airline}
GetSAPHANAIntent get {Delta Airlines|airline}
GetSAPHANAIntent get {American Airlines|airline}
HelpIntent help
HelpIntent help me
HelpIntent what can I ask you
HelpIntent get help
HelpIntent to help
HelpIntent to help me
HelpIntent what commands can I ask
HelpIntent what commands can I say
HelpIntent what can I do
HelpIntent what can I use this for
HelpIntent what questions can I ask
HelpIntent what can you do
HelpIntent what do you do
HelpIntent how do I use you
HelpIntent how can I use you
HelpIntent what can you tell me
HelpIntent how do i get an amount
The
Test section is where we can test our application by sending a utterance like
“get American Airlines”…we need to create our function code before we can test
it…so don’t worry about this for now…
Forget about the Publishing
Information section unless you really want to publish your application…
Let’s create a folder and call it
“Alexa_HANA” or something nice like that…then create a folder called “src”.
Copy the code from here and create a file called “AlexaSkills.js”
We’re going to need to install
the node-rest-client package on our function, so do the following on a terminal
This will create a folder called “node_modules”
with the package in our project folder…then create a file called “index.js” and
copy and paste the following code…
index.js
var Client = require('node-rest-client').Client
, AlexaSkill = require('./AlexaSkill')
, APP_ID = 'amzn1.echo-sdk-ams.app.XXX';
var error = function (err, response, body) {
console.log('ERROR [%s]', err);
};
function toTitleCase(str)
{
return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
var getJsonFromHANA = function(airline, callback){
var sAirline = toTitleCase(airline);
var options_auth={user:"SYSTEM",password:"YourPassword"};
client = new Client(options_auth);
client.get("http://YourServer:8000/Flights/flights.xsodata/FLIGHTS?$format=json&$filter=CARRNAME eq \'" +
sAirline + "\'", function(data, response){
if (data.d.results[0] != undefined){
var Amount = data.d.results[0].PRICE;
}else{
var Amount = "Sorry I coudln't find that airline";
}
callback(Amount);
}).on('error',function(err){
callback("Sorry there was a connection error");
});
}
var handleHANARequest = function(intent, session, response){
getJsonFromHANA(intent.slots.airline.value, function(data){
var text = data;
var cardText = 'Total sales are: ' + text;
var heading = 'Total sales for: ' + intent.slots.airline.value;
response.tellWithCard(text, heading, cardText);
});
};
var HANA = function(){
AlexaSkill.call(this, APP_ID);
};
HANA.prototype = Object.create(AlexaSkill.prototype);
HANA.prototype.constructor = HANA;
HANA.prototype.eventHandlers.onSessionStarted = function(sessionStartedRequest, session){
console.log("onSessionStarted requestId: " + sessionStartedRequest.requestId
+ ", sessionId: " + session.sessionId);
};
HANA.prototype.eventHandlers.onLaunch = function(launchRequest, session, response){
// This is when they launch the skill but don't specify what they want.
var output = 'Welcome to S A P HANA. ' +
'Say an Airline Name.';
var reprompt = 'Which Airline would you like?';
response.ask(output, reprompt);
console.log("onLaunch requestId: " + launchRequest.requestId
+ ", sessionId: " + session.sessionId);
};
HANA.prototype.intentHandlers = {
GetSAPHANAIntent: function(intent, session, response){
handleHANARequest(intent, session, response);
},
HelpIntent: function(intent, session, response){
var speechOutput = 'Get the total amount for any airline. ' +
'Which Airline would you like?';
response.ask(speechOutput);
}
};
exports.handler = function(event, context) {
var skill = new HANA();
skill.execute(event, context);
};
Once is done, we will need to
.zip the files…so go into your “src” folder and create a .zip file that include
the “node_modules” folder, the AlexaSkills.js file and the index.js file…you
can call the .zip file “SAPHANA.zip”.
Now, go back to your Amazon Lambda function and
choose the Code tab. Then choose “Upload a .ZIP file” and press Upload. Choose
your SAPHANA.zip file and then press the “Save” button.
With that…we should be ready to
keep going -;)
We can now use the Test section of our Alexa
Skill…
Once you know it’s working…you can
test your function in your Amazon Alexa by saying…
“Alexa ask get hana”
And then “get American Airlines”
or “get total amount for Delta Airlines”
Here’s a video to show you how it
works -:)
As you can see…it’s actually
pretty easy but there’s something you need to keep in mind…Alexa will not be
able to recognize everything you say…so you need to be careful with your
utterances…also…in SAP HANA we have “American Airlines” but Alexa will understand
“american airlines”…that’s why I needed to capitalize the first letter as
otherwise, both are different airline names -;)