Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an R environment exists

Tags:

r

I would like to check if an R environment exists, but the standard exists() function doesn't accept an environment as argument:

storage <- new.env(parent = emptyenv())

storage
#<environment: 0xeb3195c>

exists(storage)
#Error in exists(storage) : invalid first argument

Is there any way of checking if an environment has been defined? Or is it an intrinsically wrong thing to do? Thanks!

like image 647
Matteo Fasiolo Avatar asked Dec 03 '13 16:12

Matteo Fasiolo


1 Answers

exists takes a character argument:

exists("storage")

Though note that this will return TRUE even if storage is not an environment. You will either have to check it using is.environment as Señor O suggests, or pass mode="environment":

exists("storage", mode="environment")
like image 163
Peyton Avatar answered Sep 18 '22 06:09

Peyton