Skip to content

Commit 56ba0c1

Browse files
authored
[example] add R example (#2183)
## Summary This adds README and example R script (along with devbox.lock and devbox.json) ## How was it tested? I tested the example repo on both `aarch64-darwin` and `x86_64-linux` by `cd`'ing into `examples/data_science/R` directory; running `devbox shell` then `Rscript src/examplePlot.R`. I considered the test to have passed if an Rplots.pdf file was produced with a scatterplot image. This *should* do for most R users. However there is a case that I haven't handled: Sometimes users will want to install packages that are not on CRAN and therefore not on nixpkgs/nixhub.io (e.g. installing packages from github using the `R` package `devtools`). I'm not sure of an easy way to address this yet. Addresses #2182
1 parent 94de0d6 commit 56ba0c1

File tree

5 files changed

+377
-0
lines changed

5 files changed

+377
-0
lines changed

examples/data_science/R/.gitignore

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

examples/data_science/R/README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# R
2+
3+
[R is a free software environment for statistical computing and graphics](https://www.r-project.org/).
4+
5+
[**Example Repo**](https://github.com/jetify-com/devbox/tree/main/examples/data_science/R)
6+
7+
## Adding R to your Project
8+
9+
`devbox add [email protected]`, or in your `devbox.json` add:
10+
11+
```json
12+
"packages": [
13+
14+
],
15+
```
16+
17+
This will install R in your shell. You can find other versions of R by running `devbox search R`.
18+
You can also view the available versions on [Nixhub](https://www.nixhub.io/packages/R).
19+
20+
## Installing Packages
21+
22+
[CRAN](https://cran.r-project.org/) is the main repository of R packages.
23+
All of the CRAN packages are also available on [Nixhub](https://www.nixhub.io/).
24+
25+
You can install packages by running `devbox add rPackages.package_name`, where `package_name` is the name of the package you would normally install with `install.packages()`.
26+
Note that for packages with a dot in the name you will need to replace the dot with an underscore, i.e. `data.table` -> `data_table` (see example below).
27+
28+
```json
29+
{
30+
"packages": [
31+
32+
"rPackages.data_table@latest",
33+
"rPackages.ggplot2@latest",
34+
"rPackages.tidyverse@latest"
35+
],
36+
}
37+
```
38+
39+
You can access these packages in your R scripts as usual with `library(data.table)``.
40+
41+
## Example script
42+
43+
In this [example repo](https://github.com/jetify-com/devbox/tree/main/examples/data_science/R), after running `devbox shell`, you can start an R repl with `R` then create an example plot with `source("src/examplePlot.R")`.
44+
Alternatively run `Rscript src/examplePlot.R`.
45+
This will create an `Rplots.pdf` file.
46+
47+
## Troubleshooting
48+
49+
If you get warnings like:
50+
51+
> During startup - Warning messages:
52+
> 1: setting LC_CTYPE failed, using "C"
53+
> 2: Setting LC_COLLATE failed, using "C"
54+
> ...
55+
56+
then you need to set your locale.
57+
Find your locale (outside of a devbox shell) using `locale` in your terminal. You will see something like:
58+
59+
> LANG=en_NZ.UTF-8
60+
> LANGUAGE=en_NZ:en
61+
> LC_CTYPE="en_NZ.UTF-8"
62+
> ...
63+
64+
To set your locale, edit the `init_hook` array in the shell object in `devbox.json` to export two environment variables like below (using your specific locale):
65+
66+
```json
67+
{
68+
"shell": {
69+
"init_hook": [
70+
"export LANG=en_NZ.UTF-8",
71+
"export LC_ALL=en_NZ.UTF-8"
72+
]
73+
}
74+
}
75+
```

examples/data_science/R/devbox.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/jetify-com/devbox/0.10.6/.schema/devbox.schema.json",
3+
"packages": [
4+
5+
"rPackages.data_table@latest",
6+
"rPackages.ggplot2@latest",
7+
"rPackages.tidyverse@latest",
8+
"rPackages.dplyr@latest"
9+
],
10+
"shell": {
11+
"init_hook": [
12+
"echo 'Welcome to devbox!' > /dev/null"
13+
],
14+
"scripts": {
15+
"test": [
16+
"echo \"Error: no test specified\" && exit 1"
17+
]
18+
}
19+
}
20+
}

examples/data_science/R/devbox.lock

Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
{
2+
"lockfile_version": "1",
3+
"packages": {
4+
5+
"last_modified": "2024-07-07T16:08:25Z",
6+
"resolved": "github:NixOS/nixpkgs/ab82a9612aa45284d4adf69ee81871a389669a9e#R",
7+
"source": "devbox-search",
8+
"version": "4.4.1",
9+
"systems": {
10+
"aarch64-darwin": {
11+
"outputs": [
12+
{
13+
"name": "out",
14+
"path": "/nix/store/wzkkd1ks11fhs3qmwpqlw2vp9mrj5ybg-R-4.4.1",
15+
"default": true
16+
},
17+
{
18+
"name": "tex",
19+
"path": "/nix/store/wj95kdg34ymx1mzxfzndqlv9qwl5vil2-R-4.4.1-tex"
20+
}
21+
],
22+
"store_path": "/nix/store/wzkkd1ks11fhs3qmwpqlw2vp9mrj5ybg-R-4.4.1"
23+
},
24+
"aarch64-linux": {
25+
"outputs": [
26+
{
27+
"name": "out",
28+
"path": "/nix/store/dhd5x58iq4k202pl0b4dy4rf78214ky7-R-4.4.1",
29+
"default": true
30+
},
31+
{
32+
"name": "tex",
33+
"path": "/nix/store/zk6lv0ld9grlbijvljc3ny5zcjgrd31s-R-4.4.1-tex"
34+
}
35+
],
36+
"store_path": "/nix/store/dhd5x58iq4k202pl0b4dy4rf78214ky7-R-4.4.1"
37+
},
38+
"x86_64-darwin": {
39+
"outputs": [
40+
{
41+
"name": "out",
42+
"path": "/nix/store/ci829l73v5j6vq4190pa8xshf8w2zjn8-R-4.4.1",
43+
"default": true
44+
},
45+
{
46+
"name": "tex",
47+
"path": "/nix/store/zwax67ybxi49wkzm0wpfw45mwjh0wjm0-R-4.4.1-tex"
48+
}
49+
],
50+
"store_path": "/nix/store/ci829l73v5j6vq4190pa8xshf8w2zjn8-R-4.4.1"
51+
},
52+
"x86_64-linux": {
53+
"outputs": [
54+
{
55+
"name": "out",
56+
"path": "/nix/store/35ws8rzvrjhg9vr1sv05kh30401c25vw-R-4.4.1",
57+
"default": true
58+
},
59+
{
60+
"name": "tex",
61+
"path": "/nix/store/sjsnn5mfmng8ki9ggrh00lzy2n7qry7g-R-4.4.1-tex"
62+
}
63+
],
64+
"store_path": "/nix/store/35ws8rzvrjhg9vr1sv05kh30401c25vw-R-4.4.1"
65+
}
66+
}
67+
},
68+
"rPackages.data_table@latest": {
69+
"last_modified": "2024-07-07T07:43:47Z",
70+
"resolved": "github:NixOS/nixpkgs/b60793b86201040d9dee019a05089a9150d08b5b#rPackages.data_table",
71+
"source": "devbox-search",
72+
"version": "1.15.4",
73+
"systems": {
74+
"aarch64-darwin": {
75+
"outputs": [
76+
{
77+
"name": "out",
78+
"path": "/nix/store/1znk97633fypym8rzjrp6ylpikbgsq21-r-data.table-1.15.4",
79+
"default": true
80+
}
81+
],
82+
"store_path": "/nix/store/1znk97633fypym8rzjrp6ylpikbgsq21-r-data.table-1.15.4"
83+
},
84+
"aarch64-linux": {
85+
"outputs": [
86+
{
87+
"name": "out",
88+
"path": "/nix/store/wgy0m11pxcx0bk3aksr7hhz1gbhc3q44-r-data.table-1.15.4",
89+
"default": true
90+
}
91+
],
92+
"store_path": "/nix/store/wgy0m11pxcx0bk3aksr7hhz1gbhc3q44-r-data.table-1.15.4"
93+
},
94+
"x86_64-darwin": {
95+
"outputs": [
96+
{
97+
"name": "out",
98+
"path": "/nix/store/4sgbnbs276rrx5r0ixymsrc6x1d3ynj6-r-data.table-1.15.4",
99+
"default": true
100+
}
101+
],
102+
"store_path": "/nix/store/4sgbnbs276rrx5r0ixymsrc6x1d3ynj6-r-data.table-1.15.4"
103+
},
104+
"x86_64-linux": {
105+
"outputs": [
106+
{
107+
"name": "out",
108+
"path": "/nix/store/z9k8fdb75qb210cznnx0pxlkmd4r72n1-r-data.table-1.15.4",
109+
"default": true
110+
}
111+
],
112+
"store_path": "/nix/store/z9k8fdb75qb210cznnx0pxlkmd4r72n1-r-data.table-1.15.4"
113+
}
114+
}
115+
},
116+
"rPackages.dplyr@latest": {
117+
"last_modified": "2024-07-07T16:08:25Z",
118+
"resolved": "github:NixOS/nixpkgs/ab82a9612aa45284d4adf69ee81871a389669a9e#rPackages.dplyr",
119+
"source": "devbox-search",
120+
"version": "1.1.4",
121+
"systems": {
122+
"aarch64-darwin": {
123+
"outputs": [
124+
{
125+
"name": "out",
126+
"path": "/nix/store/swh5559irfb5cynm7vlrk6s5hyd9b9m6-r-dplyr-1.1.4",
127+
"default": true
128+
}
129+
],
130+
"store_path": "/nix/store/swh5559irfb5cynm7vlrk6s5hyd9b9m6-r-dplyr-1.1.4"
131+
},
132+
"aarch64-linux": {
133+
"outputs": [
134+
{
135+
"name": "out",
136+
"path": "/nix/store/vkgs1flmm0m4nx0rgydb00khx485zhm3-r-dplyr-1.1.4",
137+
"default": true
138+
}
139+
],
140+
"store_path": "/nix/store/vkgs1flmm0m4nx0rgydb00khx485zhm3-r-dplyr-1.1.4"
141+
},
142+
"x86_64-darwin": {
143+
"outputs": [
144+
{
145+
"name": "out",
146+
"path": "/nix/store/79hkls1vz3399fbfhbfkdnpj3zngg0jv-r-dplyr-1.1.4",
147+
"default": true
148+
}
149+
],
150+
"store_path": "/nix/store/79hkls1vz3399fbfhbfkdnpj3zngg0jv-r-dplyr-1.1.4"
151+
},
152+
"x86_64-linux": {
153+
"outputs": [
154+
{
155+
"name": "out",
156+
"path": "/nix/store/7lsabh7q1ckp6pv968z64qbbzabkvr68-r-dplyr-1.1.4",
157+
"default": true
158+
}
159+
],
160+
"store_path": "/nix/store/7lsabh7q1ckp6pv968z64qbbzabkvr68-r-dplyr-1.1.4"
161+
}
162+
}
163+
},
164+
"rPackages.ggplot2@latest": {
165+
"last_modified": "2024-07-07T16:08:25Z",
166+
"resolved": "github:NixOS/nixpkgs/ab82a9612aa45284d4adf69ee81871a389669a9e#rPackages.ggplot2",
167+
"source": "devbox-search",
168+
"version": "3.5.1",
169+
"systems": {
170+
"aarch64-darwin": {
171+
"outputs": [
172+
{
173+
"name": "out",
174+
"path": "/nix/store/n1zk4gzqdz3wqzsq8zjaia8z18ba3qrz-r-ggplot2-3.5.1",
175+
"default": true
176+
}
177+
],
178+
"store_path": "/nix/store/n1zk4gzqdz3wqzsq8zjaia8z18ba3qrz-r-ggplot2-3.5.1"
179+
},
180+
"aarch64-linux": {
181+
"outputs": [
182+
{
183+
"name": "out",
184+
"path": "/nix/store/lvfanffrdx1ghk8a1xxp92rwrpjr7888-r-ggplot2-3.5.1",
185+
"default": true
186+
}
187+
],
188+
"store_path": "/nix/store/lvfanffrdx1ghk8a1xxp92rwrpjr7888-r-ggplot2-3.5.1"
189+
},
190+
"x86_64-darwin": {
191+
"outputs": [
192+
{
193+
"name": "out",
194+
"path": "/nix/store/dd73mgxzyk2yrra5rn9371ncp74gsa6w-r-ggplot2-3.5.1",
195+
"default": true
196+
}
197+
],
198+
"store_path": "/nix/store/dd73mgxzyk2yrra5rn9371ncp74gsa6w-r-ggplot2-3.5.1"
199+
},
200+
"x86_64-linux": {
201+
"outputs": [
202+
{
203+
"name": "out",
204+
"path": "/nix/store/vcw6gbyd4867m0x9qd3d1a4bkx2p21pr-r-ggplot2-3.5.1",
205+
"default": true
206+
}
207+
],
208+
"store_path": "/nix/store/vcw6gbyd4867m0x9qd3d1a4bkx2p21pr-r-ggplot2-3.5.1"
209+
}
210+
}
211+
},
212+
"rPackages.tidyverse@latest": {
213+
"last_modified": "2024-07-07T07:43:47Z",
214+
"resolved": "github:NixOS/nixpkgs/b60793b86201040d9dee019a05089a9150d08b5b#rPackages.tidyverse",
215+
"source": "devbox-search",
216+
"version": "2.0.0",
217+
"systems": {
218+
"aarch64-darwin": {
219+
"outputs": [
220+
{
221+
"name": "out",
222+
"path": "/nix/store/p3x6f60xsrva2qbadjivk8bwr41s3xkb-r-tidyverse-2.0.0",
223+
"default": true
224+
}
225+
],
226+
"store_path": "/nix/store/p3x6f60xsrva2qbadjivk8bwr41s3xkb-r-tidyverse-2.0.0"
227+
},
228+
"aarch64-linux": {
229+
"outputs": [
230+
{
231+
"name": "out",
232+
"path": "/nix/store/raffylf8yn3saxw0q1vkijh9pl3c9svi-r-tidyverse-2.0.0",
233+
"default": true
234+
}
235+
],
236+
"store_path": "/nix/store/raffylf8yn3saxw0q1vkijh9pl3c9svi-r-tidyverse-2.0.0"
237+
},
238+
"x86_64-darwin": {
239+
"outputs": [
240+
{
241+
"name": "out",
242+
"path": "/nix/store/622vvcavqwq4jynyw8nkzqspzi0wjivg-r-tidyverse-2.0.0",
243+
"default": true
244+
}
245+
],
246+
"store_path": "/nix/store/622vvcavqwq4jynyw8nkzqspzi0wjivg-r-tidyverse-2.0.0"
247+
},
248+
"x86_64-linux": {
249+
"outputs": [
250+
{
251+
"name": "out",
252+
"path": "/nix/store/9j633ss7p6lk1h1szsg0z7rgj0avvv6n-r-tidyverse-2.0.0",
253+
"default": true
254+
}
255+
],
256+
"store_path": "/nix/store/9j633ss7p6lk1h1szsg0z7rgj0avvv6n-r-tidyverse-2.0.0"
257+
}
258+
}
259+
}
260+
}
261+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Load the required libraries
2+
library(data.table)
3+
library(ggplot2)
4+
5+
# Create example data
6+
data <- data.table(
7+
x = rnorm(100), # 100 random numbers from a normal distribution
8+
y = rnorm(100) # 100 random numbers from a normal distribution
9+
)
10+
11+
# Plot the data using ggplot2
12+
demoPlot <- ggplot(data, aes(x = x, y = y)) +
13+
geom_point() + # Create a scatter plot
14+
theme_minimal() + # Use a minimal theme
15+
labs(title = "Scatter Plot of Random Data",
16+
x = "X Axis",
17+
y = "Y Axis")
18+
19+
print(demoPlot)

0 commit comments

Comments
 (0)