Skip to content

Commit ef4562c

Browse files
committed
Connect: Add R
1 parent 5f5906b commit ef4562c

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

docs/connect/index.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ CrateDB drivers and adapters for supported programming languages, frameworks, an
9797
{fab}`python`
9898
::::
9999

100+
::::{grid-item-card} R
101+
:link: connect-r
102+
:link-type: ref
103+
:link-alt: Connect to CrateDB using R
104+
:padding: 3
105+
:text-align: center
106+
:class-card: sd-pt-3
107+
:class-body: sd-fs-1
108+
:class-title: sd-fs-6
109+
{fab}`r-project`
110+
::::
111+
100112
::::{grid-item-card} Ruby
101113
:link: connect-ruby
102114
:link-type: ref
@@ -186,6 +198,7 @@ java
186198
javascript
187199
php
188200
python
201+
r/index
189202
ruby
190203
natural
191204
All drivers <drivers>

docs/connect/r/index.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
(connect-r)=
2+
3+
# R
4+
5+
:::{div} sd-text-muted
6+
Connect to CrateDB from R applications and notebooks.
7+
:::
8+
9+
:::{rubric} About
10+
:::
11+
12+
The [RPostgres] package is the canonical C++ Interface to PostgreSQL.
13+
An alternative is the [RPostgreSQL] package, written in C.
14+
RPostgres is actively maintained and often preferred.
15+
16+
:::{rubric} Synopsis
17+
:::
18+
19+
`example.r`
20+
```r
21+
# Connect to CrateDB from R, using the "RPostgres: C++ Interface to PostgreSQL".
22+
# https://cran.r-project.org/web/packages/RPostgres/
23+
24+
# Install the Postgres library on demand.
25+
if (!requireNamespace("RPostgres", quietly = TRUE)) {
26+
install.packages("RPostgres", repos="https://cran.r-project.org")
27+
}
28+
29+
# Load the DBI and Postgres libraries.
30+
library(DBI)
31+
library(RPostgres)
32+
33+
# Connect to database.
34+
conn <- dbConnect(RPostgres::Postgres(),
35+
host = "localhost",
36+
port = 5432,
37+
sslmode = "disable",
38+
user = "crate",
39+
password = "crate",
40+
dbname = "doc"
41+
)
42+
on.exit(DBI::dbDisconnect(conn), add = TRUE)
43+
44+
# Invoke a basic select query.
45+
res <- dbGetQuery(conn, "SELECT * FROM sys.summits ORDER BY height DESC LIMIT 10;")
46+
print(res)
47+
```
48+
49+
Start CrateDB using Docker or Podman, then invoke the example program.
50+
```shell
51+
docker run --rm --publish=5432:5432 crate '-Cdiscovery.type=single-node'
52+
```
53+
```shell
54+
Rscript example.r
55+
```
56+
57+
58+
:::{rubric} CrateDB Cloud
59+
:::
60+
61+
For connecting to CrateDB Cloud, use `sslmode = "require"`, and
62+
replace username, password, and hostname with values matching
63+
your environment.
64+
```r
65+
conn <- dbConnect(RPostgres::Postgres(),
66+
host = "testcluster.cratedb.net",
67+
port = 5432,
68+
sslmode = "require",
69+
user = "admin",
70+
password = "password",
71+
dbname = "doc"
72+
)
73+
```
74+
75+
## Example
76+
77+
:::{card}
78+
:link: https://github.com/crate/cratedb-examples/tree/main/by-language/r
79+
:link-type: url
80+
{material-outlined}`play_arrow;2em`
81+
Connect to CrateDB and CrateDB Cloud using R.
82+
+++
83+
Demonstrates basic examples that use the R RPostgres and RPostgreSQL packages.
84+
:::
85+
86+
[![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)
87+
88+
89+
[RPostgres]: https://cran.r-project.org/web/packages/RPostgres/
90+
[RPostgreSQL]: https://cran.r-project.org/web/packages/RPostgreSQL/

0 commit comments

Comments
 (0)