简体   繁体   中英

R: How to check if the libraries that I am loading, I use them for my code in R?

I have used several packages of R libraries for my study. All libraries charge together at the beginning of my code. And here is the problem. It turns out that I have done several tests with different functions that were already in the packages of R. However, in the final code I have not implemented all the functions I have tried. Therefore, I am loading libraries that I do not use.

Would there be any way to check the libraries to know if they really are necessary for my code?

Start by restarting R with a fresh environment, no libraries loaded. For this demonstration, I'm going to define two functions:

zoo1 <- function() na.locf(1:10)
zoo2 <- function() zoo::na.locf(1:10)

With no libraries loaded, let's try something:

codetools::checkUsage(zoo1)
# <anonymous>: no visible global function definition for 'na.locf'
codetools::checkUsage(zoo2)
library(zoo)
# Attaching package: 'zoo'
# The following objects are masked from 'package:base':
#     as.Date, as.Date.numeric
codetools::checkUsage(zoo1)

Okay, so we know we can check a single function to see if it is abusing scope and/or using non-base functions. Let's assume that you've loaded your script full of functions (but not the calls to require or library ), so let's do this process for all of them. Let's first unload zoo , so that we'll see a complaint again about our zoo1 function:

detach("package:zoo", unload=TRUE)

Now let's iterate over all functions:

allfuncs <- Filter(function(a) is.function(get(a)), ls())
str(sapply(allfuncs, function(fn) capture.output(codetools::checkUsage(get(fn))), simplify=FALSE))
# List of 2
#  $ zoo1: chr "<anonymous>: no visible global function definition for 'na.locf'"
#  $ zoo2: chr(0) 

Now you know to look in the function named zoo1 for a call to na.locf . It'll be up to you to find in which not-yet-loaded package this function resides, but that might be more more reasonable, depending on the number of packages you are loading.

Some side-thoughts:

  1. If you have a script file that does not have everything comfortably ensconced in functions, then just wrap all of the global R code into a single function, say bigfunctionfortest <- function() { as the first line and } as the last. Then source the file and run codetools::checkUsage(bigfunctionfortest) .

  2. Package developers have to go through a process that uses this, so that the Imports: and Depends: sections of NAMESPACE (another ref: http://r-pkgs.had.co.nz/namespace.html ) will be correct. One good trick to do that will prevent "namespace pollution" is loading the namespace but not the package ... and though that may sound confusing, it often results in using zoo::na.locf for all non-base functions. This gets old quickly (especially if you are using dplyr and such, where most of your daily functions are non-base), suggesting those oft-used functions should be directly imported instead of just referenced wholly. If you're familiar with python, then:

     # R library(zoo) na.locf(c(1,2,NA,3)) 

    is analagous to

     # fake-python from zoo import * na_locf([1,2,None,3]) 

    (if that package/function exists). Then the non-polluting variant looks like:

     # R zoo::na.locf(c(1,2,NA,3)) # fake-python import zoo zoo.na_locf([1,2,None,3]) 

    where the function's package (and/or subdir packaging) must be used explicitly. There is no ambiguity. It is explicit. This is by some/many considered "A Good Thing (tm)".

    (Language-philes will likely say that library(zoo) and from zoo import * are not exactly the same ... a better way to describe what is happening is that they bring everything from zoo into the search path of functions, potentially causing masking as we saw in a console message earlier; while the :: functionality only loads the namespace but does not add it to the search path. Lots of things going on in the background.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM