Grilled Cheese

ExploreLog inSign up
Terms of UsePrivacy PolicyCommunity StandardsHelpGet the app

Grilled Cheese is a product of Village Compute

Version devBuilt at: 2026-12-03 22:42:33 EST

PhotosVideosAllPeople
LatestRanked
@drmowinckels.ioDec 5, 2026, 8:02 AM

#RCLIAdvent2026 Day 5: environment variables #rstats

Many CLIs read config from env vars. FreeSurfer needs FREESURFER_HOME and SUBJECTS_DIR, which users normally set by sourcing FreeSurfer's setup script in their shell.

Title card. Day 5 of 25, #RCLIAdvent2026: Environment variables. Set what the tool expects without sourcing scripts.Code slide: Set variables for one call. Lines starting with #> are output.

processx::run(
  "sh", c("-c", "echo $SUBJECTS_DIR"),
  env = c("current", SUBJECTS_DIR = "/data/subjects")
)$stdout
#> [1] "/data/subjects\n"

withr::with_envvar(
  c(SUBJECTS_DIR = "/data/subjects"),
  system2("sh", c("-c", shQuote("echo $SUBJECTS_DIR")), stdout = TRUE)
)
#> [1] "/data/subjects"Code slide: Borrow a setup script's environment. Lines starting with #> are output.

env_from_script <- function(script) {
  cmd <- sprintf('source "%s" >/dev/null && env', script)
  out <- processx::run("bash", c("-c", cmd))$stdout
  lines <- grep("=", strsplit(out, "\n")[[1]], value = TRUE)
  kv <- regmatches(lines, regexpr("=", lines), invert = TRUE)
  setNames(vapply(kv, `[`, "", 2), vapply(kv, `[`, "", 1))
}

env_from_script(setup)["SUBJECTS_DIR"]
#>                     SUBJECTS_DIR 
#> "/usr/local/freesurfer/subjects"Tip slide: Keep it local.

- R can't source a shell script
- Sys.setenv() leaks into the user's session
- "current" keeps the environment and adds yours
- withr::with_envvar() does the same for system2()
@drmowinckels.ioDec 4, 2026, 8:03 AM

#RCLIAdvent2026 Day 4: finding the executable #rstats

The most common bug report for a wrapper package: "it works in the terminal but not in RStudio."

Title card. Day 4 of 25, #RCLIAdvent2026: Finding the executable. Override, PATH, then known locations.Tip slide: Works in the terminal, not in RStudio.

- Sys.which() searches the PATH R sees
- GUI apps on macOS often skip your shell's PATH
- The tool is installed, R just can't see it
- So check more than one placeCode slide: Look in order.

find_tool <- function(name, option, envvar = NULL, candidates = NULL) {
  paths <- c(
    getOption(option),
    if (!is.null(envvar)) Sys.getenv(envvar),
    Sys.which(name),
    candidates
  )
  found <- paths[nzchar(paths) & fs::file_exists(paths)]
  if (length(found) == 0) {
    cli::cli_abort(c(
      "Could not find {.code {name}}.",
      i = "Set {.code options({option} = \"/path/to/{name}\")}",
      " " = "or add it to your PATH."
    ))
  }
  unname(found[[1]])
}Code slide: Fail with a fix. Lines starting with #> are output.

withr::with_path(
  "/usr/bin",
  find_tool("recon-all", option = "freesurfer.recon_all"),
  action = "replace"
)
#> Error in `find_tool()`:
#> ! Could not find `recon-all`.
#> ℹ Set `options(freesurfer.recon_all = "/path/to/recon-all")`
#>   or add it to your PATH.
@drmowinckels.ioDec 3, 2026, 8:02 AM

#RCLIAdvent2026 Day 3: processx::run() #rstats

Yesterday's system() and system2() fail silently. processx::run() doesn't.

Honest admission: I don't use it in my own wrappers yet. Researching this calendar sold me on it.

res <- processx::run("pandoc", "--version")

