Check if data frame exists

Viewed 35752

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
3 Answers
Related