Reputation: 6682
Is there a more elegant (fail-safe/robust, shorter) way of checking whether a dataset (whose name is known as a character string) exists in a package than this?
rda.name <- "Animals" # name of the data set/.rda
rda.name %in% data(package = "MASS")[["results"]][,"Item"]
Upvotes: 0
Views: 401
Reputation: 6682
As mentioned in the comment, I cannot replicate Sven's answer (under any recent version of R). The following works, but the usage of suppressWarnings()
is rather ugly and the dataset is also loaded when calling data()
this way (instead of just checking its existence). As such, I don't think this is preferable over my original version, but perhaps inspires someone to provide a fix.
exists(suppressWarnings(data(list = rda.name, package = "MASS")))
Upvotes: 0
Reputation: 81683
You can try this approach using exists
:
exists(data("Animals", package = "MASS"))
# [1] TRUE
Upvotes: 3