# R Repositories

[R](https://www.r-project.org/) is a language used for statistical analysis and machine learning. R and R Studio both allow you to install packages from repositories, allowing convenient access to a large number of packages from the remote Comprehensive R Archive Network ( [CRAN](https://cran.r-project.org/)).

Nexus Repository takes advantage of the packages in the official CRAN repository and other public repositories without incurring repeated downloads of packages.

You can publish your own packages to a private repository in a hosted R repository and then combine the remote and private repositories to R as a repository group, which is a repository that merges and exposes the contents of multiple repositories in one convenient URL. This allows you to reduce time and bandwidth usage for accessing R packages as well as more easily share your packages within your organization.

## Supported File Types

Proxy R repositories support any file type.

Hosted and group R repositories only support files with a `.gz` extension.

If your proxy R repository is part of a group, you will only be able to request files with a `.gz` extension through the group. You will need to request any other file type directly from the proxy repository.

## Proxying R Repositories

You can set up an R proxy repository to access a remote repository location.

To proxy an R repository, create a new _r (proxy)_ as shown in the documented example in [Repository Management](https://help.sonatype.com/en/repository-management.html). Minimal configuration steps for R proxy are:

- Define _Name_, e.g. `r-proxy`
- Define _URL_ for _Remote storage_, e.g. [https://cran.r-project.org/](https://cran.r-project.org/)
- Select a _Blob store_ for _Storage_

**Note**

Some R client commands, such as `devtools::install_version()` and `renv::install("package@version")`, may fail with `404` errors when installing older package versions. As a workaround, use `remotes::install_version()` to install specific versions.

## Hosting R Repositories

**Note**

Hosted R repositories only support files with a `.gz` extension.

Creating an R hosted repository allows you to register packages in the repository manager. The hosted repository acts as an authoritative location for these components.

To add a hosted R repository, create a new repository with the recipe _r (hosted)_ as shown in the documented example in [Repository Management](https://help.sonatype.com/en/repository-management.html). Minimal configuration steps for R hosted are:

- Define _Name_, e.g. `r-hosted`
- Select _Blob store_ for _Storage_

## Deploying Packages to R Hosted Repositories

**Note**

Hosted R repositories only support files with a `.gz` extension.

### Upload with Nexus Repository Manager UI

To upload packages to a hosted R repository, [upload components through the user interface](https://help.sonatype.com/en/uploading-components.html).

R format requires to specify proper package path location inside a repository. You can do this by using the _package path_ field in the Upload form. More details about the package path could be found on the official site ( [https://cran.r-project.org](https://cran.r-project.org/web/packages/index.html)) in the _Related Directories_ section_.

Several examples for package path field:

- For sources (_Package path_): `src/contrib`
- For Windows binary (_Package path_): `bin/windows/contrib/3.6`
- For OS X binary (_Package path_): `bin/macosx/el-capitan/contrib/3.6`

### Upload by HTTP POST

The following example uses the `curl` command to upload an `example_1.0.0.tar.gz` file to a hosted R repository (fill in <> with your respective data):

```bash
curl -v --user '<username>:<password>' --upload-file example_1.0.0.tar.gz http://<host>:<port>/repository/<repository_name>/src/contrib/example_1.0.0.tar.gz
```

The package will now be contained within your hosted repository and consumers can install the package as described in [Configuring R Client](https://help.sonatype.com/en/r-repositories.html#configuring-r-client).

## R Repository Groups

**Note**

Group R repositories only support files with a `.gz` extension.

A repository group is the recommended way to expose all your R repositories from the repository manager to your users, with minimal additional client-side configuration. A repository group allows you to expose the aggregated content of multiple proxy and hosted repositories as well as other repository groups with one URL in tool configuration. This is possible by creating a new repository with the _r (group)_ recipe as shown in the documented example in [Repository Management](https://help.sonatype.com/en/repository-management.html). Minimal configuration steps for R group are:

- Define _Name_, e.g. `r-group`
- Select _Blob store_ for _Storage_
- Add R repositories to the _Members_ list in the desired order

If you request a file from a group and that file is present in more than one group member, the group will return merged data.

## Configuring R Client

Once you have set up your repositories for R packages, you can adjust your R startup script to use your repository URLs. A suggested way to do so is to create a `.Rprofile` file and include a snippet similar to the following in it:

```r
## Default repo
local({r <- getOption("repos")
        r["Nexus"] <- "http://<host>:<port>/repository/<repository_name>"
        options(repos=r)
})
```

This will set your default R repository as the group repository. For more information on adjusting R startup files, please visit the [mini guide on the r-bloggers](https://www.r-bloggers.com/fun-with-rprofile-and-customizing-r-startup).

Also, it is possible to install an R package directly from the R client console:

```r
install.packages("example", repos="http://<host>:<port>/repository/<repository_name>", type="<package_type>")
```

`<package_type>` is optional and accepts values of `source` and `binary`. If left off the console command, it defaults to `binary`.

If anonymous access to the repository manager is disabled, you have to specify the credentials for accessing the repository manager as part of the URL like `http://<username>:<password>@<host>:<port>/repository/<repository_name>`.

Downloaded packages are cached, do not have to be retrieved from the remote repositories again and can be inspected in the user interface.
