Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

USE: To create custom R components and pot custom graphs in SAP PA

One of the most frequent requirement that we have in R is installing packages and referring them. Another requirement is using the plot functions to plot graphs. This graphs however are not directly available in SAP PA. But we will see how we can make them available in SAP PA using some R functions.

Here I have taken the example of the iris dataset that comes with R. For instructions on how to work with iris dataset in SAP PA please refer to the link below:

http://scn.sap.com/community/predictive-analysis/blog/2013/07/23/iris-clustering-using-r-nnet-neural-network

Now lets say we need a qplot of the Sepal and Petal Lengths of the above data set. For this we need to use the qplot() (also known as Quick Plot) function. qplot is the basic plotting function in the  ggplot2 package. It is a  convenient wrapper for creating a number of different  types of plots using a consistent calling scheme. qplot makes it easy to produce complex plots, often requiring several lines of code using other plotting systems, in one line.

We create a basic component in R that gives us the qplot for Petal and Sepal lengths of the iris dataset. The steps are as shown below:

STEP1: Creating a Custom R component to plot a simple X-Y function.

Assuming that we have imported the iris dataset, lets goto the predict tab and Insert a new component. Lets call the new component QPLOT.

I have created 3 variables: x, y and z.

X, Y allows us to pick the column we want for and x and y coordinates of the plot and Z allows us to decide how the color of the plot should be decided. We are deciding the color of the plot based on species.

Select show visualization.

We save the component and give the necessary inputs as shown below:

We drag the component into our work area and set the variable values.

    

When we run the analysis after selecting the variables, we get the following error saying "could not find function".

The reason that we get below error is because the qplot function is part of ggplot2 package. Hence when using a function belonging to a package we always need to have a reference to that package. In many cases the package may not be installed on our machine and we may need to install it from the CRAN server.

STEP2: Creating a Custom R component to install and refer a package.

We will now create a component to get rid of the above error. The component that we will create will allow us to install any package and give reference to it. You can modify the code and refer to multiple packages. In this case we will see how to install ggplot2 package so that we can use the qplot function.

Again go to insert component and insert the following code. The comments in the code explain what that piece of code is doing.

Now we insert the above component in between the dataset and the qplot component that we created and again run the analysis.

At times it may ask you to select a mirror to install the package. Select the one nearest to you. We see that after referring the package the analysis works fine.

STEP3: Using print() function  to plot a graph.

However when we goto visualize/charts we do not see any graph or plot there. Although we had selected show visualization in step 1, we are not getting any visualization. This is because qplot is a lattice function. Lattice functions create a graph object,but do not display it. The print() method for the graph object produces the actual display.  When we use these functions interactively at the command line, the result is automatically printed,but inside our script to create a component we need an explicit print() statement. Whenever we need to plot a graph of lattice functions we need to use print() in our script. Hence we go and modify the QPLOT script as below and rerun the analysis.

On completing the above steps we get the desired output as shown below:

PS: While installing packages please make sure that the R version you are using supports that package else you may not get desired results.

3 Comments
Labels in this area