"Error in library(rjson): There is no package called rjson"

Jibril picture Jibril · Sep 22, 2015 · Viewed 19.1k times · Source

My rjson package randomly doesn't work. As in, it works fine sometimes, sometimes it fails to load. Not sure why.

I get this error.

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

To try and alleviate this, despite knowing its installed, I added an install line in my script.

   install.packages("rjson", repos="http://cran.rstudio.com/")
   library(rjson)

Now I get....

Installing package(s) into ‘C:/Users/Tom/Documents/R/win-library/2.15’ (as ‘lib’ is unspecified) trying URL 'http://cran.rstudio.com/bin/windows/contrib/2.15/rjson_0.2.13.zip' Content type 'application/zip' length 491848 bytes (480 Kb) opened URL downloaded 480 Kb

package ‘rjson’ successfully unpacked and MD5 sums checked Warning: cannot remove prior installation of package ‘rjson’

The downloaded binary packages are in C:\Users\Tom\AppData\Local\Temp\RtmpiOfTqK\downloaded_packages

In R, when I go to "Packages --> Load" for some reason rjson is NOT there. It never has been, even when it worked.

I've naviaged to...

C:\Users\Tom\Documents\R\win-library\2.15

I can confirm the folder for rjson is there.

No idea what to do.

Answer

LyzandeR picture LyzandeR · Sep 22, 2015

This has happened to me quite a few times. It usually happens when you try to install a newer version of an already installed package (although it can happen in other more rare occasions).

The solution I have found so far is to go back to your library path i.e. the location on your machine where the package is installed (C:\Users\user_name\Documents\R\win-library\R_version is the default path on Windows) delete the corresponding package folder and then re-install the package as usual using:

install.packages('rjson')

And this way it should work.

Or you could even do it programmatically as per @Thomas 's comment:

#get list of installed packages
inst_packages <-  installed.packages()

if ("rjson" %in% inst_packages[, 1]) { 
     #uninstalls package
     remove.packages("rjson")
     #re-installs package
     install.packages("rjson") 
} 

or even better just use:

if ("rjson" %in% inst_packages[, 1]) update.packages("rjson")