Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You were close. Note what <code>?detach</code> has to say about the first argument <code>name</code> of <code>detach()</code>:</p> <blockquote> <p>Arguments:</p> <pre><code>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’. </code></pre> </blockquote> <p>So we need to repeatedly call <code>detach()</code> once per element of <code>pkg</code>. There are a couple of other arguments we need to specify to get this to work. The first is <code>character.only = TRUE</code>, which allows the function to assume that <code>name</code> 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 <code>unload = TRUE</code>. So the solution is, for example:</p> <pre><code>pkg &lt;- c("package:vegan","package:permute") lapply(pkg, detach, character.only = TRUE, unload = TRUE) </code></pre> <p>Here is a full example:</p> <pre><code>&gt; require(vegan) Loading required package: vegan Loading required package: permute This is vegan 2.0-0 &gt; 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 &gt; pkg &lt;- c("package:vegan","package:permute") &gt; lapply(pkg, detach, character.only = TRUE, unload = TRUE) [[1]] NULL [[2]] NULL &gt; 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 </code></pre> <p>If you want to turn this into a function, study the code in <code>sessionInfo()</code> 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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload