Installing R On Windows Machines

Introduction#

  • Installing softwares sometimes can be tricky or even daunting for computer novices, even for regular or home users.

  • In this tutorial, we will walk you through R, RStudio IDE, Classic Jupyter and Jupyter lab installation step by step, and with different ways, using graphical user interface and Command Line Interface. We will also use some utilities such as wget and choco package manager.

  • The third way of installing R is an advanced way that gives you the ability of full control over installing a software.

Installation and Setup#

The Regular Installation#

  • The first step is to install R from CRAN

Installing R Using Command Line Interface#

  • Entering the technical world will eventually lead you to use the command line interface or CLI, in Windows, there are two native CLIs, CMD and PowerShell.

  • In this section, we will discuss how to install R using the command line CMD, you can use PowerShell, or any similar command line installed on your system.

  • To be able to follow along this section, you need to install first few utilities in this sequence:

    1. Installing chocolatey (choco): Which is a package manager for Windows.

    2. Installing wget using choco, this is optional, however, we prefer to use this tool so we can determine the directory (folder) where the installer will be.

    3. Run the installer from the command prompt.

  • In a later section, we will discuss how to use the command prompt to custom the R installation, knowing this step requires prior knowledge of how computers work, however it provides more control over your system.

Installing Chocolatey#

  • You can start the command prompt by typing cmd in the search (usually looks like the magnifying glass), You will find the Command Prompt app, you have to run in admin mode since you are installing a new software. Or use a combination of keys from the keyboard (Win + R), then type cmd in the Run dialog, to start cmd in Admin mode, you need to use Ctrl + Shift + enter keys.

  • You can open PowerShell the same way.

  • Here is the link to install choco using CMD: Install with cmd

  • Or you can install that from the PowerShell from this Install with PowerShell

Installing wget or curl#

  • You can install wget or curl utilities to download softwares or package from the Internet. Using these tools facilitates the downloading process, however, you do not need to use them if don’t want to.

  • You can download wget from the official website and then run the installer, and follow the installation instruction. Wait, we just downloaded a Windows package manager, this will make your life a lot easier, and now it the time to discover a world of fun using the command prompt, I am quite sure you will fall in love with using the command prompt.

  • To install wget using choco simply run the following command (You must start CMD or PowerShell in admin mode)

choco install wget 
  • This will prompt you to type y to confirm the installation, if you trust the package, you can even suppress the confirmation prompt by adding -y flag like this:

choco install wget -y
  • Voila, that is it, wget installed on your system and it is ready to for use. I told you, the using the command prompt will make your life easier.

Installing R Using choco#

  • Installing R using choco package manager is the easiest and fastest way, it is simply typing a simple command and it will do most of the things for you, like downloading, check data integrity and adding R to the path. No intervention from you, simply type the following command, and if you don’t wish for confirmation prompt add -y flag. However, if you are interested in the file that does all this, you can do that by typing p when it is prompted that to you, if satisfied, type y to proceed.

choco install r -y

Downloading R and Running The Installer#

  • Now, it is time to download R installer, check its data integrity, then run the installer all from within the command prompt, at this stage we will accept the default settings provided with the installer. Here the steps of processing the installation:

  1. Let us create a folder (directory) like Rstuff in C:\Users\Public\RStuff , the placement where the installer will be using the this command (You can create the folder of your choice)

mkdir Rstuff
cd Rstuff
  1. Download the installer using the installer using wget

wget https://cran.r-project.org/bin/windows/base/R-4.3.1-win.exe
  1. Check the integrity if you wish with the hash key provided, if they don’t match, it means that the installer did not download correctly of it is tampered with.

    • First, you need to check the hash-key or fingerprint

    • Generate the hash key for the installer using cerutil utility.

    • Compare the keys

    • You can save both keys in different files then use the cf command to make sure that keys are really match.

  • Here are the steps of checking data integrity:

certutil -shasum installer MD5

# You may save this to a file 
certutil -shasum installer MD5 > genkey.txt
# Copy and paste the provided key getkey.txt
# Compare the two files 
cf genkey.txt getkey.txt
  • If you get the message no difference encountered then you are good to go. If the files don’t match, you need to consider downloading the software again.

  1. Run the installer: You can simply run the installer from the command prompt, and R will be installed on your system, you may need to confirm the installation, or you may change the directory to a directory where you wish the installation to take place.

R-4.3.1-win.exe
  • Now, R is installed.

Advanced Installation#

  • This section is meant to professionals who wants to have full control over installing the package like changing the default directory, and silencing the installation or setting environment variables. This can be achieved easily with the paid version of choco, however, we are interested in open source tools, even it takes a little more effort to read the documentation of R (or any other package). We will provide how to do that with in here and refer you to section where it is explained:

    • Download the installer using wget as explained above.

    • Read this section from R documentation:

      • Set the default installation directory ‘/DIR="x:\dirname"’

      • ‘/CURRENTUSER’ to install for the current user only even if the current user has Administrator privileges.

      • Silence the installation using one of these options:

        • ‘/SILENT’ to only show the installation progress window and error messages.

        • /VERYSILENT’ to only show error messages.

  • Here is an example to install R in C:\Renv (Renv is directory where the installation will take place) for the current user and totally silencing the installation

R-4.3.1-win.exe "/Dir=C:\Renv /CURRENTUSER /VERYSILENT"

Checking the Path#

  • You may want to check that R has been added to the path by typing R in the command prompt, if for some reason it hasn’t been added, we can add it to the path using either the command prompt or the graphical user interface. In this section, we add R to the path using the command prompt (in administrator mode).

setx /m "path-to-R;%PATH%"
  • That is it, relaunch the command prompt and start using R from there. I told you, using the command prompt is incredibly fast and amazing.

Adding R to Path Manually#

  • Open the run dialog (this is the easiest way)

    • Type sysdm.cpl in the Run dialog and press enter

    • The System properties will pop up

    • Go advanced tab

    • Press on Environment variables

    • Go to System variable and find path and press on edit (This will add Python system wide)

    • Press on new

    • Add the location of the executable Python, this location looks like this C:\Program Files\R\bin

    • Press ok

    • You can add Python to this specific user the same way you did with system variable from User variables

    • When you finish, press ok, the ok from the environment variables and finally Ok.

    • Test that R has been added to the path from the command prompt by typing R --version there.

Installing RStudio#

  • RStudio is an incredibly useful Integrated Development Environment or IDE for writing and using by many data scientists.

  • RStudio is used for writing and testing R codes, because it provides

    • Console window

    • Code editor

    • tools for creating plot graphics and debugging code.

    • You can even support version control features.

Warning

RStudio will not function unless R is already installed on your machine. It is important to understand that R and RStudio are two separate installations: R is the programming language itself, while RStudio is an integrated development environment (IDE) that provides a user-friendly interface for working with R. Therefore, you must first install R, and then install RStudio to ensure everything works correctly.

wget https://posit.co/download/rstudio-desktop/

# check the data integrity
# run the installer
  • Let Choco do the job for you

choco install rstudio