30

I'm working in RStudio and am attempting to install the dplyr package. I have installed various other packages without problem but have hit a problem with this.

It seems there are various parts to the install, some of these worked but the problem happens when I get 'the dependency BH'. This is the error:

>Installing package into ‘\\xxxxxxxx.local/Desktops/jagnew/My Documents/R/win-library/3.3’
    (as ‘lib’ is unspecified)
    also installing the dependency ‘BH’
    
>trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.3/BH_1.62.0-1.zip'
    Content type 'application/zip' length 16150075 bytes (15.4 MB)
    downloaded 15.4 MB
    
>trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.3/dplyr_0.5.0.zip'
    Content type 'application/zip' length 2556205 bytes (2.4 MB)
    downloaded 2.4 MB
    
>Error in install.packages : cannot open file '\\xxxxxxx.local/Desktops/jagnew/My Documents/R/win-library/3.3/file142032c9327b/BH/include/boost/function/detail/gen_maybe_include.pl': Permission denied

I have changed the directory/libpath as it did not appear that the file was downloading to the correct folder. file142032c9327b is now in the correct path, but path specified in the error is only correct up to /boost, there is no function folder here..?

I'm not sure if this missing folder is the issue, as the error says 'Permission denied', but it seems odd.

Phil
  • 7,287
  • 3
  • 36
  • 66
agwah
  • 413
  • 1
  • 4
  • 7
  • 4
    I had the same problem and discovered that my antivirus program (Avast) was blocking access to the file. Once I told Avast to allow access everything worked. – Harpagus Aug 20 '17 at 15:18
  • 2
    If R (or RStudio with R) was installed as administrator in Windows and you try to install or update a package you lack of permissions. Either run the installation as admin or avoid the installation as admin. – R Yoda Feb 18 '18 at 10:33

12 Answers12

35

I had the same issue. Tried doing the following and worked for me:

  1. Run the RStudio as administrator
  2. Under the Packages tab, click Install and change Install to Library option to C:/Program...../R/../library and select the desired package to install.

enter image description here

Hope it works.

Shubham Yadav
  • 561
  • 7
  • 16
  • 1
    How do you run RStudio as an administrator? Where is the "Packages" tab? There is no such tab in RStudio 1.4.1103. – Yozef Jan 24 '21 at 18:02
  • I haven't checked the 1.4 version but in 1.3 it is available under Tools->Install Packages. You can run it as admin by right clicking on the exe file and selecting "run as admin..". Let me know if it works. Thanks. – Shubham Yadav Jan 27 '21 at 13:01
  • I've tried running as administrator in R Version 4.2.1 and it simply doesn't give me the second option for Install to Library as you have listed here. Only the default directory is listed. – Shawn Hemelstrand Sep 23 '22 at 19:32
5

I had the same problem. R gave me an error message whenever I tried to install a new, or update an existing package. I don't remember what the message said exactly, but it was saying "Cannot open file (...) access denied". I tried all the following. Not sure what exactly fixed the problem, but now I can update and/or install new packages:

  1. Open R/RStudio as administrator;
  2. Change the R library folder (where packages are installed) security settings, and grant full access to my computer user account. Link1, link2;
  3. (I think this was what fixed the problem) Follow instructions on this link. It prompted me to install another package, "devtools". Then it worked.
franiis
  • 1,378
  • 1
  • 18
  • 33
SenshiKudo
  • 51
  • 1
  • 4
  • 3
    I figured out that it only worked for one package, "lme4". I kept facing the same problem with any other package I wanted to install. However, I found out that the solution was the abovementioned comment " I had the same problem and discovered that my antivirus program (Avast) was blocking access to the file. Once I told Avast to allow access everything worked. – [Harpagus] (https://stackoverflow.com/users/8491365/harpagus)" -- Credit to @Harpagus – SenshiKudo Aug 21 '17 at 07:46
3

Well, I think my problem was a special case, but it might benefit someone. I had the Windows Defender Ransomware Protection set to ON. That was blocking rsession.exe by default. Allowing rsession.exe solved the problem.

Rami Alloush
  • 2,308
  • 2
  • 27
  • 33
  • Check if /path/R.exe is included in Windows Ransomware Protection. Get path from file.path(R.home("bin"), "R") – ishonest Dec 05 '20 at 14:30
