Custom R Component: Google Maps
This component adds static Google Maps to SAP Predictive Analysis. The user can chose through parameters what locations should be displayed, their color coding, the map’s zoom level and the region the map should focus on.
Background
Please scroll to the bottom of this page for instructions on implementing this function (R Code and Configuration). In the next chapter you will also find a sample dataset. The file contains a small part of a perception survey done by Eurostat in 2009 about the quality of life in 75 different European cities. The cities were clustered with this component and later enriched with geocoordinates. Even though the clustering did not know anything about the location of the cities, it is striking how the clusters are clearly visible on the geographical map.
Thank you very much to our R-Expert Pramila (Pramilamma Bovilla) for adding the setInternet2() function and the destfile parameter to make this script work in SAP Predictive Analysis!
If you are new to creating Custom R Components in SAP Predictive Analysis, you can have a look at this overview to get you started. Please note that this code is not supported by SAP. When using this function please carry out your own testing.
Usage
Load the dataset EuropeanUrbanAudit_WithClusterAndGeocodes.csv into SAP Predictive Analysis.
Next add the new “Google Maps” component to your model.
Configure the component. You need to set the following:
- The geographic region for the map to center on.
- Zoom Level.
- A numerical column that controls the color coding (for instance a cluster number).
- Whether the places should be displayed by dots or with their names.
- The column that holds the place names (only used if the places are to be displayed by name).
- Columns holding the latitude and longitude coordinates.
Run the model and click on ‘Charts’. You can see how the clusters, that were created without any knowledge about their geographic location, are very much located together on the map! The perception of quality of life clearly has a strong relationship to the location.
If you want to know more the structure of these clusters, you can look at them in the Hierarchical Clustering component.
How to Implement
The component can be downloaded as .spar file from GitHub. Then deploy it as described here. You just need to import it through the option “Import/Model Component”, which you will find by clicking on the plus-sign at the bottom of the list of the available algorithms.
This is awesome!
This looks great! Thank you Andreas,
This is very help full and looks great!!! thanks a lot Andreas.
This looks good.
But when i tried this, I encounter the following error:
A critical error occurred:
An error occurred while executing commands in the R environment. Error from R:
"Error in download.file(url, destfile, mode = "wb", quiet = TRUE) :
cannot open URL 'http://maps.google.com/maps/api/staticmap?center=NA,NA&zoom=4&size=640x640&maptype=mobile&format=png32&sensor=true'
"
Is there some kind of API key required?
Hello Wei,
There is no further key required or anything special to make it work. Could it be that a firewall is blocking access to that URL?
Can you view the map when entering the address directly into your browser?
http://maps.google.com/maps/api/staticmap?center=NA,NA&zoom=4&size=640x640&maptype=mobile&format=png32&sensor=true
If the map shows ok in the web browser, please have a look at your system variable TMP. Which folder is this pointing to?
Please make sure you have write access to that folder.
Greetings
Andreas
Getting an error, any pointers ?
I have installed RODBC, have also tried
install.packages("RODBC", dependencies=TRUE)
Hi Vivek,
One thing that I sometimes comes across is that R GUI isn't always installing packages (version conflicts with packages etc.), however using RStudio can perhaps help identify the problem.
As an example installing the RODBC package in R 2.15:
However trying the exact same statement in RStudio:
What happens when you try to call the library?
library(RODBC)
Best regards,
Kurt Holst
Hi Kurt,
Thank you for quick response to this
1. I installed the R STUDIO (V 0.98.501) & ran the install commands ( for RgoogleMaps & RODBC) to get the same results as above
> install.packages("RgoogleMaps")
trying URL 'http://cran.rstudio.com/bin/windows/contrib/3.1/RgoogleMaps_1.2.0.5.zip'
Content type 'application/zip' length 468276 bytes (457 Kb)
opened URL
downloaded 457 Kb
package ‘RgoogleMaps’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\vshukla\AppData\Local\Temp\RtmpC244Xf\downloaded_packages
> install.packages("RODBC", dependencies=TRUE)
trying URL 'http://cran.rstudio.com/bin/windows/contrib/3.1/RODBC_1.3-10.zip'
Content type 'application/zip' length 828896 bytes (809 Kb)
opened URL
downloaded 809 Kb
package ‘RODBC’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\vshukla\AppData\Local\Temp\RtmpC244Xf\downloaded_packages
>
However I still get the same error
2. I re-ran the package installation in R(version 3.1.0) (again for both RgoogleMaps & RODBC)
> utils:::menuInstallPkgs()
--- Please select a CRAN mirror for use in this session ---
trying URL 'http://cran.utstat.utoronto.ca/bin/windows/contrib/3.1/RgoogleMaps_1.2.0.5.zip'
Content type 'application/zip' length 468276 bytes (457 Kb)
opened URL
do
package ‘RgoogleMaps’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\vshukla\AppData\Local\Temp\RtmpWUmaK0\downloaded_packages
> utils:::menuInstallPkgs()
trying URL 'http://cran.utstat.utoronto.ca/bin/windows/contrib/3.1/RODBC_1.3-10.zip'
Content type 'application/zip' length 828896 bytes (809 Kb)
opened URL
downloaded 809 Kb
package ‘RODBC’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\vshukla\AppData\Local\Temp\RtmpWUmaK0\downloaded_packages
>
Again same error
Not sure if I am missing something here
Thanks,
Vivek
Hi Vivek,
Thanks for testing the proposal.
I have seen other instances where the R packages were cluttered up. A way to clean up this might be solved by running this R script in either R GUI or your RStudio:
# Script that removes installed.packages:
setInternet2(TRUE)
rhomedir<-Sys.getenv("R_HOME")
libdir<-paste(rhomedir,"/library",sep="")
local({r <- getOption("repos")
r["CRAN"] <- "http://cran.r-project.org"
options(repos=r)})
pkginstall <- function(x) {
if (!x %in% installed.packages())
{
install.packages(x)
}
else
{
remove.packages(x)
install.packages(x)
}
}
# Script that re-installs needed packages:
pkginstall("rJava")
pkginstall("RODBC")
pkginstall("RJDBC")
pkginstall("DBI")
pkginstall("monmlp")
pkginstall("AMORE")
pkginstall("XML")
pkginstall("pmml")
pkginstall("arules")
pkginstall("caret")
pkginstall("reshape")
pkginstall("plyr")
pkginstall("foreach")
pkginstall("iterators")
pkginstall("corrgram")
pkginstall("e1071")
pkginstall("corrgram")
pkginstall("e1071")
pkginstall("twitteR")
pkginstall("ROAuth")
pkginstall("plyr")
pkginstall("stringr")
pkginstall("ggplot2")
pkginstall("RgoogleMaps")
pkginstall("png")
# Calling the various libraries to check everything is ok:
library(caret)
library(pmml)
library(rJava)
library(RODBC)
library(RJDBC)
library(iterators)
library(arules)
library(AMORE)
library(reshape)
library(corrgram)
library(e1071)
library(twitteR)
library(ROAuth)
library(plyr)
library(stringr)
library(ggplot2)
library(RgoogleMaps)
library(png)
If this also fails I suggest you uninstall R and deletes the R folders.
Best regards,
Kurt Holst
Hi Kurt,
I tried part 1, deleting & refreshing the packages, but it did not have any impact on the error
Again I re-installed R -Studio & R, then further installed all the packages you mentioned in both env.
However I seem to bump into a different error
GoogleMaps: REngine initialization failed
Any hints on how i can solve this
Appreciate your time & help on this
Thanks,
Vivek
Hi Vivek,
thanks for testing the proposals.
Please let us have a direct call to sort this out.
If you send me your contact information & I will setup a conference call.
Best regards, Kurt
I will connect with you on this
Thanks,
Vivek