Skip to Content
Technical Articles
Author's profile photo Ashutosh Rastogi

Tables in SAC using flextable package of R

If you have a usecase where you need to build assymetric table for displaying in SAP Analytics Cloud, you may not be able to achieve that with standard tables in SAC. In such cases, if the usecase is only consumption then R can be used to build such tables.

One such usecase is listed in blog  https://blogs.sap.com/2018/11/05/creating-a-custom-crosstab-with-r-in-sap-analytics-cloud/

Flextable package has been included in the pre-deployed libraries on the R server. The complete list is available here.

Sample script that you can simply paste and use to display an R table in SAC

library(ggplot2)
library(gridExtra)
library(flextable)
library(officer)

ActualData = c ("Open Rev.", "Revenue", "Direct Cost", 
                "Indirect Cost", "Total Cost", "Taxes", "Margin")

LY = c(0.0, 
       1079511249,
       806777210.4,
       161822021.7,
       968956266.1,
       357034,
       110554982.4)

LM = c(0.0, 
       1244782036,
       922165421.4,	
       185576898,
       1108099353,
       357034,
       136682682.5)

CTM = c(0.0, 
        1312589822,
        973687717.9,
        195670590.5,
        1169742397,
        384088.22,
        142847425.7) 

FTM = c(CTM[1] - LM[1], CTM[2] - LM[2], CTM[3] - LM[3], CTM[4] - LM[4], CTM[5] - LM[5], CTM[6] - LM[6], CTM[7] - LM[7])

YTM = c(CTM[1] - LY[1], CTM[2] - LY[2], CTM[3] - LY[3], CTM[4] - LY[4], CTM[5] - LY[5], CTM[6] - LY[6], CTM[7] - LY[7])

ActualData <- format(ActualData, justify = "left")
LY <- format(LY, digits=5, nsmall=2, big.mark=",", justify = "right")
LM <- format(LM, digits=5, nsmall=2, big.mark=",", justify = "right")
CTM <- format(CTM, digits=5, nsmall=2, big.mark=",", justify = "right")
FTM <- format(FTM, digits=5, nsmall=2, big.mark=",", justify = "right")
YTM <- format(YTM, digits=5, nsmall=2, big.mark=",", justify = "right")

df.data <- data.frame(ActualData, 
   LY, LM, CTM, FTM, YTM)

myft <- flextable(df.data, col_keys = c("ActualData", "LY", "LM", "CTM", "FTM", "YTM") )
myft <- set_header_labels(myft, ActualData = " ")
myft <- bold(italic(myft,j=1), j=1)
myft <- bg(myft, bg = "grey", part = "header")
myft <- color(myft, color = "black", part = "header")
myft <- autofit(myft)
myft

 

And here is the output visualization

More on flextable https://davidgohel.github.io/flextable/articles/overview.html

 

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.