Guest User

Untitled

a guest
Jan 13th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. library(processx)
  2. library(RPostgres)
  3. library(httr)
  4. library(dbplyr)
  5. library(tidyverse)
  6.  
  7. # this example assumes you've created a heroku postgresql
  8. # instance and have the app name (in this example, "rpgtestcon").
  9.  
  10. # use the heroku command-line app
  11. # we do this as the creds change & it avoids disclosure
  12. config <- run("heroku", c("config:get", "DATABASE_URL", "-a", "rpgtestcon"))
  13.  
  14. # ^^ gets us a URL, we need the parts
  15. pg <- httr::parse_url(config$stdout)
  16.  
  17. # use the parts from ^^
  18. dbConnect(RPostgres::Postgres(),
  19. dbname = trimws(pg$path),
  20. host = pg$hostname,
  21. port = pg$port,
  22. user = pg$username,
  23. password = pg$password,
  24. sslmode = "require"
  25. ) -> db_con
  26.  
  27. # hook it up to dbplyr
  28. db <- src_dbi(db_con)
  29.  
  30. # boom
  31. db
  32. ## src: PqConnection
  33. ## tbls:
  34.  
  35. xdf <- copy_to(db, iris, name="iris", overwrite = TRUE)
  36.  
  37. db
  38. ## src: PqConnection
  39. ## tbls: iris
  40.  
  41. xdf
  42. ## # Source: table<iris> [?? x 5]
  43. ## # Database: PqConnection
  44. ## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
  45. ## <dbl> <dbl> <dbl> <dbl> <chr>
  46. ## 1 5.1 3.5 1.4 0.2 setosa
  47. ## 2 4.9 3.0 1.4 0.2 setosa
  48. ## 3 4.7 3.2 1.3 0.2 setosa
  49. ## 4 4.6 3.1 1.5 0.2 setosa
  50. ## 5 5.0 3.6 1.4 0.2 setosa
  51. ## 6 5.4 3.9 1.7 0.4 setosa
  52. ## 7 4.6 3.4 1.4 0.3 setosa
  53. ## 8 5.0 3.4 1.5 0.2 setosa
  54. ## 9 4.4 2.9 1.4 0.2 setosa
  55. ## 10 4.9 3.1 1.5 0.1 setosa
  56. ## # ... with more rows
Add Comment
Please, Sign In to add comment