Package postgistools

Similar documents
Package dbx. July 5, 2018

Package ECctmc. May 1, 2018

Package fastdummies. January 8, 2018

Package rpostgislt. March 2, 2018

Package geojsonsf. R topics documented: January 11, Type Package Title GeoJSON to Simple Feature Converter Version 1.3.

Package condusco. November 8, 2017

Package cancensus. February 4, 2018

Package calpassapi. August 25, 2018

Package gtrendsr. August 4, 2018

Package rbgm. May 18, 2018

Package validara. October 19, 2017

Package cattonum. R topics documented: May 2, Type Package Version Title Encode Categorical Features

Package gtrendsr. October 19, 2017

Package robotstxt. November 12, 2017

Package qualmap. R topics documented: September 12, Type Package

Package rsppfp. November 20, 2018

Package nngeo. September 29, 2018

Package SEMrushR. November 3, 2018

Package gggenes. R topics documented: November 7, Title Draw Gene Arrow Maps in 'ggplot2' Version 0.3.2

Package SNPediaR. April 9, 2019

Package bisect. April 16, 2018

Package RPostgres. April 6, 2018

Package goodpractice

Package messaging. May 27, 2018

Package docxtools. July 6, 2018

Package fitbitscraper

Package taxizedb. June 21, 2017

Package RODBCext. July 31, 2017

Package waver. January 29, 2018

Package RPresto. July 13, 2017

Package jpmesh. December 4, 2017

Package readxl. April 18, 2017

Package RPostgres. December 6, 2017

Package snakecase. R topics documented: March 25, Version Date Title Convert Strings into any Case

Package clipr. June 23, 2018

Package ggimage. R topics documented: November 1, Title Use Image in 'ggplot2' Version 0.0.7

Package dkanr. July 12, 2018

Package TrajDataMining

Package censusapi. August 19, 2018

Package canvasxpress

Package strat. November 23, 2016

Package geogrid. August 19, 2018

Package mdftracks. February 6, 2017

Package internetarchive

Package rnaturalearth

Package rgho. R topics documented: January 18, 2017

Package glue. March 12, 2019

Package censusr. R topics documented: June 14, Type Package Title Collect Data from the Census API Version 0.0.

Package fingertipsr. May 25, Type Package Version Title Fingertips Data for Public Health

Package implyr. May 17, 2018

Package TrafficBDE. March 1, 2018

Package opencage. January 16, 2018

Package ggimage. R topics documented: December 5, Title Use Image in 'ggplot2' Version 0.1.0

Package MonetDBLite. January 14, 2018

Package jdx. R topics documented: January 9, Type Package Title 'Java' Data Exchange for 'R' and 'rjava'

Package knitrprogressbar

Package githubinstall

Package patentsview. July 12, 2017

Package pdfsearch. July 10, 2018

Package loggit. April 9, 2018

Package cregulome. September 13, 2018

Package rwars. January 14, 2017

Package smapr. October 20, 2017

Package liftr. R topics documented: May 14, Type Package

Package mapr. April 11, 2017

Package shinyfeedback

Package rprojroot. January 3, Title Finding Files in Project Subdirectories Version 1.3-2

Package lumberjack. R topics documented: July 20, 2018

Package redux. May 31, 2018

Package NFP. November 21, 2016

Package available. November 17, 2017

Package tibble. August 22, 2017

Package tidytransit. March 4, 2019

Package phrasemachine

Package datasets.load

Package barcoder. October 26, 2018

Package repec. August 31, 2018

Package BiocManager. November 13, 2018

Package mapr. March 21, 2018

Package geoops. March 19, 2018

Package splithalf. March 17, 2018

Package facerec. May 14, 2018

Package redlistr. May 11, 2018

Package edfreader. R topics documented: May 21, 2017

Package rzeit2. January 7, 2019

Package nlgeocoder. October 8, 2018

Package wikitaxa. December 21, 2017

Package eply. April 6, 2018

Package deductive. June 2, 2017

Package keyholder. May 19, 2018

Package gifti. February 1, 2018

Package virustotal. May 1, 2017

Package MonetDB.R. March 21, 2016

Package wrswor. R topics documented: February 2, Type Package

Package modules. July 22, 2017

Package aws.transcribe

Package statsdk. September 30, 2017

Package kdtools. April 26, 2018

Package data.world. April 5, 2018

Package bigqueryr. October 23, 2017

Transcription:

