3

I have a question regarding on how to automatically install dependent libraries while installing a self-made package from source.

Basically, the DESCRIPTION file looks like:

Package: mypackage
Type: Package
Title: mypackage_title
Version: 0.1.0
Author: test 
Maintainer: test <test @example.com>
Description: More about what it does (maybe more than one line)
Depends:
    dplyr, 
    stringr
License: MIT
LazyData: TRUE
RoxygenNote: 5.0.1

I followed the guide and created a .tar.gz file. When I distribute this package, a user will install from the source. In addition, what I expect is that from this source installation, dependent libraries such as dplyr and stringr will be installed automatically if those two are not available.

I tested in a virtual machine but ran into the following errors. So any suggestions on this issue? Thanks in advance!

install.packages("path_to_my_tar.gz", repos=NULL, type="source")
Error: dependencies 'dplyr', 'stringr' are not available for package

Update: I tried a couple of other approaches but still failed...

Approach 1

install.packages("path/to/my/package", repos = NULL, type = "source")
ERROR: dependencies 'dplyr', 'stringr' are not available for package 

Approach 2: putting my package, dplyr, and stringr into path/to/my/package/folder

install.packages("path/to/my/package", repos =  "path/to/my/package/folder/", type = "source")
Warning in install.packages :
  unable to access index for repository path/to/my/package/folder/src/contrib:
  scheme not supported in URL 'path/to/my/package/folder/src/contrib/PACKAGES'
Warning in install.packages :
  my packageis not available (for R version 3.3.1)

Approach 3: putting my package, dplyr, and stringr into path/to/my/package/folder using contriburl

install.packages("path/to/my/package", contriburl = "path/to/my/package/folder/", type = "source")
Installing package into my package (as lib?is unspecified)
Warning in install.packages :
  unable to access index for repository path/to/my/package/folder/:
  scheme not supported in URL 'path/to/my/package/folder/PACKAGES'
Warning in install.packages :
  my package is not available (for R version 3.3.1)

Approach 4:

install.packages("ProdComp08032016_0.1.2.tar.gz", repos = c("file://C:/Users/TH2/Downloads/", "http://cran.us.r-project.org"), type = "source") Installing package into :/Users/TH2/Documents/R/win-library/3.3?(as ib?is unspecified) Warning in install.packages : cannot open compressed file '//C:/Users/TH2/Downloads/src/contrib/PACKAGES', probable reason 'No such file or directory' Error in install.packages : cannot open the connection

Approach 5:

install.packages("ProdComp08032016_0.1.2.tar.gz", repos = c("C:/Users/TH2/Downloads/", "http://cran.us.r-project.org"), type = "source") Installing package into :/Users/TH2/Documents/R/win-library/3.3?(as ib?is unspecified) Warning in install.packages : unable to access index for repository C:/Users/TH2/Downloads/src/contrib: scheme not supported in URL 'C:/Users/TH2/Downloads/src/contrib/PACKAGES' Warning in install.packages : package rodComp08032016_0.1.2.tar.gz?is not available (for R version 3.3.1)

TTT
  • 4,354
  • 13
  • 73
  • 123
  • 1
    Just to clarify; your virtual machine has a configured internet connection with which it could install said dependencies? What happens if you try to install it from the `.tar.gz` on your build machine? – Jonathan Carroll Aug 03 '16 at 02:47
  • @JonathanCarroll, correct. The virtual machine has internet connection. The same installation approach works on my build machine, which had `dplyr` and `stringr` installed before. – TTT Aug 03 '16 at 03:27
  • A basic solution: run `install.packages(c("dplyr", "stringr"))` before the `install.packages("path_to_my_tar.gz", repos=NULL, type="source")`. You'll need to update the `install.packages()` call when you add new dependencies to your package, but that should be fine. – Paul Rougieux Aug 09 '18 at 12:01

2 Answers2

3

EDIT

As per this duplicate question, Package dependencies when installing from source in R, the command

install.packages(pkgs = my.package.name, repos = c("file://path/to/my/custom/repo", "http://cran.somepage.org"))

can be made to work, if you create a local R repository.

The other option is to make the package a GIT project and use

devtools::install_local("path/to/package.tar.gz")

I don't have a package ready for testing, but my guess is repos = NULL screws install.packages looking for dependencies of your package.

Looking at ?install.packages looks like you have bad luck without putting your dependencies in the local repository as well:

Arguments

