Skip to content

Commit bc8c07e

Browse files
committed
R: Extend example to demonstrate both RPostgres and RPostgreSQL
1 parent 93b871c commit bc8c07e

File tree

5 files changed

+92
-41
lines changed

5 files changed

+92
-41
lines changed

by-language/r/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
install:
22

33
test:
4-
Rscript basic.r
4+
Rscript basic_rpostgres.r
5+
Rscript basic_rpostgresql.r

by-language/r/README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
## About
44

5-
The file `basic.r` includes a basic example program that uses the R
6-
[RPostgreSQL] package, the canonical database interface and 'PostgreSQL'
7-
driver for 'R', to connect to CrateDB.
5+
The files `basic_rpostgres.r` and `basic_rpostgresql.r` include basic
6+
example programs that use the R packages [RPostgres] resp. [RPostgreSQL],
7+
the canonical database interfaces and 'PostgreSQL' drivers for 'R',
8+
to connect to CrateDB.
89

910
## Usage
1011

@@ -13,9 +14,10 @@ Start a CrateDB instance for evaluation purposes.
1314
docker run -it --rm --publish=4200:4200 --publish=5432:5432 crate:latest
1415
```
1516

16-
Invoke example program.
17+
Invoke example programs.
1718
```shell
18-
Rscript basic.r
19+
Rscript basic_rpostgres.r
20+
Rscript basic_rpostgresql.r
1921
```
2022

2123
Invoke software tests.
@@ -24,4 +26,5 @@ make test
2426
```
2527

2628

29+
[RPostgres]: https://cran.r-project.org/web/packages/RPostgres/
2730
[RPostgreSQL]: https://cran.r-project.org/web/packages/RPostgreSQL/

by-language/r/basic.r

Lines changed: 0 additions & 35 deletions
This file was deleted.

by-language/r/basic_rpostgres.r

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Install driver on demand.
2+
# RPostgres: C++ Interface to PostgreSQL
3+
# https://cran.r-project.org/web/packages/RPostgres/
4+
5+
# Optionally install the PostgreSQL library.
6+
if (!requireNamespace("RPostgres", quietly = TRUE)) {
7+
install.packages("RPostgres", repos="https://cran.r-project.org")
8+
}
9+
10+
# Load the DBI and PostgreSQL libraries.
11+
library(DBI)
12+
library(RPostgres)
13+
drv <- Postgres()
14+
15+
# Open a database connection, where `dbname` is the name of the CrateDB schema.
16+
conn <- dbConnect(drv,
17+
host = "localhost",
18+
port = 5432,
19+
sslmode = "disable",
20+
user = "crate",
21+
password = "crate",
22+
dbname = "doc",
23+
)
24+
on.exit(DBI::dbDisconnect(conn), add = TRUE)
25+
26+
# Invoke a basic select query.
27+
res <- dbGetQuery(conn, "SELECT mountain, region, height FROM sys.summits ORDER BY height DESC LIMIT 10;")
28+
print(res)
29+
30+
# Delete testdrive table when needed.
31+
if (dbExistsTable(conn, "r")) {
32+
dbRemoveTable(conn, "r")
33+
}
34+
35+
# Basic I/O.
36+
res <- dbSendQuery(conn, "CREATE TABLE r (id INT PRIMARY KEY, data TEXT);")
37+
dbClearResult(res)
38+
res <- dbSendQuery(conn, "INSERT INTO r (id, data) VALUES (42, 'foobar');")
39+
dbClearResult(res)
40+
res <- dbSendQuery(conn, "REFRESH TABLE r;")
41+
dbClearResult(res)
42+
res <- dbGetQuery(conn, "SELECT * FROM r;")
43+
print(res)

by-language/r/basic_rpostgresql.r

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Install driver on demand.
2+
# RPostgreSQL: R Interface to the 'PostgreSQL' Database System
3+
# https://cran.r-project.org/web/packages/RPostgreSQL/
4+
5+
# Optionally install the PostgreSQL library.
6+
if (!requireNamespace("RPostgreSQL", quietly = TRUE)) {
7+
install.packages("RPostgreSQL", repos="https://cran.r-project.org")
8+
}
9+
10+
# Load the DBI and PostgreSQL libraries.
11+
library(DBI)
12+
library(RPostgreSQL)
13+
drv <- RPostgreSQL::PostgreSQL()
14+
15+
# Open a database connection, where `dbname` is the name of the CrateDB schema.
16+
conn <- dbConnect(drv,
17+
host = "localhost",
18+
port = 5432,
19+
user = "crate",
20+
password = "crate",
21+
dbname = "doc",
22+
)
23+
on.exit(DBI::dbDisconnect(conn), add = TRUE)
24+
25+
# Invoke a basic select query.
26+
res <- dbGetQuery(conn, "SELECT mountain, region, height FROM sys.summits ORDER BY height DESC LIMIT 10;")
27+
print(res)
28+
29+
# Delete testdrive table when needed.
30+
if (dbExistsTable(conn, "r")) {
31+
dbRemoveTable(conn, "r")
32+
}
33+
34+
# Basic I/O.
35+
res <- dbGetQuery(conn, "CREATE TABLE IF NOT EXISTS r (id INT PRIMARY KEY, data TEXT);")
36+
res <- dbGetQuery(conn, "INSERT INTO r (id, data) VALUES (42, 'foobar');")
37+
res <- dbGetQuery(conn, "REFRESH TABLE r;")
38+
res <- dbGetQuery(conn, "SELECT * FROM r;")
39+
print(res)

0 commit comments

Comments
 (0)