getPackages <- function(packs){
packages <- unlist(
# Find (recursively) dependencies or reverse dependencies of packages.
tools::package_dependencies(packs, available.packages(),
which=c("Depends", "Imports"), recursive=TRUE)
)
packages <- union(packs, packages)
return(packages)
}
packages <- getPackages(c("dplyr", "ggplot2"))
print(packages)