Grilled Cheese

ExploreLog inSign up

ᴅʀ. ᴍᴏᴡɪɴᴄᴋᴇʟ'ꜱ

@drmowinckels.io

653 Following3k Followers

Staff scientists / RSE @LCBC_UiO & @LifebrainEU. She/Her.

PhD Cognitive Neurosciences 🧠

@rladies.org Global Team.

#Rstats #Nerd 🌈🏳️‍🌈

https://Drmowinckels.io

PostsRepliesMedia
@drmowinckels.ioDec 5, 2026, 8:02 AM

Need everything a setup script exports? Run it in bash with env at the end and parse the output in R. The repo has a ten-line env_from_script() for it.

Code: drmowinckels.io/advent/2026/...

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

"current" keeps the existing environment and adds or overrides what you list. With system2(), withr::with_envvar() does the same job.

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

R can't source a shell script. And Sys.setenv() leaks into the rest of the user's session. Better: set them for just the one call. processx::run(cmd, args, env = c("current", SUBJECTS_DIR = dir))

@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, 11:26 PM

Always an honor to be mentioned in R Weekly!

RR-Podcast (Eric)@rpodcast.bsky.socialDec 4, 2026, 8:29 PM

Episode 250 (!) of R Weekly Highlights is out! serve.podhome.fm/episodepage/... 🤔 The NOT_CRAN line @drmowinckels.io 📦 covr 4.0.0 @jimhester.bsky.social 🤖 Introducing mordew @colinfay.bsky.social h/t @mike-thomas.bsky.social & Sam Parmar 🙏 #rstats #databs

serve.podhome.fmIssue 2026-W49 HighlightsWhen was the last time you checked your environment files? A cautionary tale reveals the unexpected can happen for a variable like NOT_CRAN. The covr…
@rpodcast.bsky.socialDec 4, 2026, 8:29 PMReposted by @drmowinckels.io

Episode 250 (!) of R Weekly Highlights is out! serve.podhome.fm/episodepage/...

🤔 The NOT_CRAN line @drmowinckels.io
📦 covr 4.0.0 @jimhester.bsky.social
🤖 Introducing mordew @colinfay.bsky.social

h/t @mike-thomas.bsky.social & Sam Parmar 🙏

#rstats #databs

serve.podhome.fmIssue 2026-W49 HighlightsWhen was the last time you checked your environment files? A cautionary tale reveals the unexpected can happen for a variable like NOT_CRAN. The covr…
@drmowinckels.ioDec 4, 2026, 8:39 PM

I don't know how many times I've watched this on repeat. Her control is just so amazing.

youtube.com/shorts/TwvEH...

youtube.comAmazing Violetta Keller 😱❤️ Bayerisches Staatsballett #sobailarinosrenato 👈🏻YouTube video by Vaganova Blog
@drmowinckels.ioDec 4, 2026, 8:03 AM

Nothing found? Fail with a cli error that says exactly which option to set. The option comes first so users can always override what the package guesses.

Code: drmowinckels.io/advent/2026/...

@drmowinckels.ioDec 4, 2026, 8:03 AM

So my find_tool() checks four places, in order: 1. an R option, e.g. options(freesurfer.recon_all = "/path/to/recon-all") 2. an environment variable 3. Sys.which() 4. known install locations, like $FREESURFER_HOME/bin

@drmowinckels.ioDec 4, 2026, 8:03 AM

Sys.which("pandoc") searches your PATH. But GUI apps on macOS often don't inherit the PATH your shell sets up. The tool is installed and R still can't see it.

@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, 1:28 PM

Oh yes! We have all been down this rabbit hole at home point or other. Floating point numbers are no joke!

CCathy Newman ⚜️🐘@cenewman0.bsky.socialDec 2, 2026, 4:18 PM

Why is R lying 😭

Photo, no description
@drmowinckels.ioDec 3, 2026, 1:26 PM

I can't believe I didn't know of it before I was researching this calendar! I'm very much looking forward to exploring refactoring some packages.

@drmowinckels.ioDec 3, 2026, 8:02 AM

Two more worth knowing:
- timeout = to stop a job that hangs
- echo = TRUE to stream output live

For a FreeSurfer job that runs for hours, those matter a lot.

Code: drmowinckels.io/advent/2026/...

@drmowinckels.ioDec 3, 2026, 8:02 AM

What you get: - arguments as a character vector, no shell, nothing to quote - a real R error on a non-zero exit (or error_on_status = FALSE to handle it yourself) - res$stdout and res$stderr as plain strings, ready to parse

@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

Tomorrow: the package I should have been reaching for instead.

Code: drmowinckels.io/advent/2026/day02

@drmowinckels.ioDec 2, 2026, 8:02 AM

The trap: when the command fails, R doesn't throw an error. You get a warning, and the exit code is tucked away in attr(out, "status"). If your wrapper doesn't check it, a failed run looks just like a successful one.

@drmowinckels.ioDec 2, 2026, 8:02 AM

Both print to the console by default and return the exit code, not the text. intern = TRUE in system(), and stdout = TRUE in system2(), are what say "hand me the output as a character vector instead". Forget them and you get a number where you wanted lines.

@drmowinckels.ioDec 2, 2026, 8:02 AM

system() takes a single string and hands it to the shell: system("git --version", intern = TRUE) system2() takes the command and its arguments separately, which is easier to build from R values: system2("git", "--version", stdout = TRUE)

Older posts
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