Skip to content

Instantly share code, notes, and snippets.

@Krewn
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save Krewn/732964a9c02336aeb4ff to your computer and use it in GitHub Desktop.

Select an option

Save Krewn/732964a9c02336aeb4ff to your computer and use it in GitHub Desktop.
Check to see if an array element exists and returning a boolean.
newPointer=function(inputValue){ # see : http://www.stat.berkeley.edu/~paciorek/computingTips/Pointers_passing_reference_.html
object=new.env(parent=globalenv())
object$value=inputValue
class(object)='pointer'
return(object)
}
exDb <- function(t,i,v = F){#Check if element i exists in t
r = F
tryCatch({
temp <- newPointer(t[[i]]) # Attempt to access variable location in question
if(is.null(temp$value)){r = F}else{r = T} #If the prior line of code passes without error it is assured that the value exists but is it null?
}, error = function(e){
if(v){print(paste('Warning:',e))}#Lets you know the error
r = F #sets return value to False
})
return(r)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment