0

The following function gives all the structures of an object in R. This function (plus dput()/str() for object attributes) fully qualifies everything about an arbitrary R object.

ObjectStructure <- function(x, ShowAll=FALSE) { 
    op <- options("warn")
    options(warn=-1)  # Assign "-1" to warn option to temporarily close warnings
    on.exit(options(op)) # Reset the settings to initial ones after exit from the ObjectStructure function
    is.Functions <- grep(methods(is), pattern="<-", invert=TRUE, value=TRUE) # 55 (is.X) functions
    isDotlessFunctions <- character()
    packs <- c('base', 'utils', 'methods') # include more packages if needed
    for (pkg in packs) {
        library(pkg, character.only = TRUE)
        objects <- grep("^is.+\\w$", ls(envir=as.environment(paste('package', pkg, sep=':'))), value=TRUE)
        objects <- grep("<-", objects, invert=TRUE, value=TRUE)
        if (length(objects) > 0) 
          isDotlessFunctions <- append(isDotlessFunctions, objects[sapply(objects, function(x) class(eval(parse(text=x))) == "function")])
      }

    FunctionsList <- union(is.Functions, isDotlessFunctions)
    result <- data.frame(test=character(), value=character(), warn=character(), stringsAsFactors=FALSE)

    # Loop all the "is.(...)" functions and save the results
    for(islev in FunctionsList) {
    res <- try(eval(call(islev,x)),silent=TRUE) # In cases of error, let error be processed
    # in errored cases, try produces try-error object that contains error message
    if(class(res)=="try-error") { next() # In case of error, ignore current iteration and pass to the next iteration in the loop
    } else if (length(res)>1) {
          warn <- "*Applies only to the first element of the provided object"
          value <- paste(res,"*",sep="")
        } else {
          warn <- ""
          value <- res
        }
        result[nrow(result)+1,] <- list(islev, value, warn)
      }  
      result <- result[order(result$value, decreasing=TRUE),] # Order the results
      rownames(result) <- NULL # to arrange row numbers in a way that they start from 1 and ordered
      if(ShowAll) return(result) # Show only the structures that give TRUE
      else return(result[which(result$value=="TRUE"),]) # All the function results that give a TRUE/FALSE value
    }

ObjectStructure(1L, TRUE) # See how the function works 

As the user @dominic-comtois emphasize here, more packages can be included in packs variable. So,

What are the packages that include internal is.X (is.vector, is.numeric, etc.) and all isX (isS4, isOpen etc.) functions in general?

Erdogan CEVHER
  • 1,788
  • 1
  • 21
  • 40
  • Does this not mean to crawl through all CRAN code and look for `isX`? – zx8754 Jul 24 '18 at 08:11
  • 1
    Nope, I think. I do not want `isX` functions in user-added packages in CRAN, but rather `isX` functions that put into R by R Core Team. i.e., `isX` functions that are rather internal to R. – Erdogan CEVHER Jul 24 '18 at 08:14
  • that plays an important role as in the `isX` funcitons of the packages `base`, `utils`, `methods`. – Erdogan CEVHER Jul 24 '18 at 08:18

0 Answers0