Skip to content

Commit 90c40d0

Browse files
committed
R: Add minimal example
1 parent b0ae7e9 commit 90c40d0

File tree

6 files changed

+140
-0
lines changed

6 files changed

+140
-0
lines changed

.github/workflows/lang-r.yml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
name: R
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- '.github/workflows/lang-r.yml'
7+
- 'by-language/r/**'
8+
- '/requirements.txt'
9+
push:
10+
branches: [ main ]
11+
paths:
12+
- '.github/workflows/lang-r.yml'
13+
- 'by-language/r/**'
14+
- '/requirements.txt'
15+
16+
# Allow job to be triggered manually.
17+
workflow_dispatch:
18+
19+
# Run job each night after CrateDB nightly has been published.
20+
schedule:
21+
- cron: '0 3 * * *'
22+
23+
# Cancel in-progress jobs when pushing to the same branch.
24+
concurrency:
25+
cancel-in-progress: true
26+
group: ${{ github.workflow }}-${{ github.ref }}
27+
28+
jobs:
29+
30+
test:
31+
name: "
32+
CrateDB: ${{ matrix.cratedb-version }}
33+
on ${{ matrix.os }}"
34+
runs-on: ${{ matrix.os }}
35+
strategy:
36+
fail-fast: false
37+
matrix:
38+
os: [ 'ubuntu-latest' ]
39+
cratedb-version: [ 'nightly' ]
40+
41+
services:
42+
cratedb:
43+
image: crate/crate:${{ matrix.cratedb-version }}
44+
ports:
45+
- 4200:4200
46+
- 5432:5432
47+
env:
48+
CRATE_HEAP_SIZE: 4g
49+
50+
steps:
51+
52+
- name: Acquire sources
53+
uses: actions/checkout@v4
54+
55+
- name: Install R
56+
uses: r-lib/actions/setup-r@v2
57+
58+
- name: Install utilities
59+
run: |
60+
pip install -r requirements.txt
61+
62+
- name: Validate by-language/r
63+
run: |
64+
ngr test by-language/r

by-language/r/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.RData
2+
.Rhistory

by-language/r/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
install:
2+
3+
test:
4+
Rscript basic.r

by-language/r/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Connecting to CrateDB with R
2+
3+
## About
4+
5+
The file `basic.r` includes a basic example program that uses the R
6+
[RPostgreSQL] package, the canonical database interface and 'PostgreSQL'
7+
driver for 'R', to connect to CrateDB.
8+
9+
## Usage
10+
11+
Start a CrateDB instance for evaluation purposes.
12+
```shell
13+
docker run -it --rm --publish=4200:4200 --publish=5432:5432 crate:latest
14+
```
15+
16+
Invoke example program.
17+
```shell
18+
Rscript basic.r
19+
```
20+
21+
Invoke software tests.
22+
```shell
23+
make test
24+
```
25+
26+
27+
[RPostgreSQL]: https://cran.r-project.org/web/packages/RPostgreSQL/

by-language/r/backlog.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Backlog for R with CrateDB
2+
3+
## Bugs
4+
5+
- `SELECT * FROM sys.summits` will fail like:
6+
```
7+
RPosgreSQL warning: unrecognized PostgreSQL field type point (id:600) in column 1
8+
```

by-language/r/basic.r

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Install driver on demand.
2+
# RPostgreSQL: R Interface to the 'PostgreSQL' Database System
3+
# https://cran.r-project.org/web/packages/RPostgreSQL/
4+
if (!require(RPostgreSQL)) {
5+
install.packages("RPostgreSQL", repos="http://cran.us.r-project.org")
6+
}
7+
stopifnot(require(RPostgreSQL))
8+
9+
# Load the DBI library.
10+
library(DBI)
11+
drv <- dbDriver("PostgreSQL")
12+
13+
# Open a database connection, where `dbname` is the name of the CrateDB schema.
14+
con <- dbConnect(drv,
15+
host = "localhost",
16+
port = 5432,
17+
user = "crate",
18+
dbname = "testdrive",
19+
)
20+
21+
# Invoke a basic select query.
22+
res <- dbGetQuery(con, "SELECT mountain, region, height FROM sys.summits ORDER BY HEIGHT DESC LIMIT 10;")
23+
print(res)
24+
25+
# Delete testdrive table when needed.
26+
if (dbExistsTable(con, "r")) {
27+
dbRemoveTable(con, "r")
28+
}
29+
30+
# Basic I/O.
31+
res <- dbGetQuery(con, "CREATE TABLE IF NOT EXISTS r (id INT PRIMARY KEY, data TEXT);")
32+
res <- dbGetQuery(con, "INSERT INTO r (id, data) VALUES (42, 'foobar');")
33+
res <- dbGetQuery(con, "REFRESH TABLE r;")
34+
res <- dbGetQuery(con, "SELECT * FROM r;")
35+
print(res)

0 commit comments

Comments
 (0)