How to check if object (variable) is defined in R?

前端 未结 6 1406
被撕碎了的回忆
被撕碎了的回忆 2020-11-28 20:02

I\'d like to check if some variable is defined in R - without getting an error. How can I do this?

My attempts (not successful):

> is.na(ooxx)
Err         


        
6条回答
  •  一整个雨季
    2020-11-28 20:38

    If you don't want to use quotes, you can use deparse(substitute()) trick which I found in the example section of ?substitute:

    is.defined <- function(sym) {
      sym <- deparse(substitute(sym))
      env <- parent.frame()
      exists(sym, env)
    }
    
    is.defined(a)
    # FALSE
    a <- 10
    is.defined(a)
    # TRUE
    

提交回复
热议问题