---
title: "Installation"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Installation}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```


## PyTorch installation

### Installing Python and PyTorch
Although, _Python_ and _PyTorch_ can be installed directly from the _R_ console, before start running `rTorch`, I would recommend installing **PyTorch** first in a new Python or Python-Anaconda environment. Then, testing if PyTorch and Torchvision packages are imported alright. The advantage of doing it this way is that you define in advanced the base Python or Anaconda version to install. Although the same can be done from __rTorch__ you will need to get familiar passing parameters through one its functions.

If you opt to install _PyTorch_ from _R_, __rTorch__ has functions that could help you install _PyTorch_ from the _R_ console. 

### rTorch functions to install PyTorch

#### install_pytorch
This function is public and can be invoked with `rTorch::install_pytorch()`.

This function will allow you to indicate (i) the Python version; (ii) the PyTorch version; (iii) the name of the conda environment; (iv) which channel (`stable` or `nightly`); (v) if you require CUDA (GPU) computation; (vi) additional packages such as `matplotlib`, `pandas`; (vii) more.

```
install_pytorch(
  method = c("conda", "virtualenv", "auto"),
  conda = "auto",
  version = "default",
  envname = "r-torch",
  extra_packages = NULL,
  restart_session = TRUE,
  conda_python_version = "3.6",
  pip = FALSE,
  channel = "stable",
  cuda_version = NULL,
  dry_run = FALSE,
  ...
)

```


## Manual installation of _PyTorch_ in a _conda_ environment

If you prefer do it manually, use this example:

1. Create a conda environment with `conda create -n my-torch python=3.7 -y`

2. Activate the new environment with `conda activate my-torch`

3. Inside the new environment, install _PyTorch_ and related packages with:  

`conda install python=3.6 pytorch torchvision matplotlib pandas -c pytorch`

> Note: If you you don't specify a version, `conda` will install the latest _PyTorch_. As of this writing (August-September 2020), the latest _PyTorch_ version is 1.6.

Alternatively, you could create and install a *conda* environment a specific __PyTorch__ version with:

`conda create -n my-torch python=3.6 pytorch=1.3 torchvision matplotlib pandas -c pytorch -y`

`conda` will resolve the dependencies and versions of the other packages automatically, or let you know your options.

**Note.** `matplotlib` and `pandas` are not really necessary, but I was asked if `matplotlib` or `pandas` would work in PyTorch. Then, I decided to put them for testing and experimentation. They both work.


## Automatic Python detection 
In __rTorch__ there is an automatic detection of _Python_ built in in the package that will ask you to install `Miniconda` first if you don't have any Python installed in your machine. For instance, in `macOS`, Miniconda will be installed under `PREFIX=/Users/user_name/Library/r-miniconda`. 

After *Miniconda* is installed, you could proceed to install the flavor or *PyTorch* you want, and the packages you want, with a command like this:

```
rTorch:::install_conda(package="pytorch=1.4", envname="r-torch", conda="auto", conda_python_version = "3.6", pip=FALSE, channel="pytorch", extra_packages=c("torchvision", "cpuonly", "matplotlib", "pandas"))
```

The command above will install the __stable__ __PyTorch 1.4__ version on __Python 3.6__, including three additional packages: `torchvision`, `cpuonly`, `matplotlib` and `pandas.`

> NOTE. My experience with `Miniconda` is spotty and not 100% reliable, specially in _macOS_. I would strongly recommend using full _conda_ for your PyTorch installation.

