ezrudel
ezrudel

Reputation: 13

Can't detach R packages using paste()

I'm working on some functions in an R project for attaching and detaching packages, and I'm having trouble getting paste() to work inside the detach() function (this is eventually going to be part of a for loop):

> library(ggplot2)
> (.packages())
 [1] "ggplot2"   "stringr"   "readr"     "stats"     "graphics" 
 [6] "grDevices" "utils"     "datasets"  "methods"   "base"
> detach(paste("package", "ggplot2", sep = ":"), unload = TRUE)
Error in detach(paste("package", "ggplot2", sep = ":"), unload = TRUE) : 
  invalid 'name' argument

But it works fine if I do this:

> detach("package:ggplot2", unload = TRUE)
> (.packages())
[1] "stringr"   "readr"     "stats"     "graphics"  "grDevices"
[6] "utils"     "datasets"  "methods"   "base"

And paste() is returning the right thing:

> paste("package", "ggplot2", sep = ":")
[1] "package:ggplot2"

So I'm not sure what's going wrong here...

Upvotes: 0

Views: 83

Answers (1)

Kra.P
Kra.P

Reputation: 15123

Try add character.only = TRUE and force = TRUE

detach(paste("package", "ggplot2", sep = ":"), unload = TRUE, character.only = TRUE, force = TRUE)

It may print some warning but detach ggplot2.

Upvotes: 1

Related Questions