install-r.ps1 1.37 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Install R 
Write-Output "Installing R"

Write-Output "Downloading R Last Version"
## Download R engine for Windows machine

## download last relase name
$urlR = "https://cran.biotools.fr/bin/windows/base/release.html"
$outputR = 'c:\Windows\Temp\R-win.exe'
$wcR = New-Object System.Net.WebClient
$res = $wcR.DownloadString($urlR)

## get release name in the HTML page
$pos1 = $res.IndexOf("URL=")
$pos2 = $res.LastIndexOf('"')
$Rexe = $res.Substring($pos1+4, $pos2-$pos1-4)
$urlR = "https://cran.biotools.fr/bin/windows/base/" + $Rexe

Write-Output "Downloading $($urlR)"
#Download the exe
$wcR.DownloadFile($urlR, $outputR)

Write-Output "Download completed $Rexe"

$RDir = 'C:\R\'
Write-Output "Install $outputR into $RDir"
Start-Process -FilePath $outputR -ArgumentList "/VERYSILENT /DIR=$RDir" -Wait -NoNewWindow

if ( Test-Path "HKLM:\Software\R-core\R" ) {
	$Rver = (Get-ItemProperty -Path "HKLM:\Software\R-core\R")."Current Version"
	Write-Output "R version $($Rver) installed"
}
else {
	throw "ERROR: Can't install R $($urlR)"
}

# Add R to PATH
$oldpath = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path
$newPath = "$oldpath;C:\R\bin"
Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newPath
41
Remove-Item $outputR -Force