0

I would like to wrap R::base function exists into a wrapper function to make it more C-based and variadic in nature.

### current examples of `exists` usage

if(!exists(".humanVerse")) { initMemory(); }

#############################

if(!exists("path", .GlobalEnv$.humanVerse) || purge.memory)
        {  
        ### do something here
        }

#############################

if(exists(save.key, .GlobalEnv$.humanVerse[["colors"]][["dataframes"]]))
            {
            return( .GlobalEnv$.humanVerse[["colors"]][["dataframes"]][[save.key]] );
            }

The above examples work fine, in the order applied. But if I wanted to write a function to capture or trap the non-existent key, it will cause a problem.

exists("not.created");

whereas

checkExists = function(x)
    {
    exists(x);  
    }

checkExists(not.created);

throws an object not found error. Of course, I recognize that the "string" form of "not.created" is different than a variadic form.

I am looking for a way for the checkExists function to work.

The x in exists looks for a name, a character vector. What if I want to checkExists on an object, not a character? That is the essence of the variadic question.

Maybe I am merely asking how to capture the character name of the object? I don't know. R::base logic contorts my logic from time to time.

mshaffer
  • 959
  • 1
  • 9
  • 19
  • what if you do `x<-"not.created"` then `checkExists(x)`, what exactly should it check for? `x` or `not.created`? I'm also not sure I understand what you mean by "variadic". Normally that means you can pass any number of arguments to a function but in each of these cases you seem to be passing just one. – MrFlick Mar 06 '21 at 20:56
  • 1
    Yes, how do I cast a potential object `not.created` into a character `"not.created"` so that could occur? – mshaffer Mar 06 '21 at 21:00
  • What are you answering "yes" to? I'm confused. Do you want your function only to take symbol names and never variables? So in the case of `varname <- "not.created"; exists(varname)`, what should it be testing? – MrFlick Mar 06 '21 at 21:02
  • 1
    fun2 <- function (var_pass) { deparse(substitute(var_pass)) } – mshaffer Mar 06 '21 at 21:09
  • 1
    `dir.exists` and `file.exists` behave very different from `exists` ... I get contorted and confused. – mshaffer Mar 06 '21 at 21:10

0 Answers0