Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2@file 

3@brief Various functions to install `R <http://www.r-project.org/>`_. 

4""" 

5from __future__ import print_function 

6import sys 

7import re 

8import os 

9 

10from ..installhelper.install_cmd_helper import run_cmd 

11from .install_custom import download_page, download_file 

12 

13 

14def get_R_version(): 

15 """ 

16 returns the version of installed R, 

17 we only focus on the x64 version 

18 

19 @return tuple (bin, version), None if R is not installed 

20 """ 

21 if sys.platform.startswith("win"): 

22 path = "{0}\\R".format(os.environ["ProgramFiles"]) 

23 if os.path.exists(path): 

24 vers = os.listdir(path) 

25 vers.sort() 

26 if len(vers) == 0: 

27 return None 

28 vers = vers[-1] 

29 bin = os.path.join(path, vers, "bin", "x64") 

30 if not os.path.exists(bin): 

31 return None 

32 return (bin, vers) 

33 return None 

34 else: 

35 raise NotImplementedError("not available on platform " + sys.platform) 

36 

37 

38def IsRInstalled(): 

39 """ 

40 @return True of False whether or not it was installed 

41 """ 

42 r = get_R_version() 

43 return r is not None 

44 

45 

46def install_R( 

47 temp_folder=".", fLOG=print, install=True, force_download=False, version=None): 

48 """ 

49 Install `R <http://www.r-project.org/>`_. 

50 It does not do it a second time if it is already installed. 

51 

52 @param temp_folder where to download the setup 

53 @param fLOG logging function 

54 @param install install (otherwise only download) 

55 @param force_download force the downloading of R 

56 @param version None for last, otherwise requested version 

57 @return temporary file 

58 """ 

59 bb = IsRInstalled() 

60 if bb and not force_download: 

61 return True 

62 

63 link = "http://cran.univ-paris1.fr/bin/windows/base/" 

64 page = download_page(link) 

65 if sys.platform.startswith("win"): 

66 reg = re.compile("href=\\\"(.*?[.]exe)\\\"") 

67 alls = reg.findall(page) 

68 if len(alls) == 0: 

69 raise Exception( 

70 "unable to find a link on a .exe file on page: " + 

71 page) 

72 

73 url = link + alls[0] 

74 if version is not None: 

75 spl = url.split("/R-") 

76 url = spl[0] + "/old/" + version + "/R-" + version + "-win.exe" 

77 full = url.split("/")[-1] 

78 outfile = os.path.join(temp_folder, full) 

79 fLOG("[pymy] download ", url) 

80 local = download_file(url, outfile) 

81 if install and not bb: 

82 run_cmd("msiexec /i " + local, fLOG=fLOG, wait=True) 

83 return local 

84 else: 

85 raise NotImplementedError("not available on platform " + sys.platform)