Installing R using Powershell

[This article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Installing R from scratch and creating your favorite IDE setup is especially useful when making fresh installation or when you are developing and testing out different versions.

This blogpost will guide you through some essential steps (hopefully, there will not be many) on how to download the desired R engine, desired R GUI – in this case RStudio, and how to prepare the additional packages with some custom helper functions to be used in the client set-up / environment. And mostly, using PowerShell script.

2019-02-14 20_03_23-Window

Test folder for this new R Environment will be: C:\DataTK\99_REnv\01_Source\.  And the rest of the folder structure will be:

2019-02-14 20_59_45-Window

Folder structure is completely arbitrary and can be changed, accordingly.

1. Downloading the RStudio and R

All the programs will be installed with predefined paths (Please note, this path might vary on your client machine):

  • RStudio ->  c:\Program Files\RStudio
  • R Engine -> c:\Program Files\R\R-3.5.1

Both paths can be different on your machine. In the folder structure, I will set my folder pointing to 01_Source sub-folder, as shown in ps script.

$dir = "C:\DataTK\99_REnv\01_Source\"
Set-Location $dir

## Download RSTudio for Windows machine

# Version of RStudio is deliberatly set to specific version
# so that code is repeatable and always returns same results
$urlRStudio = ""
$outputRStudio = "$dir\RStudio.exe"

$wcRStudio = New-Object System.Net.WebClient
$wcRStudio.DownloadFile($urlRStudio, $outputRStudio) # $PSScriptRoot 
Write-Output "Download Completed"

## Download R engine for Windows machine
$urlR = ""
$outputR = "$dir\R-win.exe"
$wcR = New-Object System.Net.WebClient
$wcR.DownloadFile($urlR, $outputR)
Write-Output "Download completed"

## Installing R / RStudio on desired Path
## Silent install
$dirRStudio = $dir + "RStudio.exe"
$dirR = $dir + "R-win.exe"

Start-Process -FilePath $dirRStudio -ArgumentList "/S /v/qn"
Start-Process -FilePath $dirR -ArgumentList "/S /v/qn"

Now that we have the R engine and R Studio installed, you need to repeat the process for downloading the R Packages. In same manner, I will start downloading the specific R packages.

2. Downloading the R packages

For the brevity of this post, I will only download couple of R packages from CRAN repository, but this list is indefinite.

There are ways many ways to retrieve the CRAN packages for particular R version using powershell. I will just demonstrate this by using Invoke-WebRequest cmdlet.

Pointing your cmdlet to URL:  where  list of all packages for this version is available. But first we need to extract the HTML tag where information is stored. Since the URL stores data in a table, we have to navigate to following tag: html>body>table>tbody>tr>td>a where the file name is presented.

2019-02-17 07_37_25-Window

Packages names is retrieved by:

2019-02-17 07_45_48-Window

$ListRPackages= Invoke-WebRequest -Uri ""
$pack = ($ListRPackages.ParsedHtml.getElementsByTagName('a')).outerText

If you have the list of needed packages listed in a txt file, you can read the package names from file and iterate through the webpage and download the files:

$ListPackageLocation = "C:\DataTK\99_REnv\01_SourceList\packages.txt"
$PackList = Get-Content -Path $ListPackageLocation
$dir = "C:\DataTK\99_REnv\01_Source\"

ForEach ($Name in $PackList)
   $UrlRoot = ""
   $url = $UrlRoot + $Name
   $FileName = $dir +'\' + $Name
   $PackagesOut = New-Object System.Net.WebClient
   $PackagesOut.DownloadFile($url, $FileName) 
   Write-Output "Download Completed"


Now that we have all the packages downloaded and programs installed, we can move to R.

3. Setting up the R Environment

In the folder structure, there is a folder including the helper files:

2019-02-17 08_15_56-03_RHelperFiles.png


In this file all the paths are typed and later used in any other file. Simply the folder structure is described:

sourcePath = "c:\\DataTK\\R_packages\\01_Source"
sourcePackagePath = "c:\\DataTK\\R_packages\\01_Sourcelist"
libPath = "C:\\DataTK\\R_Packages\\02_R"
wdPath = "C:\\DataTK\\R_Packages"



This file includes all the functions lists in one place, mainly for sharing or creating shared projects. In  this case, just two functions, one for checking and installing missing packages, read from the folder structures (that were previously downloaded using powershell).

# Function for sum of squares for two input integers
sum_squares <- function(x,y) {
  x^2 + y^2

# Function for package installation with check for existing packages
function_install_4 <- function(df_name) {
    for (i in 1:nrow(df_name)){
       if (df_name[i,2] %in% rownames(installed.packages(lib.loc=libPath))){
       print(paste0("Package ",df_name[i,2], " already installed."))
   } else {
     install.packages(df_name[i,1], type="source", repos=NULL, lib=libPath)



This file wraps all the helper files in one place and invokes all the functions from packages and paths:

# Loading files with function lists and Paths

#updating the list of packages
listPackages <- data.frame(read.csv("packages.txt", header=FALSE))
names(listPackages)[1] <- "name"

#just names of the packages
temp <- strsplit(as.character(listPackages$name),"_")
temp <- data.frame(library=matrix(unlist(temp), ncol=2, byrow=TRUE)[,1])
listPackages<- cbind(name=listPackages, library=temp$library)

#installing the missing packages

library(dplyr, lib.loc=libPath)
library(ggplot2, lib.loc=libPath)
library(knitr, lib.loc=libPath)

4. Start using R

Finally, every new R file or projects needs to have a single line included:


And this will load all the settings, all the packages and make sure the environment downloaded are correctly installed.


As always, complete code is available on Github.

Happy coding and happy Rrrrring ????

To leave a comment for the author, please follow the link and comment on their blog: R – TomazTsql. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)