Type Package Package postgistools March 28, 2018 Title Tools for Interacting with 'PostgreSQL' / 'PostGIS' Databases Functions to convert geometry and 'hstore' data types from 'PostgreSQL' into standard R objects, as well as to simplify the import of R data frames (including spatial data frames) into 'PostgreSQL'. Note: This package is deprecated. For new projects, we recommend using the 'sf' package to interface with geodatabases. Version 0.2.2 License GPL-3 URL https://github.com/sesync-ci/postgistools BugReports https://github.com/sesync-ci/postgistools/issues Depends R (>= 3.2.3), sp (>= 1.2) Imports DBI (>= 0.5), jsonlite (>= 0.9), methods, rgdal (>= 0.8), rgeos (>= 0.3), RPostgreSQL (>= 0.4), stringr (>= 1.0) Suggests testthat, knitr, rmarkdown LazyData TRUE RoxygenNote 6.0.1 VignetteBuilder knitr NeedsCompilation no Author Philippe Marchand [aut, cre], Richard Ellison [aut] Maintainer Philippe Marchand <marchand.philippe@gmail.com> Repository CRAN Date/Publication 2018-03-28 11:22:42 UTC R topics documented: postgistools-package.................................... 2 get_postgis_query...................................... 2 1

2 get_postgis_query new_hstore......................................... 3 postgis_insert........................................ 4 %->%............................................ 6 Index 8 postgistools-package postgistools: Tools for interacting with PostgreSQL / PostGIS Databases postgistools facilitates the import/export of data tables between R and PostgreSQL, in particular those with associated geometries (PostGIS extension) and hstore (key-value pairs) type columns. Key Functions get_postgis_query works like dbgetquery, with the additional benefit of parsing hstore types (as a list-column in the resulting R data frame) and geometry types (producing a spatial data frame in R). %->% reproduces the behavior of the PostgreSQL hstore -> key operator. postgis_insert and postgis_update respectively insert new rows or update existing rows in a PostgreSQL table based on the contents of a R data frame. For spatial data and hstore columns, they performs the same conversions as get_postgis_query, in reverse. get_postgis_query Send SELECT query and parse geometry, hstore columns Usage This function is an extension of dbgetquery that is useful in cases where selected columns include a PostgreSQL hstore, which is parsed as a list-column, and/or a PostGIS geometry, in which case the output is a spatial data frame (from the sp package). get_postgis_query(conn, statement, geom_name = NA_character_, hstore_name = NA_character_) Arguments conn statement geom_name hstore_name A PostgreSQLConnection-class object, such as the output of dbconnect. Character string for a SQL SELECT query. Name of the geometry column (NA if none). Name of the hstore column (NA if none).

new_hstore 3 Details Conversion to spatial data frame objects will fail if there are NULL values in the geometry column, so these should be filtered out in the provided query statement. Value Either a data frame (if geom_name = NA) or a Spatial[Points/Lines/Polygons]DataFrame containing the query result. If a hstore column is present, it appears as a list-column in the data frame, i.e. each cell is a named list of key-value pairs. References The code for importing geom fields is based on a blog post by Lee Hachadoorian: Load PostGIS geometries in R without rgdal. See Also The %->% operator for working with hstore columns; postgis_insert and postgis_update for writing to a PostgreSQL connection. Examples ## Not run: library(rpostgresql) con <- dbconnect(postgresql(), dbname = "my_db") # If geom column holds points, returns a SpatialPointsDataFrame cities <- get_postgis_query(con, "SELECT name, geom, datalist FROM city", geom_name = "geom", hstore_name = "datalist") # Get the populations (part of datalist hstore) as a vector pop <- cities@data$datalist %->% "population" ## End(Not run) new_hstore Create a empty hstore This function creates an empty list of lists, which can be appended to a data frame as a hstore column. Usage new_hstore(nr)

