R Studio Package Installation Not Recognized - cannot remove prior installation of package ‘...’

I've installed two packages, "lmtest" and "sandwich". When I try to run a package, R says that there is no package with the name though it has already said that it was successfully downloaded. I've already uninstalled and reinstalled R, I've removed the packaged and reinstalled them and restarted R, closed the notebook and reopened he notebooks -- nothing has worked. I've also checked my firewall and R is let through it.

HELP!


install.packages("lmtest")
install.packages("sandwich")

Installing package into �C:/Users/---/OneDrive/Documents/R/win-library/3.5�
(as �lib� is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/lmtest_0.9-36.zip'
Content type 'application/zip' length 359101 bytes (350 KB)
downloaded 350 KB

package ‘lmtest’ successfully unpacked and MD5 sums checked
Warning in install.packages :
cannot remove prior installation of package ‘lmtest’

The downloaded binary packages are in
C:\Users---\AppData\Local\Temp\RtmpGaJ4vT\downloaded_packages
Installing package into �C:/Users/Emily/OneDrive/Documents/R/win-library/3.5�
(as �lib� is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/sandwich_2.4-0.zip'
Content type 'application/zip' length 1315938 bytes (1.3 MB)
downloaded 1.3 MB

package ‘sandwich’ successfully unpacked and MD5 sums checked
Warning in install.packages :
cannot remove prior installation of package ‘sandwich’

The downloaded binary packages are in
C:\Users---\AppData\Local\Temp\RtmpGaJ4vT\downloaded_packages
C:\Users---\AppData\Local\Temp\RtmpGaJ4vT\downloaded_packagesal\Temp\RtmpGaJ4vT\downloaded_packagesly\AppData\Local\Temp\RtmpGaJ4vT\downloaded_packagesta\Local\Temp\RtmpGaJ4vT\downloaded_packages

sandwich::standwich(bptest(mod.h))

Error in loadNamespace(name) : there is no package called ‘sandwich’

sandwich::bptest(mod.h)

Error in loadNamespace(name) : there is no package called ‘sandwich’

bptest(mod.h)

Error in bptest(mod.h) : could not find function "bptest"

1 Like

It looks like the library folders are locked, and can't be overwritten.

The key pieces of information, to me, are

Installing package into �C:/Users/---/OneDrive/Documents/R/win-library/3.5�

and

cannot remove prior installation of package ‘lmtest’

In my experience, when this happens, I usually go to C:/Users/---/OneDrive/Documents/R/win-library/3.5, delete the lmtest folder. With the folder deleted, install.packages should be able to write the package.

There's probably a more elegant way to do this, but that's the solution I've been using.

1 Like

Hi, thanks for the help! I really appreciate it.

so when I did this:

library(sandwich)
(this message popped up): Error in loadNamespace(name) : there is no package called ‘sandwich’

that happened but when I ran the tests I needed such as the bptest and coeftest, they ran. So clearly it's running tests but do you know if I should be concerned about the previous "there is no package called 'sandwich'" message?

Will the test still normally if I close R and reopen it later?

Try to re-install the packages with administrative privilege:

  • close RStudio
  • right click on a R (not RStudio) shortcut
  • select "Run as administrator"
  • re-install both packages (same command you used before in RStudio)
  • close R
  • re-open RStudio

Note: I tried to reply by email a while ago, but either there is a very long delay or something went wrong. But a duplicate of this reply might popup at some point.

Yes: your solution is a workaround: since you don't have the right privileges to write in your library from within R, you do it manually. With administrative privilege, this should work without having to go edit your library manually. Working from within R with the right privileges is a lot cleaner because R will take care of dependencies properly.

I would :smile: You might have only manually deleted lmtest, but not sandwich? In any case, I would try to reinstall both as I suggested.

HI, I uninstalled and reinstalled R and R Studio then did as you suggested. When I reran my bptest, it said that there was not such thing as a bptest. When I ran "library(sandwich)" it said there was not such thing as sandwich.

bptest(log.mod)

Error in bptest(log.mod) : could not find function "bptest"

library(sandwich)

Error in library(sandwich) : there is no package called ‘sandwich’

But when I did the other thing and deleted the file "lmtest", everything worked. And I think the file may popped back up again after I reinstalled the packages (I left the file explorer page open and saw it had returned when I wet to closed everything out and uninstall R and R Studio).

So what should I do now? I'm confused about what's happening with my computer and R and R Studio. Thanks

It sounds like you need to install sandwich. After that you can load the package, and use the functions it contains.

install.packages("sandwich")
library(sandwich)

I'm not familiar with the package, but, from looking at its docs on CRAN, it doesn't look like bptest() is a function in the package, so:

sandwich::bptest(foo)

won't work even with the package installed and attached.

It looks like there's a function bgtest() in the lmtest package, but I'm not sure if that's the one you're after.

One of the most helpful skills to hone as you write code is a keen eye for details— e.g. typos like

sandwich::standwich(bptest(mod.h))

can leave you scratching your head about all sorts of installation problems etc., when really you've got an errant t that's snuck into s**t**andwich!

I've installed both together (several times). It said:

install.packages(lmtest)
intsall.packages(sandwich)

and then tried running my bptests and coeftests. The problem now is that after installing it, the functions I need to use that should have been installed with the packages aren't recognized, even when I have done:

sandwich::sandwich(bptest(mod.h))
lmtest::lmtest(bptest(mod.h))
.
etc.
.
bptest(mod.h)

The first solution worked but the packages I installed weren't recognized if I said "library(sandwich)" or "library(lmtest)". So I uninstalled and installed R and tried the second solution. That didn't work and the test I wanted to run weren't tests that R recognized. Even though the first solution offered work, but I was worried that I didn't know exactly what I was doing, that the packages were recognize, and what type of problems it could cause later down the road.

Could you please turn this into a self-contained reprex (short for minimal reproducible example)? It will help us help you if we can be sure we're all working with/looking at the same stuff.

Right now the best way to install reprex is:

# install.packages("devtools")
devtools::install_github("tidyverse/reprex")

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page. The reprex dos and don'ts are also useful.

If you run into problems with access to your clipboard, you can specify an outfile for the reprex, and then copy and paste the contents into the forum.

reprex::reprex(input = "fruits_stringdist.R", outfile = "fruits_stringdist.md")

For pointers specific to the community site, check out the reprex FAQ, linked to below.

HI Hi -- Thanks for the help. For some strange reason, it started working. I think it probably had something to do with restarting things. I can't be certain but my problem has been solved but I'll look into learning about reprex for future reference.

2 Likes

Awesome this worked.