Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion by-language/r/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
install:

test:
Rscript basic.r
Rscript basic_rpostgres.r
Rscript basic_rpostgresql.r
13 changes: 8 additions & 5 deletions by-language/r/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

## About

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

## Usage

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

Invoke example program.
Invoke example programs.
```shell
Rscript basic.r
Rscript basic_rpostgres.r
Rscript basic_rpostgresql.r
```

Invoke software tests.
Expand All @@ -24,4 +26,5 @@ make test
```


[RPostgres]: https://cran.r-project.org/web/packages/RPostgres/
[RPostgreSQL]: https://cran.r-project.org/web/packages/RPostgreSQL/
35 changes: 0 additions & 35 deletions by-language/r/basic.r

This file was deleted.

43 changes: 43 additions & 0 deletions by-language/r/basic_rpostgres.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Install driver on demand.
# RPostgres: C++ Interface to PostgreSQL
# https://cran.r-project.org/web/packages/RPostgres/

# Optionally install the PostgreSQL library.
if (!requireNamespace("RPostgres", quietly = TRUE)) {
install.packages("RPostgres", repos="https://cran.r-project.org")
}

# Load the DBI and PostgreSQL libraries.
library(DBI)
library(RPostgres)
drv <- Postgres()

# Open a database connection, where `dbname` is the name of the CrateDB schema.
conn <- dbConnect(drv,
host = "localhost",
port = 5432,
sslmode = "disable",
user = "crate",
password = "crate",
dbname = "doc",
)
on.exit(DBI::dbDisconnect(conn), add = TRUE)

# Invoke a basic select query.
res <- dbGetQuery(conn, "SELECT mountain, region, height FROM sys.summits ORDER BY height DESC LIMIT 10;")
print(res)

# Delete testdrive table when needed.
if (dbExistsTable(conn, "r")) {
dbRemoveTable(conn, "r")
}

# Basic I/O.
res <- dbSendQuery(conn, "CREATE TABLE r (id INT PRIMARY KEY, data TEXT);")
dbClearResult(res)
res <- dbSendQuery(conn, "INSERT INTO r (id, data) VALUES (42, 'foobar');")
dbClearResult(res)
res <- dbSendQuery(conn, "REFRESH TABLE r;")
dbClearResult(res)
res <- dbGetQuery(conn, "SELECT * FROM r;")
print(res)
39 changes: 39 additions & 0 deletions by-language/r/basic_rpostgresql.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Install driver on demand.
# RPostgreSQL: R Interface to the 'PostgreSQL' Database System
# https://cran.r-project.org/web/packages/RPostgreSQL/

# Optionally install the PostgreSQL library.
if (!requireNamespace("RPostgreSQL", quietly = TRUE)) {
install.packages("RPostgreSQL", repos="https://cran.r-project.org")
}

# Load the DBI and PostgreSQL libraries.
library(DBI)
library(RPostgreSQL)
drv <- RPostgreSQL::PostgreSQL()

# Open a database connection, where `dbname` is the name of the CrateDB schema.
conn <- dbConnect(drv,
host = "localhost",
port = 5432,
user = "crate",
password = "crate",
dbname = "doc",
)
on.exit(DBI::dbDisconnect(conn), add = TRUE)

# Invoke a basic select query.
res <- dbGetQuery(conn, "SELECT mountain, region, height FROM sys.summits ORDER BY height DESC LIMIT 10;")
print(res)

# Delete testdrive table when needed.
if (dbExistsTable(conn, "r")) {
dbRemoveTable(conn, "r")
}

# Basic I/O.
res <- dbGetQuery(conn, "CREATE TABLE IF NOT EXISTS r (id INT PRIMARY KEY, data TEXT);")
res <- dbGetQuery(conn, "INSERT INTO r (id, data) VALUES (42, 'foobar');")
res <- dbGetQuery(conn, "REFRESH TABLE r;")
res <- dbGetQuery(conn, "SELECT * FROM r;")
print(res)