|
| 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 | +[](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