Last active
November 17, 2025 17:52
-
-
Save t-kalinowski/6ee1af983749e5ce74d551adf65b072a to your computer and use it in GitHub Desktop.
Setup codex-universal (Codex Cloud) for R package development
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This setup script prepares a Codex Cloud environment for developing and testing R packages. | |
| # It installs the rig, R, an R toolchain, configures CRAN and parallel-testing defaults, | |
| # It installs all package dependencies listed in the project’s DESCRIPTION file. | |
| # It then installs the local package itself. | |
| # If the package uses reticulate, it also materializes a package python venv | |
| # install rig | |
| curl -L https://rig.r-pkg.org/deb/rig.gpg -o /etc/apt/trusted.gpg.d/rig.gpg | |
| echo "deb http://rig.r-pkg.org/deb rig main" > /etc/apt/sources.list.d/rig.list | |
| apt-get update | |
| apt-get install -y r-rig | |
| # install R | |
| rig add release | |
| # Set some R env variables for R for better devtools/testthat behavior | |
| # (I interactively asked codex cloud how many cores are available, | |
| # it ran nproc and got back 5) | |
| cat >> ~/.Renviron <<'EOF' | |
| NOT_CRAN=true | |
| TESTTHAT_CPUS=5 | |
| R_CLI_DYNAMIC=false, | |
| R_CLI_NUM_COLORS=1, | |
| NO_COLOR=1, | |
| TESTTHAT_PARALLEL=false | |
| EOF | |
| # Set up some R options to | |
| # - configure a CRAN with Posit Package Manager, which | |
| # offers pre-built binaries. | |
| # - improve testthat output for codex | |
| cat >> ~/.Rprofile <<'EOF' | |
| options( | |
| repos = c(CRAN = sprintf( | |
| "https://packagemanager.posit.co/cran/latest/bin/linux/noble-%s/%s", | |
| R.version["arch"], substr(getRversion(), 1, 3) | |
| )), | |
| Ncpus = 4L, | |
| testthat.use_colours = FALSE, | |
| testthat.summary.omit_dots = TRUE, | |
| pager = 'cat', | |
| cli.dynamic = FALSE, | |
| cli.num_colors = 1L, | |
| testthat.default_reporter = "summary" | |
| ) | |
| EOF | |
| # - parse the package DESCRIPTION file and install all package dependencies | |
| # - then install the package itself and try to load it. | |
| # - then, if the package has python/reticulate requirements, | |
| # force reticulate to download all python packages | |
| # (Note, we don't use pak for R package install because it errors on codex cloud due to internet proxy settings) | |
| Rscript - <<'EOF' | |
| desc <- as.list(as.data.frame(read.dcf("DESCRIPTION"))) | |
| pkgs <- c( | |
| desc$Depends, | |
| desc$Imports, | |
| desc$Suggests | |
| ) |> | |
| strsplit(",", fixed = TRUE) |> | |
| unlist(use.names = FALSE) |> | |
| trimws() |> | |
| sub("\\s*\\(.*\\)", "", x = _) |> | |
| c("remotes", "devtools") |> | |
| setdiff(unname(c( | |
| "R", | |
| installed.packages(priority = c("base", "recommended"))[, "Package"] | |
| ))) | |
| install.packages(pkgs) | |
| remotes::install_local() | |
| try({ | |
| # try to load the package | |
| library(desc$Package, character.only = TRUE) | |
| # if the package has python requirements, | |
| # force reticulate to materialize a venv (i.e., download python, python packages) | |
| if (!"keras3" %in% loadedNamespaces()) { | |
| if ("keras" %in% loadedNamespaces()) keras::py_require_legacy_keras() | |
| if ("tensorflow" %in% loadedNamespaces()) tensorflow::py_require_tensorflow() | |
| } | |
| if ("reticulate" %in% loadedNamespaces()) { | |
| reticulate::py_config() | |
| cat("\nUV_OFFLINE=1\n", file = "~/.Renviron", append = TRUE) | |
| } | |
| }) | |
| EOF | |
| curl -LsSf https://github.com/posit-dev/air/releases/latest/download/air-installer.sh | sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment