runsusingpackagesonly: Check that function runs using objects only in its argument...

View source: R/check_data_prep_function.R

runsusingpackagesonlyR Documentation

Check that function runs using objects only in its argument list or imported from currently loaded packages

Description

Check that function runs using objects only in its argument list or imported from currently loaded packages

Usage

runsusingpackagesonly(fun, args, erroronfalse = TRUE)

Arguments

fun

The function to check

args

An argument list to pass to fun like in do.call().

Value

TRUE if the functions runs in the parent environment of the global environment (an environment with only packages) with arguments attached, which means the function runs when it can't access the interactively defined objects. Or FALSE if arguments are missing, the function tries to access an object in the global environment (and not in the parent environment), or if there is some other problem with the function executing. In this case a warning is given. This warning contains the error condition that created the failure.

Examples

myfun <- function(B, sd){
  out <- (B - cmns) * sd
  return(out)
}
runsusingpackagesonly(myfun, args = list(sd = 3)) #returns FALSE
B <- matrix(rnorm(4 * 3), nrow = 3)
runsusingpackagesonly(myfun, args = list(sd = 3, B = B)) #returns FALSE
cmns <- colMeans(B) 
runsusingpackagesonly(myfun, args = list(sd = 3, B = B)) #returns FALSE too - myfun run such that it can't see globalenv()
    
myfun2 <- function(B, sd, cmns){
  out <- (B - cmns) * sd
  return(out)
}
runsusingpackagesonly(myfun2, args = list(sd = 3, B = B, cmns = cmns)) # TRUE, which is correct! :)

sustainablefarms/sflddata documentation built on April 19, 2022, 11:19 a.m.