Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #
  2. # In this global.R field, we create all objects and run all code
  3. # that we only want to run once when we start our app and that we
  4. # want accessible to all parts of our app. This includes database
  5. # queries (in this case at least), API keys, etc.
  6. #
  7. # First, we load and attach packages. A package must be installed before
  8. # loading; for example, to install RPostgreSQL, we simply type
  9. # install.packages("RPostgreSQL").
  10. #
  11. # RPostgreSQL allows us to connect to our database.
  12. library(RPostgreSQL)
  13.  
  14. # sf (Simple Features) is our spatial package of choice. It uses Simple
  15. # Features lists to store geometries. SF is a standard storage model
  16. # maintained by the Open Geospatial Consortium (OGS) and the
  17. # International Organization for Standardization (ISO), so it's like
  18. # reaaaal offish. Incidentally, PostGIS also implements Simple Features,
  19. # so SF plays very nicely with SF.
  20. library(sf)
  21.  
  22. # Shiny is a web application Framework for R that provides both
  23. # server-side scripting and a browser user interface API.
  24. library(shiny)
  25.  
  26. # Mapdeck is an R interface for MapboxGL and Deck.gl; this is the library
  27. # that we'll be using to build our map.
  28. library(mapdeck)
  29.  
  30. # Here, we create and initialize a PostgreSQL client.
  31. # We could also write driver <- dbDriver('PostgreSQL')
  32. # I discovered digging into the docs that dbDriver methods are deprecated,
  33. # meaning that users are encouraged to instead use database-specific
  34. # functions to initialize drivers.
  35. driver <- PostgreSQL()
  36.  
  37. # Here, we initiate a connection to our database using parameters that
  38. # should by now look pretty familiar!
  39. con <- dbConnect(driver,
  40. dbname='class521_s19',
  41. host='cronpgsql.mit.edu',
  42. user='',
  43. password='')
  44.  
  45. # Storing a SQL query as a character string.
  46. evic_rate_q <- "SELECT
  47. t.geoid10,
  48. t.geom,
  49. e.eviction_rate,
  50. e.year
  51. FROM
  52. somerville.gb_bg_2010 AS t
  53. INNER JOIN somerville.evic_bg AS e ON t.geoid10 = e.geoid10;"
  54.  
  55.  
  56. # st_read comes from the SF package, and allows us to read a file or
  57. # query a database whose results are stored as a data frame with a
  58. # simple feature column. The ST_ prefix should look familiar from our
  59. # PostGIS work! It stands for Spatial Type.
  60. evic_rate <- st_read(dsn = con, query = evic_rate_q)
  61.  
  62. # We now disconnect our database instance to ensure that we don't open
  63. # multiple unnecessary connections to our database (this is especially
  64. # relevant in development, as you'll likely be stopping and starting
  65. # your application frequently).
  66. dbDisconnect(con)
  67.  
  68. # This is your Mapbox API key (or 'token'), available on the dashboard of your account.
  69. mb_token = ''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement