dardisco
dardisco

Reputation: 5274

Access (exporting) function from namespace

I know something similar has been asked before here on SO, but the solution given there doesn't seem to apply in my case. I'm trying to follow convention in creating a package by referring to functions exported from other namespaces and avoiding use of require() within a function. I'm basically trying to prevent a function taking too long to run. For example,

fun <- function(i){
require(R.utils)
setTimeLimit(elapsed=10, transient=TRUE) # prevent taking more than 10secs
 return(i^i)
 }
>fun(10)

Works fine, but if I try:

require(R.utils)
fun <- function(i){
R.utils:::setTimeLimit(elapsed=10, transient=TRUE) # prevent taking more than 10secs
 return(i^i)
 }
>fun(10)

I get:

Error in get(name, envir = asNamespace(pkg), inherits = FALSE) : 
object 'setTimeLimit' not found

Changing ::: to :: doesn't change this behavior. I'm open to any simpler methods to achieving the same objective. Also is it really so bad to have require() calls inside a function? Many thanks!

EDIT: If import works then great, thanks. Still in development so wanted to make sure it would be OK.

EDIT: Apologies, it's there in base. Not sure how I missed this; I was originally using R.utils::evalWithTimeout and must have assumed both were in the same package. *looks sheepish*

Upvotes: 2

Views: 417

Answers (1)

dardisco
dardisco

Reputation: 5274

I'm just posting this to prevent the question from showing up as unanswered, but will be glad to accept another...

isTRUE("setTimeLimit" %in% ls(getNamespace("base"), all.names=TRUE))

Upvotes: 1

Related Questions