Check if data frame exists

dataframe, r

Solution

The second option can be shortened to

exists(df_name) && is.data.frame(get(df_name))

The operator `&&` allows lazy evaluation, i.e., the second statement is only evaluated if the first one returns `TRUE`.

Problem

What is the preferred way to check that data frame exists given you have data frame name as string? I can think of: ``` df_name <- 'iris' # Option 1 tryCatch(is.data.frame(get(df_name)), error=function(cond) FALSE) # Option 2 if (exists(df_name)) is.data.frame(get(df_name)) else FALSE ```

Original source