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 Functions to prepare a setup on Windows, R functions 

4""" 

5import sys 

6import os 

7from ..installhelper.install_cmd_helper import run_cmd 

8 

9if sys.version_info[0] == 2: 

10 FileNotFoundError = Exception 

11 

12 

13_script = os.path.join( 

14 os.path.abspath(os.path.dirname(__file__)), "R_install.r") 

15 

16 

17class RBatchException(Exception): 

18 

19 """ 

20 raised when running R in batch mode 

21 """ 

22 pass 

23 

24 

25def r_run_script(r_path, script, output=None): 

26 """ 

27 run a script on R 

28 

29 @param r_path r location 

30 @param script script to run 

31 @param output where to store the output (can be None) 

32 @return output 

33 """ 

34 exe = os.path.join(r_path, "bin", "x64", "R.exe") 

35 if not os.path.exists(exe): 

36 raise FileNotFoundError(exe) 

37 

38 os.environ["R_LIBS"] = os.path.join(r_path, "library") 

39 cmd = [exe, "CMD", "BATCH", script] 

40 if output is not None: 

41 cmd.append(output) 

42 cmd = " ".join(cmd) 

43 out, err = run_cmd(cmd, wait=True) 

44 if err is not None and len(err) > 0: 

45 raise RBatchException( 

46 "CMD:\n{0}\nOUT:\n{1}\nERR--G:\n{2}".format(cmd, out, err)) 

47 return out 

48 

49 

50def get_package_description(r_path, pack): 

51 """ 

52 returns the description of an R package as a dictionary 

53 

54 @param r_path path to R 

55 @param pack package name 

56 @return dictionary 

57 """ 

58 path = os.path.join(r_path, "library", pack) 

59 if not os.path.exists(path): 

60 raise FileNotFoundError(path) 

61 version = os.path.join(path, "DESCRIPTION") 

62 if not os.path.exists(version): 

63 raise FileNotFoundError(version) 

64 with open(version, "r") as f: 

65 lines = f.readlines() 

66 res = {} 

67 for line in lines: 

68 spl = line.split(":") 

69 if len(spl) > 1: 

70 key = spl[0] 

71 val = ":".join(spl[1:]) 

72 res[key] = val.strip("\r\n\t ") 

73 return res