Repository navigation
Expand file tree
/
Copy pathr_runner.py
More file actions
50 lines (44 loc) · 1.94 KB
/
Copy pathr_runner.py
File metadata and controls
50 lines (44 loc) · 1.94 KB
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
41
42
43
44
45
46
47
48
49
50
import os
import sys
import json
import subprocess
def get_resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
def find_rscript():
portable_r = get_resource_path(os.path.join("R-Portable", "bin", "Rscript.exe"))
if not os.path.exists(portable_r):
raise FileNotFoundError(f"Rscript not found at {portable_r}")
return portable_r
def run_r_analysis(data_path, output_dir="temp", params=None):
if params is None:
params = {
"param_posthoc": "SNK",
"welch_posthoc": "Games_Howell",
"nonparam_posthoc": "Dunn",
"adjust": "BH",
"control": "A",
"force_welch": False
}
os.makedirs(output_dir, exist_ok=True)
params_path = os.path.join(output_dir, "params.json")
with open(params_path, "w", encoding="utf-8") as f:
json.dump(params, f, indent=2)
r_script = get_resource_path("run_stats.R")
if not os.path.exists(r_script):
raise FileNotFoundError(f"R script not found at {r_script}")
if not os.path.exists(data_path):
raise FileNotFoundError(f"Data file not found: {data_path}")
rscript_path = find_rscript()
output_path = os.path.join(output_dir, "Routput.xlsx")
cmd = [rscript_path, r_script, data_path, params_path, output_path]
creationflags = subprocess.CREATE_NO_WINDOW if sys.platform == 'win32' else 0
result = subprocess.run(cmd, capture_output=True, text=True, timeout=300,
encoding='utf-8', errors='ignore', creationflags=creationflags)
if result.returncode != 0:
print("STDERR:", result.stderr)
raise RuntimeError("R execution failed")
if not os.path.exists(output_path):
raise FileNotFoundError(f"R output not generated at {output_path}")
return output_path