Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # In this global.R field, we create all objects and run all code
- # that we only want to run once when we start our app and that we
- # want accessible to all parts of our app. This includes database
- # queries (in this case at least), API keys, etc.
- #
- # First, we load and attach packages. A package must be installed before
- # loading; for example, to install RPostgreSQL, we simply type
- # install.packages("RPostgreSQL").
- #
- # RPostgreSQL allows us to connect to our database.
- library(RPostgreSQL)
- # sf (Simple Features) is our spatial package of choice. It uses Simple
- # Features lists to store geometries. SF is a standard storage model
- # maintained by the Open Geospatial Consortium (OGS) and the
- # International Organization for Standardization (ISO), so it's like
- # reaaaal offish. Incidentally, PostGIS also implements Simple Features,
- # so SF plays very nicely with SF.
- library(sf)
- # Shiny is a web application Framework for R that provides both
- # server-side scripting and a browser user interface API.
- library(shiny)
- # Mapdeck is an R interface for MapboxGL and Deck.gl; this is the library
- # that we'll be using to build our map.
- library(mapdeck)
- # Here, we create and initialize a PostgreSQL client.
- # We could also write driver <- dbDriver('PostgreSQL')
- # I discovered digging into the docs that dbDriver methods are deprecated,
- # meaning that users are encouraged to instead use database-specific
- # functions to initialize drivers.
- driver <- PostgreSQL()
- # Here, we initiate a connection to our database using parameters that
- # should by now look pretty familiar!
- con <- dbConnect(driver,
- dbname='class521_s19',
- host='cronpgsql.mit.edu',
- user='',
- password='')
- # Storing a SQL query as a character string.
- evic_rate_q <- "SELECT
- t.geoid10,
- t.geom,
- e.eviction_rate,
- e.year
- FROM
- somerville.gb_bg_2010 AS t
- INNER JOIN somerville.evic_bg AS e ON t.geoid10 = e.geoid10;"
- # st_read comes from the SF package, and allows us to read a file or
- # query a database whose results are stored as a data frame with a
- # simple feature column. The ST_ prefix should look familiar from our
- # PostGIS work! It stands for Spatial Type.
- evic_rate <- st_read(dsn = con, query = evic_rate_q)
- # We now disconnect our database instance to ensure that we don't open
- # multiple unnecessary connections to our database (this is especially
- # relevant in development, as you'll likely be stopping and starting
- # your application frequently).
- dbDisconnect(con)
- # This is your Mapbox API key (or 'token'), available on the dashboard of your account.
- mb_token = ''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement