How to unload a package without restarting R

512    Asked by AmandaHawes in Devops , Asked on Jun 14, 2021

How can I unload a package without restarting R?

I tried library but it doesn’t show any options that would unload the package.

I also tried detach but it also failed:

detach(ans)
Error in detach(ans) : invalid name argument
detach("ans")
Error in detach("ans") : invalid name argument
So How can I perform this task?

Answered by ajith Jayaraman

To solve r unload library, Try this using ?detach:

detach("package:ans", unload=TRUE)
Alternatively you can also use unloadNamespace command
unloadNamespace("ans")
For multiple versions of a package loaded at once use
detach_package <- function(pkg, character.only = FALSE)
{
 if(!character.only)
{
  pkg <- deparse(substitute(pkg))
}
search_item <- paste("package", pkg, sep = ":")
while(search_item %in% search())
 {
   detach(search_item, unload = TRUE, character.only = TRUE)
 }
}
In your case either use
detach_package(ans)
or use
detach_package("ans", TRUE)
Thank you for reading my answer.

Your Answer

Interviews

Parent Categories