Installs the torch R package (if missing) and downloads the
platform-appropriate libtorch + lantern binaries with GPU support.
After installation, verifies that GPU acceleration works via
check_torch().
Arguments
- reinstall
Logical. Force re-download of libtorch/lantern binaries even if they already exist. Use this to fix broken CUDA installations (e.g., CPU-only lantern accidentally installed). Default: FALSE.
- type
Character or NULL. Installation type, passed to
torch::install_torch(). Common values:NULL(auto-detect),"cuda"(force CUDA on Windows/Linux),"cpu"(force CPU-only). Default: NULL (auto-detect based on available GPU).- verbose
Logical. Default: TRUE.
Value
Invisibly, the result of check_torch() after installation,
or a partial list with needs_restart = TRUE if R must be restarted.
Details
The installation proceeds in stages:
If the
torchR package is not installed, it is installed from CRAN.torch::install_torch()downloads libtorch and lantern binaries. On Windows/Linux with an NVIDIA GPU and a CUDA toolkit, this automatically selects CUDA-enabled binaries.A verification step runs
check_torch()to confirm GPU works.
Important: If the torch package was already loaded in the current R
session before calling setup_torch(), you must restart R for the new
binaries to take effect. The function detects this and warns accordingly.
CUDA auto-detection
torch::install_torch() detects the CUDA version from:
The
CUDAenvironment variable (if set, forces a version)CUDA_PATH(Windows) orCUDA_HOME(Linux)nvcc --versionon PATH
See also
check_torch() to diagnose the current setup,
use_gpu() to enable GPU acceleration after setup.