BroVic
BroVic

Reputation: 1079

Check for DLL in R session without loading it

How does one check whether a given DLL is found in a given R package (i.e. without loading it as with dyn.load())? Functions like getLoadedDLLs() will show them if the relevant namespaces are loaded. I need to carry out this check without explicitly loading the namespace.

Upvotes: 1

Views: 233

Answers (2)

Rui Barradas
Rui Barradas

Reputation: 76460

The following function finds DLL files given a package name and a DLL name. If the DLL file can be found in several directories in .libPaths() then it will return all of them.

pkg_dll_exists <- function(package, dll){
  res <- lapply(.libPaths(), function(x){
    lib1 <- file.path(x, package, "libs")
    f1 <- list.files(path = lib1, pattern = "\\.dll", recursive = TRUE)
    lib2 <- file.path(x, package, "inst")
    f2 <- list.files(path = lib2, pattern = "\\.dll", recursive = TRUE)
    f1 <- file.path(lib1, f1)
    f2 <- file.path(lib2, f2)
    c(f1, f2)
  })
  res <- unlist(res)
  list(dll.exists = any(grepl(dll, res)), dll.file = res[grep(dll, res)])
}

pkg_dll_exists("foreign", "foreign.dll")
#$dll.exists
#[1] TRUE
#
#$dll.file
#[1] "C:/Program Files/R/site-library/foreign/libs/i386/foreign.dll"   
#[2] "C:/Program Files/R/site-library/foreign/libs/x64/foreign.dll"    
#[3] "C:/Program Files/R/R-4.0.2/library/foreign/libs/i386/foreign.dll"
#[4] "C:/Program Files/R/R-4.0.2/library/foreign/libs/x64/foreign.dll"

Upvotes: 1

Allan Cameron
Allan Cameron

Reputation: 173928

If you want to check for a given dll, then you will know the name of the package and its relative location within the package directory. You can therefore use file.exists:

package         <- "Rcpp"
path_in_package <- "libs/x64/Rcpp.dll"
file.exists(paste(.libPaths(), package, path_in_package, sep = "/"))
#> [1] TRUE

and

getDLLRegisteredRoutines(paste(.libPaths(), package, path_in_package, sep = "/"))
                            .Call .Call.numParameters                 .External .External.numParameters
1                     Class__name                   1         CppMethod__invoke                      -1
2  Class__has_default_constructor                   1    CppMethod__invoke_void                      -1
3              CppClass__complete                   1 CppMethod__invoke_notvoid                      -1
4               CppClass__methods                   1   InternalFunction_invoke                      -1
5             CppObject__finalize                   2            Module__invoke                      -1
6            Module__classes_info                   1        class__newInstance                      -1
7                Module__complete                   1      class__dummyInstance                      -1
8               Module__get_class                   2                                                  
9               Module__has_class                   2                                                  
10           Module__has_function                   2                                                  
11        Module__functions_arity                   1                                                  
12        Module__functions_names                   1                                                  
13                   Module__name                   1                                                  
14           Module__get_function                   2                                                  
15                 get_rcpp_cache                   0                                                  
16            rcpp_error_recorder                   1                                                  
17       as_character_externalptr                   1                                                  
18                  CppField__get                   3                                                  
19                  CppField__set                   4                                                  
20              rcpp_capabilities                   0                                                  
21             rcpp_can_use_cxx0x                   0                                                  
22             rcpp_can_use_cxx11                   0                                                  
23          getRcppVersionStrings                   0   

Upvotes: 2

Related Questions