Netherlands: Software

Introductie van Micorosoft SQL Server 2016

Issue link:

Contents of this Issue


Page 130 of 212

118 C H A P T E R 6 | More analytics created in an earlier example, and googMap, which you create in a later step. R allows you to reference this argument in the definition without testing for its existence because you are not yet attempting to execute the function. The body of the function appears between the two braces. The first two lines use the library function to load the ggmap 1 and mapproj packages to ensure that the functions they provide are available and ready to use on your server. When you install MRO, a core set of packages and libraries is available for immediate use. A package is a collection of objects that can include code, data, or documentation that you use to extend base R, whereas a library is a directory containing packages. There are thousands of add-on packages contributed by the open-source community that you can download and use to build your analytical application. Next, the rxImport function loads your data into server memory as a data frame called ds. This step prepares the data for use in open-source R functions because these functions cannot run in-database. The variable p is a plot object consisting of two layers. The ggmap function produces a map from the object passed in as an argument, which we explain later in this section. The + operator adds another layer to the plot object. You can add as many layers as necessary by using this technique. The geom_point function creates a scatterplot of pickup_longitude on the x-axis and pickup_latitude on the y-axis from the in-memory data frame. The alpha, color, and size arguments set point transparency, point color, and point size, respectively. The final line of code assigns a tag, myplot, to the p variable and converts the object to a list data type, which is the return value of the custom function. Next you execute the geocoding call to get the map, call the custom function to send the map and combine it with your plot points, and render the results in the local context, as shown in Example 6-6. Example 6-6: Creating a custom function to plot spatial data library(ggmap) library(mapproj) gc <- geocode("Manhattan", source = "google") googMap <- get_googlemap(center = as.numeric(gc), zoom = 12, maptype = 'roadmap', color = 'color') myplots <- rxExec(mapPlot, inDataSource, googMap, timesToRun = 1) plot(myplots[[1]][["myplot"]]); The first two lines are calls to load ggmap and mapproj again, but this time in the local context. Then the geocode function takes a street address or a place name as its first argument and sets the source to google as the second argument. The other possible source is dsk, which is the Data Science Toolkit (, but this source tends to return results more slowly or timeout. Next, the get_googlemap function uses the latitude and longitude coordinates stored in the gc variable to set the center of the map that it downloads from Google. The zoom argument takes an integer value ranging from 3 (for continent) to 21 (for building) to indicate the level of detail to display in the map. You can set the color argument to either color or black and white. The resulting map is stored in the googMap variable that you pass to the mapPlot function. In the next line, the rxExec function executes the function specified as the first argument (the custom function mapPlot, in this case) on SQL Server, using the arguments passed as a list as subsequent arguments, until it encounters rxExec arguments such as timesToRun. The variable myplots stores the results of execution, which is a list containing one item called myplot. 1 D. Kahle and H. Wickham, "ggmap: Spatial Visualization with ggplot2," The R Journal no. 5(1): 144– 61,

Articles in this issue

Links on this page

Archives of this issue

view archives of Netherlands: Software - Introductie van Micorosoft SQL Server 2016