Skip to Content
Author's profile photo Former Member

Components to install package and plot graphs using package functions

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-rnnet-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:

1.PNG

2.PNG

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

3.PNG     4.PNG

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.

5.PNG

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.

6.PNG

7.PNG

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.

8.PNG9.PNG

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.

10.PNG

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

11.PNG

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

Assigned Tags

      3 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Henrique Pinto
      Henrique Pinto

      This is a very nice example.

      I'm not an R expert, so I was wondering: would it be possible to create some kind of try/catch enclosure in your R function and, in case an exception is thrown due to a missing package, to parse the error msg, get the missing package's name, install and try again?

      Best,

      Henrique.

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      Hi Henrique,

      We do have tryCatch() function available in R. However I have never used it in any of the work I have done, but I am sure we can catch the error and return a message with missing class name. I will give it a try soon and if it works will update the document here.

      Author's profile photo Nidhi Sawhney
      Nidhi Sawhney

      Thank you so much for the post otherwise would have missed seeing those beautiful graphs in PA without the "print".