2

A very common question on StackOverflow with regards to C++/R or C/R package integration is regarding the error in dyn.load(), e.g.

> ## within R
> Error in .Call("function_c") : C symbol name "function_c" not in load table

whereby function_c is some function in C like

SEXP function_c() {
  Rprintf("Hello World!\n"); // manually changed
  return(R_NilValue);
}

This error come sup due to many types of mistakes, e.g. incorrect compliation, misnamed functions, the user didn't use extern "C" for Cpp code, etc.

Question: Is there any way to view all "available" objects which the user could load via dyn.load() after compilation?

ShanZhengYang
  • 16,511
  • 49
  • 132
  • 234

1 Answers1

1

How about the following? I'm not sure it covers everything, but it should be close:

# manipulate search() to get all loaded packages
loadedPkgs = grep('^package:', search(), value = TRUE)
loadedPkgs = gsub('package:', '', loadedPkgs, fixed = TRUE)
# add names here to make the results of lapply pretty
names(loadedPkgs) = loadedPkgs

allCRoutines = lapply(loadedPkgs, function(pkg) {
  # see: https://stackoverflow.com/questions/8696158/
  pkg_env = asNamespace(pkg)
  # this works at a glance
  check_CRoutine = function(vname) {
    'CallRoutine' %in% attr(get(vname, envir = pkg_env), 'class')
  }
  names(which(sapply(ls(envir = pkg_env, all = TRUE), check_CRoutine)))
})

The object is a bit long, so I'll just show for one package:

allCRoutines[['utils']]
# $utils
#  [1] "C_crc64"         "C_flushconsole"  "C_menu"          "C_nsl"           "C_objectSize"    "C_octsize"       "C_processevents"
#  [8] "C_sockclose"     "C_sockconnect"   "C_socklisten"    "C_sockopen"      "C_sockread"      "C_sockwrite"  

What I'm not sure of is that check_CRoutine catches everything we'd consider as relevant to your question. I'm also not sure this covers your main interest (whether these objects can succesfully be fed to dyn.load); perhaps the routines returned here could be passed to dyn.load with a try wrapper?

MichaelChirico
  • 33,841
  • 14
  • 113
  • 198