From 3bc08dd6b11848f69d23d42cd2cc543aced43dbf Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Thu, 16 Oct 2025 00:14:26 +0200 Subject: [PATCH 1/3] Connect: Add R --- docs/connect/index.md | 13 ++++++ docs/connect/r/index.md | 88 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 docs/connect/r/index.md diff --git a/docs/connect/index.md b/docs/connect/index.md index e9e6b458..d3d8bddf 100644 --- a/docs/connect/index.md +++ b/docs/connect/index.md @@ -93,6 +93,18 @@ CrateDB drivers and adapters for supported programming languages, frameworks, an {fab}`python` :::: +::::{grid-item-card} R +:link: connect-r +:link-type: ref +:link-alt: Connect to CrateDB using R +:padding: 3 +:text-align: center +:class-card: sd-pt-3 +:class-body: sd-fs-1 +:class-title: sd-fs-6 +{fab}`r-project` +:::: + ::::{grid-item-card} Ruby :link: connect-ruby :link-type: ref @@ -203,6 +215,7 @@ java/index javascript php python +r/index ruby odbc/index natural diff --git a/docs/connect/r/index.md b/docs/connect/r/index.md new file mode 100644 index 00000000..ac59fa61 --- /dev/null +++ b/docs/connect/r/index.md @@ -0,0 +1,88 @@ +(connect-r)= + +# R + +:::{div} sd-text-muted +Connect to CrateDB from R applications and notebooks. +::: + +:::{rubric} About +::: + +The [RPostgres] package is the canonical C++ Interface to PostgreSQL, +which is actively maintained and often preferred. +An alternative is the [RPostgreSQL] package, written in C. + +:::{rubric} Synopsis +::: + +`example.r` +```r +# Connect to CrateDB from R, using the "RPostgres: C++ Interface to PostgreSQL". +# https://cran.r-project.org/web/packages/RPostgres/ + +# Install the Postgres library on demand. +if (!requireNamespace("RPostgres", quietly = TRUE)) { + install.packages("RPostgres", repos="https://cran.r-project.org") +} + +# Load the DBI and Postgres libraries. +library(DBI) +library(RPostgres) + +# Connect to database. +conn <- dbConnect(RPostgres::Postgres(), + 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 * FROM sys.summits ORDER BY height DESC LIMIT 10;") +print(res) +``` + +:::{include} ../_cratedb.md +::: +```shell +Rscript example.r +``` + + +:::{rubric} CrateDB Cloud +::: + +For connecting to CrateDB Cloud, use `sslmode = "require"`, and +replace hostname, username, and password with values matching +your environment. +```r +conn <- dbConnect(RPostgres::Postgres(), + host = "testcluster.cratedb.net", + port = 5432, + sslmode = "require", + user = "admin", + password = "password", + dbname = "doc" + ) +``` + +## Example + +:::{card} +:link: https://github.com/crate/cratedb-examples/tree/main/by-language/r +:link-type: url +{material-outlined}`play_arrow;2em` +Connect to CrateDB and CrateDB Cloud using R. ++++ +Demonstrates basic examples that use the R RPostgres and RPostgreSQL packages. +::: + +[![R](https://github.com/crate/cratedb-examples/actions/workflows/lang-r.yml/badge.svg)](https://github.com/crate/cratedb-examples/actions/workflows/lang-r.yml) + + +[RPostgres]: https://cran.r-project.org/web/packages/RPostgres/ +[RPostgreSQL]: https://cran.r-project.org/web/packages/RPostgreSQL/ From 9bcca639295103a1573d1fccd07b1b49a519bc10 Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Sun, 26 Oct 2025 20:49:22 +0100 Subject: [PATCH 2/3] Connect/R: Copy editing --- docs/connect/r/index.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/connect/r/index.md b/docs/connect/r/index.md index ac59fa61..94356eab 100644 --- a/docs/connect/r/index.md +++ b/docs/connect/r/index.md @@ -2,6 +2,12 @@ # R +:::{div} .float-right .text-right +[![R](https://github.com/crate/cratedb-examples/actions/workflows/lang-r.yml/badge.svg)](https://github.com/crate/cratedb-examples/actions/workflows/lang-r.yml) +::: +:::{div} .clearfix +::: + :::{div} sd-text-muted Connect to CrateDB from R applications and notebooks. ::: @@ -75,14 +81,12 @@ conn <- dbConnect(RPostgres::Postgres(), :::{card} :link: https://github.com/crate/cratedb-examples/tree/main/by-language/r :link-type: url -{material-outlined}`play_arrow;2em` +{material-regular}`play_arrow;2em` Connect to CrateDB and CrateDB Cloud using R. +++ Demonstrates basic examples that use the R RPostgres and RPostgreSQL packages. ::: -[![R](https://github.com/crate/cratedb-examples/actions/workflows/lang-r.yml/badge.svg)](https://github.com/crate/cratedb-examples/actions/workflows/lang-r.yml) - [RPostgres]: https://cran.r-project.org/web/packages/RPostgres/ [RPostgreSQL]: https://cran.r-project.org/web/packages/RPostgreSQL/ From 0f7fb4d76eb6afa14e92ef512a29ccbbbb7d330e Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Thu, 30 Oct 2025 01:01:28 +0100 Subject: [PATCH 3/3] Connect/R: Copy editing --- docs/connect/r/index.md | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/docs/connect/r/index.md b/docs/connect/r/index.md index 94356eab..e91d82fb 100644 --- a/docs/connect/r/index.md +++ b/docs/connect/r/index.md @@ -52,13 +52,6 @@ res <- dbGetQuery(conn, "SELECT * FROM sys.summits ORDER BY height DESC LIMIT 10 print(res) ``` -:::{include} ../_cratedb.md -::: -```shell -Rscript example.r -``` - - :::{rubric} CrateDB Cloud ::: @@ -76,7 +69,19 @@ conn <- dbConnect(RPostgres::Postgres(), ) ``` -## Example +:::{rubric} Quickstart example +::: + +Create the file `example.r` including the synopsis code shared above. + +:::{include} ../_cratedb.md +::: +```shell +Rscript example.r +``` + +:::{rubric} Full example +::: :::{card} :link: https://github.com/crate/cratedb-examples/tree/main/by-language/r