pkgs character vector of the names of packages whose current versions should be downloaded from the repositories. If repos = NULL, a character vector of file paths of ‘.zip’ files containing binary builds of packages. (http:// and file:// URLs are also accepted and the files will be downloaded and installed from local copies.) Source directories or file paths or URLs of archives may be specified with type = "source", but some packages need suitable tools installed (see the ‘Details’ section). If this is missing or a zero-length character vector, a listbox of available packages is presented where possible in an interactive R session.
lib character vector giving the library directories where to install the packages. Recycled as needed. If missing, defaults to the first element of .libPaths().
repos character vector, the base URL(s) of the repositories to use, e.g., the URL of a CRAN mirror such as "http://cran.us.r-project.org". For more details on supported URL schemes see url. Can be NULL to install from local files, directories or URLs: this will be inferred by extension from pkgs if of length one.

Here are my two guesses:

install.packages(pkgs = "package", ..., repos = c("file://path.to.tar.gz", "http://path.to.cran.mirror")) # Try to specify the path to your package as another repo
install.packages(pkgs = "file://path.to.tar.gz.file") # Leave repos alone and hope the file:// suffices

Please let me know whether they work or not :)

Community
  • 1
  • 1
AlexR
  • 2,412
  • 16
  • 26
  • So you are suggesting that I should put both of my dependency packages (`dplyr` and `stringr`) into my distributed R package, then list their path as part of repo? Is it possible to use an URL for a local install? – TTT Aug 11 '16 at 17:01
  • @tao.hong I'm hoping that one of the two calls will work without downloading `dplyr` or `stringr` manually, but according to the docs, placing the dependencies into the same folder seems the only supported way. – AlexR Aug 11 '16 at 17:02
  • `@AlexR` I tried your suggested method, but still failed... details are in the updates. – TTT Aug 11 '16 at 18:33
  • @tao.hong It looks like you've omited the `file://` specifier before the path/to/package/folder. Mind retrying since the docs specifically mention that method 3 would work with the `file://`?... – AlexR Aug 11 '16 at 18:39
  • I am kind of lost here. You mean sth like this? It did not work.. `install.packages("ProdComp08032016_0.1.2.tar.gz", contriburl = "file://C:/Users/TH2/Downloads/", type = "source")` – TTT Aug 11 '16 at 18:51
  • @tao.hong I meant replace "contriburl" by "repos". Basically as it stands in my answer. Especially the one with `install.packages(pkgs = "mypackage", repos = c("file://C:/Users/TH2/Downloads/", "http://cran.us.r-project.org"))` where "mypackage" is the package name looks promising to me. – AlexR Aug 11 '16 at 18:54
  • @tao.hong I dug around a bit and found this duplicate question: http://stackoverflow.com/questions/5805049/package-dependencies-when-installing-from-source-in-r It basically uses approach 5 (as I suggested), but you need to create an own repo, i.e. you need to create the file it doesn't find "src/contrib/PACKAGES"... Also, this Q contains the same answer as here, using devtools; this seems to require you to make your package a valid git project before .tar.gz-ing it. Either way, you're gonna have to add some new files - I'd go with the git approach, as this seems the cleanest. – AlexR Aug 11 '16 at 19:03
  • `@AlexR` Yes, I am considering to build stuff from command line or devtools instead of using Rstudio's GUI. I will take a look at later and keep this thread alive. THX!! – TTT Aug 11 '16 at 19:06
  • On windows there is no `//` in `file://`. I've had luck with the following: `install.packages('mypkg', contriburl=sprintf('file:%s', mycrandir), repos=NULL)` – logworthy May 09 '18 at 06:04
  • 1
    @TH339 Under windows I seem to need to use three slashes instead of two and no drive letter colon: file:///C/... – AlexR May 09 '18 at 06:06
1

No guarantees, but perhaps

devtools::install_local("path_to_my_tar.gz")

and if that doesn't quite work, try with dependencies=TRUE?

Jonathan Carroll
  • 3,897
  • 14
  • 34
  • Thanks for the suggestion. Tested both suggestions, but without luck... For the `devtools` approach, I got the following error `Error in git2r::discover_repository(path, ceiling = 0) : Error in 'git2r_repository_discover': The `.git` file at 'path_to_my.tar.gz' is malformed' – TTT Aug 03 '16 at 03:45
  • I'm not sure why it would be going down that path to begin with, so now I have to ask - how did you create the `.tar.gz` source file? – Jonathan Carroll Aug 03 '16 at 03:52
  • I built it in Rstudio by clicking `Build Source Package` through its `GUI` – TTT Aug 03 '16 at 03:54
  • 1
    @TH339 the archive file you created in RStudio with `Build Source Package` can only be installed with `install.packages("packagename.tar.gz", repos = NULL, type="source")`. To use `devtools::install_local()`, you need to provide the folder in which you develop the package, before you build it. Then you can run `devtools::install_local("path_to_the_package_creation_folder")`. – Paul Rougieux Aug 09 '18 at 11:56
  • @PaulRougieux, I see. Thanks for proving a solution, Been using `install.packages("packagename.tar.gz", repos = NULL, type="source")` for a while but will look into the second approach you suggested. – TTT Aug 09 '18 at 16:31