Back to Devbox

R

examples/data_science/R/README.md

0.17.22.2 KB
Original Source

R

R is a free software environment for statistical computing and graphics.

Example Repo

Adding R to your Project

devbox add [email protected], or in your devbox.json add:

json
  "packages": [
    "[email protected]"
  ],

This will install R in your shell. You can find other versions of R by running devbox search R. You can also view the available versions on Nixhub.

Installing Packages

CRAN is the main repository of R packages. All of the CRAN packages are also available on Nixhub.

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(). 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).

json
{
    "packages": [
      "[email protected]",
      "rPackages.data_table@latest",
      "rPackages.ggplot2@latest",
      "rPackages.tidyverse@latest"
    ],
}

You can access these packages in your R scripts as usual with `library(data.table)``.

Example script

In this example repo, after running devbox shell, you can start an R repl with R then create an example plot with source("src/examplePlot.R"). Alternatively run Rscript src/examplePlot.R. This will create an Rplots.pdf file.

Troubleshooting

If you get warnings like:

During startup - Warning messages: 1: setting LC_CTYPE failed, using "C" 2: Setting LC_COLLATE failed, using "C" ...

then you need to set your locale. Find your locale (outside of a devbox shell) using locale in your terminal. You will see something like:

LANG=en_NZ.UTF-8 LANGUAGE=en_NZ:en LC_CTYPE="en_NZ.UTF-8" ...

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):

json
{
  "shell": {
    "init_hook": [
      "export LANG=en_NZ.UTF-8",
      "export LC_ALL=en_NZ.UTF-8"
    ]
  }
}