2

I've created a local repository of several r packages with all their dependency - that is a directory names PACKAGES under which there many zip files for the packages.

I try to install a package using:

install.packages("dplyr",repos="path_to_repos",type="source")

and keep getting errors:

cannot open compressed file path_to_repos/src/contrib/PACKAGES, probable cause "permission denied"

I am able to install packages from the same directory by choosing the files one by one - but that's not practical due to dependencies.

I'm working on windows 10, and run as administrator. could not figure out what might be the cause of the permission error could be.

Any insight?

amit
  • 3,332
  • 6
  • 24
  • 32
  • Have you read the documentation about `install.packages()`? There is the sentence: "repos: ... Can be NULL to install from local files, directories or URLs: this will be inferred by extension from pkgs if of length one." – J_F Jun 21 '16 at 09:16
  • This happens because you haven't created a repository - you've simply copied the zips, but haven't conformed to the required respository format as described in the manuals. The package `miniCRAN` can help you solve this problem (https://cran.rstudio.com/web/packages/miniCRAN/index.html) – Andrie Jun 21 '16 at 10:53
  • Tahnks @Andrie. I've once looked at a proper repository, and it didn't look much different than just a collection of zips in one directory. can you tell me abit more about the difference? – amit Jun 22 '16 at 05:13
  • Read the [manual](https://cran.r-project.org/doc/manuals/r-release/R-admin.html#Setting-up-a-package-repository) or use `miniCRAN` to create a repository and compare the difference. – Andrie Jun 22 '16 at 06:49

2 Answers2

1

If I understand correctly, you've created a directory called "PACKAGES", which appears to be the problem. All your files should go in the "contrib" directory.

Once you've added package files to the "contrib" directory, you can use the setRepositories function from the utils package to create the repository. I'd recommend adding the following code to your .Rprofile for a local repository:

utils::setRepositories(ind = 0, addURLs = c(WORK = "file://<your higher directories>/R"))

After editing your .Rprofile, restart R.

ind = 0 will indicate that you only want the local repository. Additional repositories can be included in the addURLs = option and are comma separated within the character vector.

Next, create the repository index with the following code:

tools::write_PACKAGES("/<your higher directories>/R/src/contrib", verbose = TRUE) 

This will generate the PACKAGE files that serve as the repository index.

To see what packages are in your repository, run the following code and take a look at the resulting data frame: my_packages <- available.packages()

At this point, you can install packages from the repo without referencing the entire path to the package installation file. For example, to install the dplyr package, you could run the following:

install.packages("dplyr", dependencies = TRUE)

If you want to take it a step further and manage a changing repository, you could install and use the miniCRAN package. Otherwise, you'll need to execute the write_PACKAGES function whenever your repository changes.

After installing the miniCRAN package, you can execute the following code to create the miniCRAN repo:

 my_packages <- available.packages()

 miniCRAN::makeRepo(
  pkgs = my_packages[,1, 
  path = "/<your higher directories>/R",
  repos = getOption("repos"), 
  type = "source",
  Rversion = R.version, 
  download = TRUE, 
  writePACKAGES = TRUE,
  quiet = FALSE
 )

You only need to execute the code above once for each repo.

Then, check to make sure each miniCRAN repo has been created. You only need to do this once for each repo:

 pkgAvail(
  repos = getOption("repos"),
  type = "source",
  Rversion = R.version,
  quiet = FALSE
 )

Whenever new package files are placed into the local repo you can update the local repo's index as follows:

miniCRAN::updateRepoIndex("/<your higher directories>/R/")

Finally, as an optional step to see if the new package is in the index, create a data frame of the available packages and search the data frame:

my_packages <- available.packages(repos = "file://<your higher directories>/R/")

This approach has worked pretty well for me, but perhaps others have comments and suggestions that could improve upon it.

Ted M.
  • 182
  • 1
  • 8
  • I know this is post is almost a year old, but hoping someone can help me. I'm running Win10 and I've set the line in .Rprofile as `utils::setRepositories(ind = 0, addURLs = c(WORK = "file://R42/R"))` but I get the following error: `Error in read.dcf(file = tmpf) : cannot open the connection. In addition: Warning message: In read.dcf(file = tmpf) : Cannot open compressed file ‘//R42/R/src/contrib/PACKAGES’, probable reason ‘No such file or directory'` I've successfully added the PACKAGES file so I don't know why this error is occuring. – dave May 24 '23 at 23:41
  • I figured out 'a' way to do this and posted my method below. – dave May 28 '23 at 17:21
0

This is how I managed to make this work on a Windows 10 system. Thanks to @Andrie and @Ted-M for their above work!

Add these directories to your file system (where C:\R42_Packages is your own naming convention):

C:\R42_Packages\R\bin\windows\contrib\4.2
AND
C:\R42_Packages\R\src\contrib

Place all of the .zip Windows packages in C:\R42_Packages\R\bin\windows\contrib\4.2

Add this line to your .Rprofile - note the single / in file:/R42_Packages as opposed to file://R42_Packages shown above and in the R Installation and Administration manual:

utils::setRepositories(ind = 0, addURLs = c(localCRAN = "file:/R42_Packages/R"))

... where localCRAN is the name you want to use as a reference. (If you don't want to mess with .Rprofile then type this line into the R console NOW and then each time you use R.) If you used .Rprofile then restart R.

Verify your current R session repository points to your local directory.

options('repos')

Next, create the repository index by typing this line in the R console:

tools::write_PACKAGES("C:/R42_Packages/R/bin/windows/contrib/4.2", verbose = TRUE)

This will create three PACKAGES files in the C:\R42_Packages\R\bin\windows\contrib\4.2 directory: PACKAGES, PACKAGES.gz, and PACKAGES.rds. This process takes a bit of time to complete, so wait for the console prompt to show up.

At this point you can verify all worked correctly by, in the R console, typing:

myPacks <- available.packages()  
View(myPacks)

Finally, copy the single PACKAGES file to C:\R42_Packages\R\src\contrib. The R Installation and Administration manual says this can be a ' ... possibly empty PACKAGES file ... ' but I found this needs to be a copy of the PACKAGES file from the C:\R42_Packages\R\bin\windows\contrib\4.2 directory. Note that it won't hurt anything if you copy all three PACKAGES files into the C:\R42_Packages\R\src\contrib directory.

Lastly, you will need to re-build and re-copy the PACKAGES file(s) Every Time you update or add new .zip package files to the C:/R42_Packages/R/bin/windows/contrib/4.2 directory.

dave
  • 147
  • 1
  • 1
  • 12