3

TL;DR

The "Documents" (or "My Documents") directory is a special location in Windows. I don't know if the system itself processes its permissions differently or if antiviruses target operations inside this directory more strictly, but many R users have issues installing packages in the personal library inside this folder (even if running R as administrator). Other answers haven't worked.

For me, what solved the problem was to put the personal library directly under X:\\Users\username\ instead of under Documents, which is the R default on Windows. To make it permanent, I set a user environment variable R_USER to %USERPROFILE%. Here is a nice guide of how to set environment variables in Windows.

Note: after this change, you may need to move your .Rprofile, .Renviron, etc. files from Documents to the user profile directory.


Details

The default location for R's user "home" directory on Windows is X:\\Users\username\Documents. This is because there is no concept of HOME in Windows, just of a "personal" directory (Documents): https://cran.r-project.org/bin/windows/base/rw-FAQ.html#What-are-HOME-and-working-directories_003f

Therefore, by default, the personal library location is under X:\\Users\username\Documents\R\win-library\X.Y\. You can get the exact location on your machine with this command in the R console:

Sys.getenv('R_LIBS_USER')

I was getting this same cannot open file error when trying to install or update packages, even when running as administrator. I suspected of directory permissions, checked it within the directories' "properties" without luck. Removed the whole R\win-library tree and recreated it and nothing... Finally, I tried to move the personal library location to my users' directory:

.libPaths('X://Users/username/R/win-library/X.Y/')

And it worked!

leogama
  • 898
  • 9
  • 13
1
Warning in install.packages :
  cannot create file '/usr/local/Cellar/r/3.6.2/lib/R/doc/html/packages.html', reason 'Permission denied'
Warning in install.packages :
  cannot update HTML package index

I had the same permission issue when trying to download a pkg--ISLR. Fixed it after following the above answers.

In case, some are new to R studio like me. These were my steps to install a pkg.

  1. Click Terminal next to Console in R studio

  2. Type sudo r, to run r as an administrator

  3. install.packages("ISLR") to install your package

--- Please select a CRAN mirror for use in this session ---
https://cran.rstudio.com/src/contrib/ISLR_1.2.tar.gz
Secure CRAN mirrors

 1: 0-Cloud [https]                   2: Australia (Canberra) [https]
 3: Australia (Melbourne 1) [https]   4: Australia (Melbourne 2) [https]
  1. Pick a number -- a mirror site, then click enter.

Voilà! Package installed.

KailiC
  • 111
  • 1
  • 10
0

I had faced also this issue. The one and only solution that helped me was to turn off my firewall. Then I was able to download all packages.

RickvH
  • 1
0

The problem solved for me after running R in admin mode and disabling Antivirus.

khalid tounoussi
  • 499
  • 5
  • 21
0

Translating @Shubham Yadav's answer to code mode would be something like this: install.packages("your_package", lib = "C:/Program Files/R/R-4.0.3/library")

It's working for me (running RStudio as admin, course).

jgarces
  • 519
  • 5
  • 17
0

This procedure worked for me.

  • Removed existing package with remove.packages()
  • ran RStudio as an admin
  • Installed by specifying the libpath: devtools::install_github('https://github.com/Rdatatable/data.table/tree/frollmax', lib=.libPaths()[2])
gaut
  • 5,771
  • 1
  • 14
  • 45
0

For me what worked was manually deleting within the library directory the folder that I had problem with updating. And then install.package() again.

keremw
  • 11
  • 2
-2

I had faced the same issue. I tried below steps to resolve this issue:

  • Launch RGui application.
  • Go to Packages section and select Install Packages.
  • Select the Package that you want to install from the Packages pop up window.
  • And click OK, that's it package will get successfully installed.
  • After following above steps, you can run use your package from RStudio.
franiis
  • 1,378
  • 1
  • 18
  • 33
-2

I ran into this issue today where rlang couldn't install the newest version and so tidyverse wouldn't work. I tried running as admin, changing install location, running as admin, permitting rstudio through Windows Defender Randsomware Protection, and installing DevTools. The only thing that worked was to uninstall RStudio, reboot, reinstall.

myfatson
  • 354
  • 2
  • 14