Title card. Day 3 of 25, #RCLIAdvent2026: processx::run(). Arguments as a vector. No shell. Real errors.Code slide: Arguments as a vector, no shell. Lines starting with #> are output.

res <- processx::run("pandoc", "--version")
res$status
#> [1] 0
strsplit(res$stdout, "\n")[[1]][1]
#> [1] "pandoc 3.11"Code slide: Failures you can handle. Lines starting with #> are output.

failed <- processx::run(
  "git", c("-C", tempdir(), "rev-parse"),
  error_on_status = FALSE
)
failed$status
#> [1] 128

processx::run("sleep", "5", timeout = 1)
#> Error in `processx::run()`:
#> ! ! System command 'sleep' timed outTip slide: What you get.

- No shell, so no quoting to get wrong
- A real R error on a non-zero exit
- stdout and stderr as plain strings
- timeout = and echo = TRUE for long jobs
@drmowinckels.ioDec 2, 2026, 8:02 AM

#RCLIAdvent2026 Day 2: system() vs system2() #rstats

Base R has two functions for running external commands, and they share one trap.

Title card. Day 2 of 25, #RCLIAdvent2026: system() vs system2(). Two base R ways to run a command.Code slide: One string, or command + arguments. Lines starting with #> are output.

system("git --version", intern = TRUE)
#> [1] "git version 2.54.0 (Apple Git-157)"

system2("git", "--version", stdout = TRUE)
#> [1] "git version 2.54.0 (Apple Git-157)"Code slide: The trap. Lines starting with #> are output.

out <- system2(
  "git", c("-C", tempdir(), "rev-parse"),
  stdout = TRUE, stderr = TRUE
)
#> Warning in system2("git", c("-C", tempdir(), "rev-parse"), stdout = TRUE, :
#> running command ''git' -C
#> /tmp rev-parse 2>&1'
#> had status 128

attr(out, "status")
#> [1] 128Tip slide: Check the status.

- A failing command gives a warning, not an error
- The exit code hides in attr(out, "status")
- Unchecked, a failed run looks like a success
- Tomorrow: a package that errors for you
@drmowinckels.ioDec 1, 2026, 8:02 AM

Welcome to #RCLIAdvent2026 🎄 #rstats

For 25 days I'll post one technique a day for wrapping command-line tools in R, from the first system2() call all the way to CRAN.

Day 1: why wrap a CLI at all?

Title card. Day 1 of 25, #RCLIAdvent2026: Why wrap a CLI? The tool already works. R adds data in and data out.Code slide: In bash, one file at a time.

pandoc intro.md -o intro.html
pandoc methods.md -o methods.html
pandoc results.md -o results.htmlCode slide: In R, a whole vector of files. Lines starting with #> are output.

md_to_html <- function(input) {
  output <- fs::path_ext_set(input, "html")
  processx::run("pandoc", c(input, "-o", output))
  output
}

vapply(notes, md_to_html, character(1), USE.NAMES = FALSE)
#> [1] "/tmp/intro.html"  
#> [2] "/tmp/methods.html"Tip slide: Two roads.

- Wrap the CLI: call the program, pass arguments, read results back
- Bind the library: e.g. rambutan binds lychee via extendr
- No executable to find, but users need Rust to build it
- This calendar takes the first road
@drmowinckels.ioNov 30, 2026, 8:01 AM

Tomorrow I open the first door of #RCLIAdvent2026 🎄 #rstats

25 days of wrapping command-line tools from R: one small technique a day, from your first system2() call all the way to a package that's ready for CRAN.

Title card. Starts tomorrow, #RCLIAdvent2026: Wrapping CLI tools from R. 25 days, one technique a day, all the way to CRAN.Tip slide: Four weeks.

- Days 1-6: running commands, finding the tool, paths
- Days 7-12: building the wrapper itself
- Days 13-18: the user's side: errors, sitrep(), long jobs
- Days 19-25: testing and shipping, to CI and CRAN