Find all occurences of a legacy package (sp) in my R package

30 Views Asked by At

I maintain an R package that partially depends on sp. sp is now retiring, and I need to find all occurrences in the code that use it - to replace with functions from sf.

Finding those where I used sp:: is easy, but I might have forgotten it somewhere, because roxygen2 still tries to load sp when preparing the documentation. Is there a way of finding all occurrences of functions from a certain package in a file or package?

I appreciate your help!

I have manually printed the list of functions from sp but I do not plan to grep them all manually. There must be an easier way ...

1

There are 1 best solutions below

0
Stéphane Laurent On

R CMD check, as proposed by @Carl Witthoft in his comment, surely is the easiest way.

Otherwise, especially if you use RStudio, you can use the findInFiles package. First get all functions of sp:

spfuncs <- ls(envir = asNamespace("sp"))

Then construct a regular expression which matches these function names:

regex <- sprintf("(%s)", paste0(spfuncs, collapse = "|"))

Then, from the R directory, use findInFiles as follows:

findInFiles::findInFiles("R", regex, perl = TRUE)

If you use RStudio, this will display all matches in the viewer pane, with pretty colors.