Is there a way to 'install' R packages without running install.packages()? - r

We are testing how to run R in the cloud in a secure isolated environment that is blocked from CRAN and also cannot use packages.install(). We defined an environment which is based on R essentials Anaconda's bundle, still we would like to be able to customize it on demand with extra packages. Is there a way to be able to simulate packages.install(), e.g. by offline downloading the package, zip it, copy to the secure environment and unzipping it to a specific location in the library folder?
thanks!

You can download the package from CRAN as a zip and then transport it to the isolated PC as a file. For example, here is the link to dplyr on CRAN: https://cran.r-project.org/web/packages/dplyr/index.html
Then use the code below to install the local file:
install.packages("~/Downloads/dplyr_1.0.7.zip", repos = NULL)
On Windows you might require Rtools. At least there was a Warning about it but the package still installed.
For Linux machines, you can build the package from source using the tarball from the same page:
install.packages("~/Downloads/dplyr_1.0.7.tar.gz", repos = NULL, type = "source")
In both cases you need to take care of dependencies yourself as they are not checked while installing through this method (look at the "imports" field on the CRAN website for the package).

Related

Non-standard Remotes package INLA in R package

I have a package that Requires INLA, which is not hosted on CRAN or a standard GitHub repository. There are multiple SO questions detailing how to install the package on a personal machine, such as this, or even mentions it as a dependency in a package.
The two ways that are typically recommended to install on a personal machine are:
Direct from INLA website
install.packages("INLA",repos=c(getOption("repos"),INLA="https://inla.r-inla-download.org/R/stable"), dep=TRUE)
From the GitHub host
devtools::install_github(repo = "https://github.com/hrue/r-inla", ref = "stable", subdir = "rinla", build = FALSE)
Now, these are fine for personal machines, but don't work in the DESCRIPTION files Remotes: section.
If we do url::https://inla.r-inla-download.org/R/stable, this gives an error that the file extension isn't recognized.
Error: Error: Failed to install 'unknown package' from URL:
Don't know how to decompress files with extension
If we do github::hrue/r-inla, I am unaware of how to pass (or if it's even possible) the ref, subdir, and build arguments in the DESCRIPTION file.
Previous packages used a read only mirror of the INLA code that was hosted on GitHub, solely for this purpose, at this repo and then just using github::inbo/INLA. However, this repository is out of date.
Current solution
What I'm doing instead is to directly reference the tarball hosted on the main webpage.
url::https://inla.r-inla-download.org/R/stable/src/contrib/INLA_21.02.23.tar.gz
This solution works, and passes CI as well as the machines are able to install and load from there. The only issue is that I need to periodically update the static link to this tarball, and would prefer to reference the stable build, either directly from the INLA website as above, or the hrue/inla repo with those other arguments passed. Directly referencing those links also has the advantage that when my package is re-installed on a machine, it would recognize whether or not the latest version of INLA has been installed on that machine. Is there a way to achieve this in the DESCRIPTION file?
This is not a perfect answer but maybe what you can do is add the zip url of the stable branch of INLA from the new github repository of INLA:-
url::https://github.com/hrue/r-inla/archive/refs/heads/stable.zip
Hence, this will always install the latest stable version of the package.

How do i keep source files when using R's devtools library function 'install'

I am trying to build an R package (DESeq2) from source so that I can debug it. I've installed all the dependencies required and I'm following Hillary Parker's instructions for creating R packages. I'm running this on CentOS 6.6 using R-3.4.2.
I run :
library("devtools")
install("DESeq2", keep_source=TRUE)
It installs it in the directory with all my other R libraries. When I look at the installed DESeq2 library it is missing all the DESeq2/R/*.R and DESeq2/src/*.cpp files.
QUESTION : Where are these files and why didn't they get installed? This does not seem like the expected behavior.
R uses binary database format for installed packages to pack the objects into a database-alike file format for efficiency reasons (lazy loading). These database files (*.rdb and *.rdx) are stored in the R sub folder of the package installation path (see ?lazyLoad).
Even if
you are looking at the right place to find the installed package (use .libPaths() in R to find the installation folder)
and you have installed the package with the source code (like you did or
via install.packages("a_CRAN_package", INSTALL_opts = "--with-keep.source"))
you will not find R files in R folder there.
You can verify that the source code is available by picking one function name from the package and print it on the console. If you can see the source code (with comments) the package sources (R files) are available:
print(DeSeq2::any_function)
To make the source code available for debugging and stack traces you can set the option keep.source.pkgs = TRUE (see ?options) in your .Rprofile file or via an environment variable:
keep.source.pkgs:
As for keep.source, used only when packages are
installed. Defaults to FALSE unless the environment variable
R_KEEP_PKG_SOURCE is set to yes.
Note: The source code is available then only for newly installed and updated packages (not for already installed packages!).
For more details see: https://yetanothermathprogrammingconsultant.blogspot.de/2016/02/r-lazy-load-db-files.html

what is the difference of install r package in tow commands?

When I install the packages in R, sometimes it is used by devtools::install_github(). other times it is used by install.packages().
Could I ask what is the essential difference between them?
R's official repository for packages is located on CRAN (Comprehensive R Archive Network). The process of publishing a package there is very strict and is reachable via install.packages(). For the most part, binary packages (opposed to source code, which is not "properly translated" yet) are available and no additional tools need to be present for proper installation (see next paragraph).
GitHub is one of many webservices that offers repositories for code, incl. R code. Author can upload her or his package and if everything is in its place, the user can install a package from source via devtools::install_github(). This means you need to have a proper toolchain installed (also a distributoin of LaTeX). In Windows, this means Rtools. Linux based OS are likely to be shipped with most of the necessary tools.

unable to install R library in azure ml

I have been trying to install a machine learning package that I can use in my R script.
I have done placed the tarball of the installer inside a zip file and am doing
install.packages("src/packagename_2.0-3.tar.gz", repos = NULL, type="source")
from within the R script. However, the progress indicator just circles indefinitely, and it's not installed in environment.
How can I install this package?
ada is the package I'm trying to install and ada_2.0-3.tar.gz is the file I'm using.
You cannot use the tarball packages. If you are on windows you need to do the following:
Once you install a package (+ it's dependencies) it will download the packages in a directory
C:\Users\xxxxx\AppData\Local\Temp\some directory
name\downloaded_packages
These will be in a zip format. These are the packages you need.
Or download the windows binaries from cran.
Next you need to put all the needed packages in one total zip-file and upload this to AzureML as a new dataset.
in AzureML load the data package connected to a r-script
install.packages("src/ada.zip", lib = ".", repos = NULL, verbose = TRUE)
library(ada, lib.loc=".", verbose=TRUE)
Be sure to check that all dependent packages are available in Azure. Rpart is available.
For a complete overview, look at this msdn blog explaining it a bit better with some visuals.

R - setting up my own CRAN repository

I want to set up a local CRAN repository . I want to put just one package in this repository ( let's call it MyPackage ). The reason I'm doing this is that I want to share this package with people at my company. By the way - we all use Ubuntu Linux.
I have already done this:
I have a web server ( BOA web server ) and made a web folder called R. Made folder src and contrib.
In the contrib folder I put my package MyPackage ( tar.gz) plus the PACKAGES file.
However, when I do this:
install.packages("MyPackage", repos = "127.0.0.1/R" )
it does not work ;
Warning: unable to access index for repository [ ]
Warning message:
In getDependencies(pkgs, dependencies, available, lib) :
package ‘MyPackage’ is not available (for R version 2.13.1)
Can you guys guide me a bit and tell me what is the correct folder structure ?
Thanks.
See "Section 6.6 Setting up a package repository" of the R Admin manual.
Edit some three+ years later: We now have the drat package which automates creating a repository, and can use GitHub in a clever way to host it for you.
You might just need to specify the URL properly; http://127.0.0.1/R.
Also, make sure you can access that URL in your browser.
The miniCRAN works good for me. There are few advantages to using miniCRAN to create the repository:
Security: Many R users are accustomed to downloading and installing new R packages at will, from CRAN or one of its mirror sites.
Easier offline installation: To install package to an offline server requires that you also download all package dependencies, Using miniCRAN makes it easier to get all dependencies in the correct format.
Improved version management: In a multiuser environment, there are good reasons to avoid unrestricted installation of multiple package versions on the server.
Use other R Package Indexes: You may wish to make packages available from public repositories other than CRAN, e.g. BioConductor, r-forge, OmegaHat, etc.
Prepare own R repo: You may wish to add custom in-house packages to your repository.
See intro:
Using miniCRAN to create a local CRAN repository
Create a local package repository using miniCRAN
I think the problem is revealed in this statement:
"In the contrib folder I put ... the PACKAGES file."
The PACKAGES file is an index for the repository. You need to create that file after your package files are placed in the repository directory. Don't copy and paste the PACKAGES file from another repository.
If I were you, here's what I would do. First, add the following code to your .Rprofile for a local repository:
utils::setRepositories(ind = 0, addURLs = c(WORK = "127.0.0.1/R"))
Restart R after changing your .Rprofile.
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.
Then, create the repository index:
tools::write_PACKAGES("127.0.0.1/R/src/contrib", verbose = TRUE)
After you do that, you should be able to generate a data frame that has a list of all the packages. For example, my_packages <- available.packages().
If you see packages in your repository data frame, then install using the following code:
install.packages("MyPackage")
For more information, please see here.

Resources