Skip to content

Instantly share code, notes, and snippets.

@jimbrig
Forked from JosiahParry/read-r-headers.R
Created May 14, 2026 14:22
Show Gist options
  • Select an option

  • Save jimbrig/92f5623bf8a7c9fe35847286eebe0ed0 to your computer and use it in GitHub Desktop.

Select an option

Save jimbrig/92f5623bf8a7c9fe35847286eebe0ed0 to your computer and use it in GitHub Desktop.
Read R’s C headers to identify where R functions are coming from
library(dplyr)
api <- as_tibble(tools:::funAPI())
# list all header files from include dir
header_files <- list.files(
R.home("include"),
full.names = TRUE,
recursive = TRUE,
pattern = "*.h"
)
read_header <- function(.h) {
# find include dir
include_dir <- R.home("include")
# generate the clang ar #thanksChatGPT
system_cmd <- sprintf(
"clang -Xclang -ast-dump=json -fsyntax-only -x c -I%s %s",
include_dir,
.h
)
# parse and read
json <- paste(system(system_cmd, intern = TRUE), collapse = "")
in_header <- yyjsonr::read_json_str(json)[[c("inner", "name")]]
header_fns <- tibble(
name = in_header,
header_path = gsub(paste0(include_dir, "/"), "", .h)
)
header_fns
}
all_api_fns <- lapply(header_files, read_header) |>
bind_rows()
res <- inner_join(api, all_api_fns, by = "name")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment