If you are running spatial geocoding or heavy data pipelines in R on macOS using packages like geocodebr, duckdb, or callr, you might run into a perplexing IO error. The pipeline suddenly crashes with an error message that looks like this:

! Invalid Error: IO Error: Failed to create directory "/var/folders/.../T//Rtmp7gZjsr/duckdb/temp": No such file or directory
ℹ Context: rapi_execute
ℹ Error type: INVALID

This issue frequently appears on macOS when background R processes spawned by callr attempt to write temporary DuckDB database files to an R temporary directory (RtmpXXXXXX) that either no longer exists, lacks parent permissions, or contains corrupted file path references.

Why Does This DuckDB IO Error Occur?

When running functions in parallel or in isolated sub-sessions via callr, R creates a isolated child session. DuckDB tries to initialize a temporary workspace inside R's default temporary folder (tempdir()). On macOS, R's tempdir() points deep inside the system's /var/folders/ directory.

There are three main reasons this directory creation fails:

  • Isolated Subprocess Paths: The callr subprocess inherits an environment or path string reference to a parent process's Rtmp folder that has already been cleaned up or deleted.
  • Malformed Path Slashes: Double slashes (like /T//Rtmp...) generated during path concatenation can cause file system path creation to fail in underlying C++ libraries on macOS.
  • macOS Privacy & Permissions: macOS security features or automatic temporary folder purging utilities can invalidate short-lived temporary paths mid-execution.

How to Fix the Issue

Solution 1: Override the Temp Directory in your R Session

The cleanest fix is to direct DuckDB and R subprocesses to use a custom, persistent temporary folder outside of macOS's volatile /var/folders/ directory. You can set this near the top of your R script before executing your geocoding or DuckDB functions:

# Create a dedicated temp directory for DuckDB operations
custom_temp <- file.path(Sys.getenv("HOME"), "R_duckdb_temp")
if (!dir.exists(custom_temp)) {
  dir.create(custom_temp, recursive = TRUE)
}

# Set system environment variables so child processes inherit it
Sys.setenv(TMPDIR = custom_temp)
Sys.setenv(TMP = custom_temp)
Sys.setenv(TEMP = custom_temp)

# Now run your geocodebr / callr execution
df_chunk <- geocodebr::geocode(
  enderecos = chunk,
  campos_endereco = campos,
  resultado_completo = FALSE,
  resolver_empates = TRUE,
  resultado_sf = FALSE,
  verboso = FALSE
)

Solution 2: Configure Environment Variables via .Renviron

If the error persists across sessions or inside background tasks, set the temporary path permanently in your user-level .Renviron file. This ensures every R process—and any subprocess launched by callr—uses a stable directory.

  1. Open your .Renviron file in RStudio or R using usethis::edit_r_environ().
  2. Add the following line pointing to a directory in your user folder:
TMPDIR=/Users/YOUR_USERNAME/tmp

Replace YOUR_USERNAME with your actual macOS username. Save the file, restart R, and execute your code again.

Solution 3: Update duckdb and geocodebr Packages

Driver bugs and path-handling issues in DuckDB's C++ interface are actively fixed by maintainers. Make sure you are running the latest binaries rather than an older cached version.

# Reinstall duckdb and geocodebr from CRAN or GitHub
install.packages("duckdb", type = "binary")
install.packages("geocodebr")

Summary

The IO Error: Failed to create directory error occurs when duckdb attempts to build temporary directories inside a non-existent or inaccessible Rtmp path passed down through callr. Pointing your environment's TMPDIR variable to a dedicated, stable folder in your user home directory resolves this issue on macOS reliably.