R Shiny Server connection with Cassandra on Google Maps

It was a project involving R and Cassandra to represent the customers on Google Maps.

 

================================================================

--// Full Code

================================================================

library(shiny)

library(RODBC)

library(RCassandra)

library(googleVis)

library(dplyr)    

library(magrittr)

 

# getMapData(testConnection)

getMapData <- function(connection){

 

  #RC.read.table(connection, c.family, convert = TRUE, na.strings = "select Latitude, Lon, suburb, typeOfSale, category from tbReckonGoogle", as.is = FALSE, dec = ".")

  RC.read.table(connection, "tbReckonGoogle")

}

 

  #   sqlQuery(connection, "select Distinct

  #                               (convert(varchar,Lat) +','+ convert(varchar,Lon)) as Latitude,

  #                               Lon,

  #                               suburb,

  #                               rd2.vchParameterDesc as typeOfSale,

  #                               rd1.vchParameterDesc as category 

  #                           from Onyx.dbo.zOnyxCustomerView c

  #                           inner join tbNZPostcode p on c.vchPostCode = p.postcode and charindex(replace(dbo.fnAddressName(vchAddress1),'Box', 'Queen'), street)>0  

  #                           inner join vCustomerproduct CP on CP.iOwnerId = c.iOnyxId

  #                           inner join ProductMaster PM ON PM.chProductNumber = CP.chProductNumber

  #                           inner join vReferenceDefinition rd1 ON rd1.iParameterId = PM.vchUser1

  #                           inner join vReferenceDefinition rd2 ON rd2.iParameterId = PM.vchUser2                         

  #                           where chCountryCode = 'NZ'

  #                                and vchCity = 'AUCKLAND'

  #                                and Lon <> '174.947443'

  #                                and subUrb in ('Auckland Central', 'Epsom', 'Pakuranga', 'Remuera')

  #                                and rd1.vchParameterDesc in ('Quicken', 'Quickbooks')

  #                                and rd2.vchParameterDesc in ('Full Version', 'Trial', 'Upgrade')  ")

#}

 

 

# filterMapData(gps, "Epsom", "All", "All")

# filterMapData(gps, "Remuera", "QuickBooks", "Full Version")

# filterMapData(gps, "All", "All", "All")

filterMapData <- function (gpsData, gpsSuburb, gpsCategory, gpsTypeOfSale) {

 

  gpsData %>%

    filter((gpsSuburb == suburb | gpsSuburb == "All") &

             (gpsCategory == category | gpsCategory == "All") &

             (gpsTypeOfSale == typeOfSale | gpsTypeOfSale == "All"))

}

 

# sampleMapData(gps, 10)

sampleMapData <- function (gpsData, topCount) {

 

  maxTopCount <- nrow(gpsData)

  topCount <- min(topCount, maxTopCount)

 

  gpsData %>%

    sample_n(topCount)

}

 

# drawMapData(gps) 

drawMapData <- function (gpsData) {

 

  gpsData %>%

    gvisMap(options=list(showTip=TRUE,

                         showLine=TRUE,

                         enableScrollWheel=TRUE,

                         mapType='normal',

                         useMapTypeControl=TRUE,

                         width=400,

                         height=1000))

}

 

# createMap(gps, "Epsom", "All", "All", 10)

createMap <- function (mapData, gpsSuburb, gpsCategory, gpsTypeOfSale, topCount) {

  mapData %>%

    filterMapData(gpsSuburb, gpsCategory, gpsTypeOfSale) %>%

    sampleMapData(topCount) %>%

    drawMapData

}

 

# materialise the GPS map data from Onyx

#connection <- odbcConnect("STAONYX", uid="usr_studioR", pwd="******")

#connection <- odbcConnect("Cassandra", uid="usr_studioR", pwd="******")

#connection <- odbcConnect("10.64.12.248")

#connection <- RC.connect(host = "10.64.12.40", port = 9042)

connection <- RC.connect("10.64.12.40")

RC.use(connection, "onyxdb")

 

gps <- getMapData(connection)

 

shinyServer(function(input, output) {

 

  output$map <- renderGvis({

   

                gps %>%

                    createMap(input$neighbours,input$Category, input$TypeOfSale, input$bins)

 

  })

 

})