Skip to Content

As I wrote in my blog Analytics with SAP and R (Windows version) we can use RSAP to connect to our ERP system and play with the data.

This time I wanted of course, to keep exploring the capabilities of RSAP, but using something else. As everybody knows, I love micro-frameworks, so for R that not an exception…gladly, Rook came to the rescue…

Rook is a simple web server that will run locally and will allow us to do some really nice things…enough talk…let’s go to the source code…

RSAP_Rook.R

library(“RSAP”)

require(“Rook”)

setwd(“C:/Blag/R_Scripts”)

conn = RSAPConnect(“sap.yml”)

parms <- list(‘DELIMITER’ = ‘;’,

              ‘FIELDS’ = list(FIELDNAME = list(‘CARRID’, ‘CARRNAME’)),

              ‘QUERY_TABLE’ = ‘SCARR’)

res <- RSAPInvoke(conn, “RFC_READ_TABLE”, parms)

#RSAPClose(conn)

scarr<-res$DATA

flds<-sub(“\\s+$”, “”, res$FIELDS$FIELDNAME)

scarr<-data.frame(colsplit(scarr$WA,”;”, names=flds))

parms <- list(‘DELIMITER’ = ‘;’,

              ‘FIELDS’ = list(FIELDNAME = list(‘CITYFROM’)),

              ‘QUERY_TABLE’ = ‘SPFLI’)

res <- RSAPInvoke(conn, “RFC_READ_TABLE”, parms)

#RSAPClose(conn)

spfli<-res$DATA

flds<-sub(“\\s+$”, “”, res$FIELDS$FIELDNAME)

spfli<-data.frame(colsplit(spfli$WA,”;”, names=flds))

spfli<-unique(spfli)

get_data<-function(p_carrid,p_cityfrom){

  parms<-list(‘DELIMITER’ = ‘;’,

              ‘FIELDS’ = list(FIELDNAME = list(‘CITYTO’,’FLTIME’)),

              ‘OPTIONS’ = list(TEXT = list(p_carrid, p_cityfrom)),

              ‘QUERY_TABLE’ = ‘SPFLI’)

  res<-RSAPInvoke(conn, “RFC_READ_TABLE”, parms)

  RSAPClose(conn)

  spfli<-res$DATA

  flds<-sub(“\\s+$”, “”, res$FIELDS$FIELDNAME)

  if(length(spfli$WA)>0){

  spfli<-data.frame(colsplit(spfli$WA,”;”, names=flds))

  return(spfli)

  }else{

   return(spfli)

  }

}

newapp<-function(env){

  req<-Rook::Request$new(env)

  res<-Rook::Response$new()

  res$write(‘<form method=”POST”>\n’)

  res$write(‘<div align=”center”><table><tr>’) 

  res$write(‘<td>Select a carrier: <select name=CARRID>’)

  for(i in 1:length(scarr$CARRID)) {

    res$write(sprintf(‘<OPTION VALUE=%s>%s</OPTION>’,scarr$CARRID[i],scarr$CARRNAME[i]))

  }

  res$write(‘</select></td><td>’)

  res$write(‘Select a city: <select name=CITYFROM>’)

  for(i in 1:length(spfli$CITYFROM)) {

    res$write(sprintf(‘<OPTION VALUE=%s>%s</OPTION>’,spfli$CITYFROM[i],spfli$CITYFROM[i]))

  }

  res$write(‘</select></td>’)

  res$write(‘<td><input type=”submit” name=”Get Flights”></td>’)

  res$write(‘</tr></table></div>’)

  res$write(‘</form>’)

  if (!is.null(req$POST())) {

    p_carrid = req$POST()[[“CARRID”]]

    p_cityfrom = req$POST()[[“CITYFROM”]]

    flights_from<-paste(‘Distance in Flights from ‘,p_cityfrom,sep=”)

    p_carrid<-paste(‘CARRID = \”,p_carrid,’\”,sep=”)

    p_cityfrom<-paste(‘AND CITYFROM =\”,p_cityfrom,’\”,sep=”)

    spfli<-get_data(p_carrid,p_cityfrom)

    if(length(spfli$CITYTO) > 0){

    png(“Flights.png”,width=800,height=500)

    plot(spfli$FLTIME,type=”n”,axes=FALSE,ann=FALSE)

    lines(spfli$FLTIME,col=”blue”)

    points(spfli$FLTIME, pch=21, bg=”lightcyan”, cex=1.25)

    box()

    xy<-length(spfli$CITYTO)

    axis(2, col.axis=”blue”, las=1)

    axis(1, at=1:xy, lab=spfli$CITYTO, col.axis=”purple”)

    title(main=flights_from, col.main=”red”, font.main=4)

    dev.off()

    res$write(“<div align=’center’>”)

    res$write(paste(“<img src='”, server$full_url(“pic”), “/”, “Flights.png'”, “/>”, sep = “”))

    res$write(“</div>”)

    }else{

      res$write(“<p>No data to select…</p>”)

    }

  }

  res$finish()

}

server = Rhttpd$new()

server$add(app = newapp, name = “Flights”)

server$add(app = File$new(“C:/Blag/R_Scripts”), name = “pic”)

server$start()

server$browse(“Flights”)

This is the result…

RSAP_Rook_001.png

RSAP_Rook_002.png

RSAP_Rook_003.png

RSAP_Rook_004.png

As you can see, we’re getting the data from SAP to fill both SELECT’s and then call out the query. We generate a PNG graphic showing the distance from the City From to the City To and then call it from our Web Page to show it on the screen.

As you can see, RSAP give us a lot of opportunities that we can take advantage by simply putting some effort and imagination. Hope this boots your R interest

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

  1. Andrew Barnard

    Hi Alvaro,

    Some time back, I was doing some performance analytics for an SAP EAM customer based upon ERP transactional data. I wish I’d had a combination of R and SAP back then – it would have transformed the project deliverables from a few rudimentary “numbers” to some quite meaningful statistical inferences. Having R in the back pocket opens up a wide range of statistical analyses that would requirement a lot of heavy lifting in ABAP.

    Thanks for inspiring me to get more acquainted with R.

    Cheers

    Andrew

    (0) 
    1. Alvaro Tejada Galindo Post author

      Hello Andrew:

      Thanks for your comment 🙂 Having the option to integrate ERP and R is really something awesome and very helpful…

      I believe everyone should spend some time learning R…simple yet powerful and fun to use 😉

      Greetings,

      Blag.

      (0) 

Leave a Reply