title: R: Error in as.environment(pos) / Error during wrapup: no item called "package:[package]" on the search list
body: <p>This is similar to <a href="https://community.rstudio.com/t/error-in-as-environment-pos-no-item-called-package-package-on-the-search-list/75585" rel="nofollow noreferrer">this</a> but with a followup question:</p>
  <p>I'm preparing my package for submission to CRAN. In one script, it gives the titular error:</p>
  <pre><code>Error in as.environment(pos): no item called &quot;package:shapefiles&quot; on the search list
  </code></pre>
  <p>or</p>
  <pre><code>Error during wrapup: no item called &quot;package:shapefiles&quot; on the search list
  </code></pre>
  <p>If, like the similar thread linked above, I add</p>
  <p><code>attachNamespace(&quot;shapefiles&quot;)</code></p>
  <p>Then I no longer get that error, but instead I sometimes get the error or warning:</p>
  <pre><code>namespace(shapefiles) was already taken
  </code></pre>
  <p>I believe this happens if the function/script is run more than once. What's so weird about this to me is that</p>
  <pre><code>#' @import shapefiles
  </code></pre>
  <p>is in that script's markdown block,</p>
  <p>Which means</p>
  <pre><code>import(shapefiles)
  </code></pre>
  <p>is in NAMESPACE, and</p>
  <pre><code>Imports:
      shapefiles (&gt;= 0.7),
  </code></pre>
  <p>is in DESCRIPTION. I have numerous other package dependencies which are imported the same way (correctly, I believe), and none of them produce this error. I don't see why this is behaving differently therefore, and would like to avoid having either issue before submitting to CRAN. The code that calls this package, in my script, is</p>
  <pre><code>shapefiles::read.shapefile(savename)
  </code></pre>
  <p>Script is <a href="https://github.com/SimonDedman/gbm.auto/blob/master/R/gbm.basemap.R" rel="nofollow noreferrer">here</a>, for reference.</p>
  <p>Thanks in advance for any ideas!</p>
  <p>Edit: steps to reproduce:</p>
  <pre><code>setwd(&quot;/folder&quot;)
  library(devtools)
  install_github(&quot;SimonDedman/gbm.auto&quot;)
  library(gbm.auto)
  gbm.basemap(bounds = c(-81.7, -80.3, 24.7, 25.9),
              savedir = &quot;/folder&quot;)
  </code></pre>
  <p>I've just removed <code>shapefiles::</code> from the code. The correct output is produced regardless.</p>