4 postgis_insert Arguments nr Number of records. Value See Also A empty hstore (list of lists) of length nr. %->% to read or edit the resulting data structure. Examples contacts <- data.frame(name = c("anne", "Bert", "Chris")) contacts$phone <- new_hstore(3) contacts$phone %->% "home" <- c("555-123-4567", "555-923-9134", "555-276-1123") contacts$phone[2] %->% "cell" <- "555-889-9134" str(contacts) postgis_insert Insert or update records in a PostgreSQL table from a R data frame. Usage These functions produce INSERT (postgis_insert) or UPDATE (postgis_update) queries to write data from a R data frame to a PostgreSQL table, with options to include a geometry layer and a list-column of key-value pairs (as a PostgreSQL hstore). The queries are passed to the database with dbsendquery. postgis_insert(conn, df, tbl, write_cols = NA, geom_name = NA_character_, hstore_name = NA_character_) postgis_update(conn, df, tbl, id_cols, update_cols, geom_name = NA_character_, hstore_name = NA_character_, hstore_concat = TRUE) Arguments conn df tbl write_cols geom_name A PostgreSQLConnection-class object, such as the output of dbconnect. A data frame (if geom_name = NA) or Spatial[Points/Lines/Polygons]DataFrame. Name of the PostgreSQL table to write to. A character vector, corresponding to the columns in df to insert in the database table. If NA, inserts all columns. Name of the geometry column in the database table (NA if none).

postgis_insert 5 hstore_name id_cols update_cols hstore_concat Name of the hstore column in both df and the database table (NA if none). A character vector, corresponding to the columns in df used to match records between df and the database table. A character vector, corresponding to the columns that must be updated in the database table based on values in df. If TRUE, hstore columns are updated by concatenation. Details Value All column names used in the query must match between the input data frame and the target database table (except for geom_name which only applies to the table). postgis_update creates an UPDATE... SET... FROM... query, which effectively joins the original table and input data frame based on matching values in id_cols, then updates the values in update_cols. The combination of id_cols must be unique in df, but they can be duplicated in the database table, in which case multiple rows are updated from a single row in df. Neither the geometry nor the hstore column can be used in id_cols. Note that if hstore_concat = TRUE (the default), hstore columns are updated by concatenation, i.e. new keys are added, values associated with existing keys are updated, no keys are deleted. To overwrite whole hstore "cells", potentially deleting keys absent in df, set hstore_concat = FALSE. The result of dbsendquery. See Also get_postgis_query for the inverse operation (read from database to R). Examples ## Not run: library(rpostgresql) con <- dbconnect(postgresql(), dbname = "my_db") # Returns a SpatialPointsDataFrame cities <- get_postgis_query(con, "SELECT name, geom, datalist FROM city", geom_name = "geom", hstore_name = "datalist") # Create a new field in hstore and update DB cities@data$datalist %->% "pop_density" <- cities@data$datalist %->% "population" / cities@data$datalist %->% "area" postgis_update(con, cities, "city", id_cols = "name", update_cols = "datalist", geom_name = "geom", hstore_name = "datalist") # Add rows to DB with postgis_insert # (new_cities is a SpatialPointsDataFrame with same columns as cities) postgis_insert(con, new_cities, "city", geom_name = "geom", hstore_name = "datalist")

6 %->% ## End(Not run) %->% Extract or replace hstore values by key Operator to get or set values corresponding to a given key for all records in a hstore. Usage hstore %->% key hstore %->% key <- value Arguments hstore key value A hstore (i.e. list of lists). Character string corresponding to a key in hstore. Vector of values of the same length as hstore. Details Based on the hstore "->" operator in PostgreSQL, the %->% operator returns values associated with a given key for all records in the hstore. The assignment version of the operator i.e. hstore %->% key <- value either creates a new key-value pair or, if they key exists, update the associated value. It can also delete a key by assigning its value to NULL. Note that to subset the records in hstore to which the operator applies, you must use single brackets so that the result remains a list of lists. See below for usage examples. Value For the extract version, a vector of the same length as hstore, containing the value corresponding to key for each record (or NA if none). For the replace version, the modified hstore. See Also new_hstore to create a empty hstore.

%->% 7 Examples contacts <- data.frame(name = c("anne", "Bert", "Chris")) contacts$phone <- new_hstore(3) contacts$phone %->% "home" <- c("555-123-4567", "555-923-9134", "555-276-1123") contacts$phone[2:3] %->% "home" contacts$phone[2] %->% "home" <- NULL contacts$phone %->% "home" contacts$phone[2] %->% "cell" <- "555-889-9134" contacts$phone %->% "cell"

Index %->%<- (%->%), 6 %->%, 6 dbconnect, 2, 4 dbgetquery, 2 dbsendquery, 4, 5 get_postgis_query, 2, 2, 5 new_hstore, 3, 6 postgis_insert, 2, 3, 4 postgis_update, 2, 3 postgis_update (postgis_insert), 4 postgistools (postgistools-package), 2 postgistools-package, 2 sp, 2 8