'I can't install packages in R

A few days ago I wanted to install a certain package in R that wasn't available in the older version of R. I installed new version of R and R studio and now there is a problem. I can't install any packages, whenever I try, I get the following error:

Error in install.packages : STRING_ELT() can only be applied to a 'character vector', not a 'NULL'
Error in list.files(skeletonPath) : 
  STRING_ELT() can only be applied to a 'character vector', not a 'NULL'
Error in list.files(skeletonPath) : 
  STRING_ELT() can only be applied to a 'character vector', not a 'NULL'
Error in list.files(.rs.uniqueLibraryPaths(), full.names = TRUE) : 
  STRING_ELT() can only be applied to a 'character vector', not a 'NULL'

I also get the same error when I open R studio for the first time (without the first line of the code above). When I try to install packages manually by going to Tools, I get "R code execution error" message.

I'm not sure what's wrong, I don't understand that error.

Edit: After typing trace(utils:::unpackPkgZip, edit=TRUE) new window with the code opens:

function (pkg, pkgname, lib, libs_only = FALSE, lock = FALSE, 
  quiet = FALSE) 
{
  .zip.unpack <- function(zipname, dest) {
    if (file.exists(zipname)) {
      if ((unzip <- getOption("unzip")) != "internal") {
        system(paste(shQuote(unzip), "-oq", zipname, 
          "-d", dest), show.output.on.console = FALSE, 
          invisible = TRUE)
      }
      else unzip(zipname, exdir = dest)
    }
    else stop(gettextf("zip file %s not found", sQuote(zipname)), 
      domain = NA)
  }
  lib <- normalizePath(lib, mustWork = TRUE)
  tmpDir <- tempfile(, lib)
  if (!dir.create(tmpDir)) 
    stop(gettextf("unable to create temporary directory %s", 
      sQuote(normalizePath(tmpDir, mustWork = FALSE))), 
      domain = NA, call. = FALSE)
  cDir <- getwd()
  on.exit(setwd(cDir))
  on.exit(unlink(tmpDir, recursive = TRUE), add = TRUE)
  res <- .zip.unpack(pkg, tmpDir)
  setwd(tmpDir)
  res <- tools::checkMD5sums(pkgname, file.path(tmpDir, pkgname))
  if (!quiet && !is.na(res) && res) {
    cat(gettextf("package %s successfully unpacked and MD5 sums checked\n", 
      sQuote(pkgname)))
    flush.console()
  }
  desc <- read.dcf(file.path(pkgname, "DESCRIPTION"), c("Package", 
    "Type"))
  if (desc[1L, "Type"] %in% "Translation") {
    fp <- file.path(pkgname, "share", "locale")
    if (file.exists(fp)) {
      langs <- dir(fp)
      for (lang in langs) {
        path0 <- file.path(fp, lang, "LC_MESSAGES")
        mos <- dir(path0, full.names = TRUE)
        path <- file.path(R.home("share"), "locale", 
          lang, "LC_MESSAGES")
        if (!file.exists(path)) 
          if (!dir.create(path, FALSE, TRUE)) 
            warning(gettextf("failed to create %s", 
              sQuote(path)), domain = NA)
        res <- file.copy(mos, path, overwrite = TRUE)
        if (any(!res)) 
          warning(gettextf("failed to create %s", paste(sQuote(mos[!res]), 
            collapse = ",")), domain = NA)
      }
    }
    fp <- file.path(pkgname, "library")
    if (file.exists(fp)) {
      spkgs <- dir(fp)
      for (spkg in spkgs) {
        langs <- dir(file.path(fp, spkg, "po"))
        for (lang in langs) {
          path0 <- file.path(fp, spkg, "po", lang, "LC_MESSAGES")
          mos <- dir(path0, full.names = TRUE)
          path <- file.path(R.home(), "library", spkg, 
            "po", lang, "LC_MESSAGES")
          if (!file.exists(path)) 
            if (!dir.create(path, FALSE, TRUE)) 
              warning(gettextf("failed to create %s", 
                sQuote(path)), domain = NA)
          res <- file.copy(mos, path, overwrite = TRUE)
          if (any(!res)) 
            warning(gettextf("failed to create %s", 
              paste(sQuote(mos[!res]), collapse = ",")), 
              domain = NA)
        }
      }
    }
  }
  else {
    instPath <- file.path(lib, pkgname)
    if (identical(lock, "pkglock") || isTRUE(lock)) {
      lockdir <- if (identical(lock, "pkglock")) 
        file.path(lib, paste0("00LOCK-", pkgname))
      else file.path(lib, "00LOCK")
      if (file.exists(lockdir)) {
        stop(gettextf("ERROR: failed to lock directory %s for modifying\nTry removing %s", 
          sQuote(lib), sQuote(lockdir)), domain = NA)
      }
      dir.create(lockdir, recursive = TRUE)
      if (!dir.exists(lockdir)) 
        stop(gettextf("ERROR: failed to create lock directory %s", 
          sQuote(lockdir)), domain = NA)
      if (file.exists(instPath)) {
        file.copy(instPath, lockdir, recursive = TRUE)
        on.exit({
          if (restorePrevious) {
            try(unlink(instPath, recursive = TRUE))
            savedcopy <- file.path(lockdir, pkgname)
            file.copy(savedcopy, lib, recursive = TRUE)
            warning(gettextf("restored %s", sQuote(pkgname)), 
              domain = NA, call. = FALSE, immediate. = TRUE)
          }
        }, add = TRUE)
        restorePrevious <- FALSE
      }
      on.exit(unlink(lockdir, recursive = TRUE), add = TRUE)
    }
    if (libs_only) {
      if (!file_test("-d", file.path(instPath, "libs"))) 
        warning(gettextf("there is no 'libs' directory in package %s", 
          sQuote(pkgname)), domain = NA, call. = FALSE, 
          immediate. = TRUE)
      for (sub in c("i386", "x64")) if (file_test("-d", 
        file.path(tmpDir, pkgname, "libs", sub))) {
        unlink(file.path(instPath, "libs", sub), recursive = TRUE)
        ret <- file.copy(file.path(tmpDir, pkgname, 
          "libs", sub), file.path(instPath, "libs"), 
          recursive = TRUE)
        if (any(!ret)) {
          warning(gettextf("unable to move temporary installation %s to %s", 
            sQuote(normalizePath(file.path(tmpDir, pkgname, 
              "libs", sub), mustWork = FALSE)), sQuote(normalizePath(file.path(instPath, 
              "libs"), mustWork = FALSE))), domain = NA, 
            call. = FALSE, immediate. = TRUE)
          restorePrevious <- TRUE
        }
      }
      fi <- file.info(Sys.glob(file.path(instPath, "libs", 
        "*")))
      dirs <- row.names(fi[fi$isdir %in% TRUE])
      if (length(dirs)) {
        descfile <- file.path(instPath, "DESCRIPTION")
        olddesc <- readLines(descfile)
        olddesc <- grep("^Archs:", olddesc, invert = TRUE, 
          value = TRUE, useBytes = TRUE)
        newdesc <- c(olddesc, paste("Archs:", paste(basename(dirs), 
          collapse = ", ")))
        writeLines(newdesc, descfile, useBytes = TRUE)
      }
    }
    else {
      ret <- unlink(instPath, recursive = TRUE, force = TRUE)
      if (ret == 0) {
        Sys.sleep(0.5)
        ret <- file.rename(file.path(tmpDir, pkgname), 
          instPath)
        if (!ret) {
          warning(gettextf("unable to move temporary installation %s to %s", 
            sQuote(normalizePath(file.path(tmpDir, pkgname), 
              mustWork = FALSE)), sQuote(normalizePath(instPath, 
              mustWork = FALSE))), domain = NA, call. = FALSE, 
            immediate. = TRUE)
          restorePrevious <- TRUE
        }
      }
      else {
        warning(gettextf("cannot remove prior installation of package %s", 
          sQuote(pkgname)), domain = NA, call. = FALSE, 
          immediate. = TRUE)
        restorePrevious <- TRUE
      }
    }
  }
}

Edit 2: Completely deleting all R versions from computer except for the newest version solved the problem.



Solution 1:[1]

In RStudio you go to Session and do the following things:

  • Clear Workspace
  • Terminate R
  • Restart R

Then close the program and restarted it. Some how that fix the issue.

For the error: "Error listing packages: R code execution error" try the following things:

  • Try restarting R using .rs.restartR()
  • Try removing the packages using remove.packages()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1