Access in-memory SQL database in R.
Source: https://cran.r-project.org/web/packages/RPostgres/readme/README.html
library(DBI)
library(RPostgres)
library(rstudioapi)
con <- dbConnect(Postgres(), host='localhost', port=askForSecret("port(e.g. 5432)"), dbname=askForSecret("database name"), user='postgres', password = rstudioapi::askForSecret("password"))
db_tables <- dbListTables(con)
sprintf("The database has %d tables.", length(db_tables))
## [1] "The database has 15 tables."
sprintf("The first table is `%s`.", db_tables[1])
## [1] "The first table is `age`."
dbReadTable(con, "age")
## agelvl agelvlt
## 1 A Less than 20
## 2 B 20-24
## 3 C 25-29
## 4 D 30-34
## 5 E 35-39
## 6 F 40-44
## 7 G 45-49
## 8 H 50-54
## 9 I 55-59
## 10 J 60-64
## 11 K 65 or more
## 12 Z Unspecified
query_1 <- dbSendQuery(con, "SELECT * FROM supervisor")
dbFetch(query_1)
## supertyp supertypt supervis supervist
## 1 1 Supervisor 2 2-SUPERVISOR OR MANAGER
## 2 2 Leader 6 6-LEADER
## 3 2 Leader 7 7-TEAM LEADER
## 4 3 Non-Supervisor 4 4-SUPERVISOR (CSRA)
## 5 3 Non-Supervisor 5 5-MANAGEMENT OFFICIAL (CSRA)
## 6 3 Non-Supervisor 8 8-ALL OTHER POSITIONS
## 7 4 Unspecified * *-UNSPECIFIED
dbClearResult(query_1)
dbDisconnect(con) # disconnect and exit