detach all packages while working in R

后端 未结 10 1788
猫巷女王i
猫巷女王i 2020-11-28 19:23

While working to solve another problem I got this problem:

I can remove all R objects by:

rm(list = ls(all = TRUE))

Is there equiv

相关标签:
10条回答
  • Most of the times its the plyr vs dplyr issue. Use this in the beginning of the code:

    detach("package:plyr", unload=TRUE)
    

    So whenever the script runs, its clears the plyr package

    0 讨论(0)
  • 2020-11-28 20:16

    Combining bits from various answers gave the most robust solution I could find...

    packs <- c(names(sessionInfo()$otherPkgs), names(sessionInfo()$loadedOnly))
    if(length(packs) > 0){ 
      message('Unloading packages -- if any problems occur, please try this from a fresh R session')
      while(length(packs) > 0){
        newpacks <- c()
        for(packi in 1:length(packs)){
          u=try(unloadNamespace(packs[packi]))
          if(class(u) %in% 'try-error') newpacks <- c(newpacks,packs[packi])
        }
        packs <- newpacks
        Sys.sleep(.1)
      }
    }
    
    0 讨论(0)
  • 2020-11-28 20:21

    You were close. Note what ?detach has to say about the first argument name of detach():

    Arguments:

    name: The object to detach.  Defaults to ‘search()[pos]’.  This can
          be an unquoted name or a character string but _not_ a
          character vector.  If a number is supplied this is taken as
          ‘pos’.
    

    So we need to repeatedly call detach() once per element of pkg. There are a couple of other arguments we need to specify to get this to work. The first is character.only = TRUE, which allows the function to assume that name is a character string - it won't work without it. Second, we also probably want to unload any associated namespace. This can be achieved by setting unload = TRUE. So the solution is, for example:

    pkg <- c("package:vegan","package:permute")
    lapply(pkg, detach, character.only = TRUE, unload = TRUE)
    

    Here is a full example:

    > require(vegan)
    Loading required package: vegan
    Loading required package: permute
    This is vegan 2.0-0
    > sessionInfo()
    R version 2.13.1 Patched (2011-09-13 r57007)
    Platform: x86_64-unknown-linux-gnu (64-bit)
    
    locale:
     [1] LC_CTYPE=en_GB.utf8       LC_NUMERIC=C             
     [3] LC_TIME=en_GB.utf8        LC_COLLATE=en_GB.utf8    
     [5] LC_MONETARY=C             LC_MESSAGES=en_GB.utf8   
     [7] LC_PAPER=en_GB.utf8       LC_NAME=C                
     [9] LC_ADDRESS=C              LC_TELEPHONE=C           
    [11] LC_MEASUREMENT=en_GB.utf8 LC_IDENTIFICATION=C      
    
    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods  
    [7] base     
    
    other attached packages:
    [1] vegan_2.0-0   permute_0.7-0
    
    loaded via a namespace (and not attached):
    [1] grid_2.13.1     lattice_0.19-33 tools_2.13.1   
    > pkg <- c("package:vegan","package:permute")
    > lapply(pkg, detach, character.only = TRUE, unload = TRUE)
    [[1]]
    NULL
    
    [[2]]
    NULL
    
    > sessionInfo()
    R version 2.13.1 Patched (2011-09-13 r57007)
    Platform: x86_64-unknown-linux-gnu (64-bit)
    
    locale:
     [1] LC_CTYPE=en_GB.utf8       LC_NUMERIC=C             
     [3] LC_TIME=en_GB.utf8        LC_COLLATE=en_GB.utf8    
     [5] LC_MONETARY=C             LC_MESSAGES=en_GB.utf8   
     [7] LC_PAPER=en_GB.utf8       LC_NAME=C                
     [9] LC_ADDRESS=C              LC_TELEPHONE=C           
    [11] LC_MEASUREMENT=en_GB.utf8 LC_IDENTIFICATION=C      
    
    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods  
    [7] base     
    
    loaded via a namespace (and not attached):
    [1] grid_2.13.1     lattice_0.19-33 tools_2.13.1
    

    If you want to turn this into a function, study the code in sessionInfo() to see how it identifies what it labels as "other attached packages:". Combine that bit of code with the idea above in a single function and you are home and dry. I'll leave that bit up to you though.

    0 讨论(0)
  • 2020-11-28 20:25

    if you're having problems with packages that have similarly named functions conflicting with each other, you can always reference the namespace of the package who's function you DO want.

    pkg_name::function_i_want()
    
    0 讨论(0)
提交回复
热议问题