From 1a00282288ef0f549879a0af3ec0d5846a967951 Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Fri, 17 Oct 2025 00:46:47 +0200 Subject: [PATCH] R: Extend example to demonstrate both RPostgres and RPostgreSQL --- by-language/r/Makefile | 3 ++- by-language/r/README.md | 13 ++++++---- by-language/r/basic.r | 35 ------------------------- by-language/r/basic_rpostgres.r | 43 +++++++++++++++++++++++++++++++ by-language/r/basic_rpostgresql.r | 39 ++++++++++++++++++++++++++++ 5 files changed, 92 insertions(+), 41 deletions(-) delete mode 100644 by-language/r/basic.r create mode 100644 by-language/r/basic_rpostgres.r create mode 100644 by-language/r/basic_rpostgresql.r diff --git a/by-language/r/Makefile b/by-language/r/Makefile index fe2d4865..c6105a9d 100644 --- a/by-language/r/Makefile +++ b/by-language/r/Makefile @@ -1,4 +1,5 @@ install: test: - Rscript basic.r + Rscript basic_rpostgres.r + Rscript basic_rpostgresql.r diff --git a/by-language/r/README.md b/by-language/r/README.md index 07cdc9cd..e1b4bdf2 100644 --- a/by-language/r/README.md +++ b/by-language/r/README.md @@ -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 @@ -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. @@ -24,4 +26,5 @@ make test ``` +[RPostgres]: https://cran.r-project.org/web/packages/RPostgres/ [RPostgreSQL]: https://cran.r-project.org/web/packages/RPostgreSQL/ diff --git a/by-language/r/basic.r b/by-language/r/basic.r deleted file mode 100644 index 56ec2491..00000000 --- a/by-language/r/basic.r +++ /dev/null @@ -1,35 +0,0 @@ -# Install driver on demand. -# RPostgreSQL: R Interface to the 'PostgreSQL' Database System -# https://cran.r-project.org/web/packages/RPostgreSQL/ -if (!require(RPostgreSQL)) { - install.packages("RPostgreSQL", repos="http://cran.us.r-project.org") -} -stopifnot(require(RPostgreSQL)) - -# Load the DBI library. -library(DBI) -drv <- dbDriver("PostgreSQL") - -# Open a database connection, where `dbname` is the name of the CrateDB schema. -con <- dbConnect(drv, - host = "localhost", - port = 5432, - user = "crate", - dbname = "testdrive", - ) - -# Invoke a basic select query. -res <- dbGetQuery(con, "SELECT mountain, region, height FROM sys.summits ORDER BY HEIGHT DESC LIMIT 10;") -print(res) - -# Delete testdrive table when needed. -if (dbExistsTable(con, "r")) { - dbRemoveTable(con, "r") -} - -# Basic I/O. -res <- dbGetQuery(con, "CREATE TABLE IF NOT EXISTS r (id INT PRIMARY KEY, data TEXT);") -res <- dbGetQuery(con, "INSERT INTO r (id, data) VALUES (42, 'foobar');") -res <- dbGetQuery(con, "REFRESH TABLE r;") -res <- dbGetQuery(con, "SELECT * FROM r;") -print(res) diff --git a/by-language/r/basic_rpostgres.r b/by-language/r/basic_rpostgres.r new file mode 100644 index 00000000..71080e08 --- /dev/null +++ b/by-language/r/basic_rpostgres.r @@ -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) diff --git a/by-language/r/basic_rpostgresql.r b/by-language/r/basic_rpostgresql.r new file mode 100644 index 00000000..0fab7c2e --- /dev/null +++ b/by-language/r/basic_rpostgresql.r @@ -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)