rstruck
rstruck

Reputation: 1294

R : Check if R object exists before creating it

I am trying to skip steps loading data from large files if this has already been done earlier. Since the data ends up in (for example) mydf, I thought I could do:

if( !exists(mydf) )
{
  #... steps to do loading here.
}

I got this from How to check if object (variable) is defined in R? and https://stat.ethz.ch/R-manual/R-devel/library/base/html/exists.html

However R Studio simply complains with

'Error in exists(mydf) : object 'mydf' not found

Why does it complain instead of just returning 'true' or 'false'? Any tips appreciated.

Upvotes: 26

Views: 26740

Answers (1)

Fedorenko Kristina
Fedorenko Kristina

Reputation: 2777

You should use exists("mydf") instead exists(mydf)

Upvotes: 55

Related Questions