Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
7 views
in R Programming by (3.5k points)
edited by

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?

1 Answer

0 votes
by (46k points)
edited by

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 this answer.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...