/
thermacrofunctions.jsee
187 lines (171 loc) · 4.86 KB
/
thermacrofunctions.jsee
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
function startR(which){
switch (which){
case 1:
rexe = 'rgui.exe\" --internet2'
rexepath = '"'+rbin+''+rexe
RunExe(rexepath)
break;
case 2:
rexe = 'rterm.exe\" --internet2'
rexepath = '"'+rbin+''+rexe
RunExe(rexepath)
break;
}
}
function getRWnd(target) {
switch (target) {
case 1: // RGui
rguiw = shell.FindWindow("Rgui Workspace", "");
if (rguiw) {
targwnd = getChildProcID(rguiw)
} else {
targwnd = shell.FindWindow("RGui", "");
}
break;
default: // rterm
targwnd = shell.FindWindow("ConsoleWindowClass","Rterm")
}
return targwnd;
}
function getChildProcID(parentWnd) {
// Required to find console running in RGui
var childWnd=0;
if(parentWnd) {
Cwnds = parentWnd.Children
for ( i=1;i<=Cwnds.Count;i++) {
if (Cwnds.item(i).Caption == "R Console") {
childWnd = Cwnds.item(i) //.ProcessID
break;
}
}
}
return childWnd;
}
function getAnyPid(skip) {
guipid = getRWnd(1);
if (guipid) {guipid = getRWnd(1) } else {guipid = getRWnd(2)}
return guipid;
}
function createTempFile(rcode) {
tmpPath='';
try {
fso = new ActiveXObject("Scripting.FileSystemObject");
tmpPath = fso.BuildPath(fso.GetSpecialFolder(2),fso.GetTempName());
tf = fso.CreateTextFile(tmpPath, true);
tf.WriteLine(rcode) ;
tf.Close();
}
catch (e) {
OutputBar.Visible = true;
OutputBar.Writeln(e.message)
}
finally {
tf=null;
fso=null;
}
return tmpPath;
}
function getRCode(skipSelectAll){
skip = skipSelectAll?true:false;
rcode = editor.ActiveDocument.selection.Text;
Col=1;Row=1;
if (rcode == ''){
if (!skip){
Col=editor.ActiveDocument.selection.GetActivePointX(eePosView);
Row=editor.ActiveDocument.selection.GetActivePointY(eePosView);
editor.ActiveDocument.selection.SelectAll();
rcode = editor.ActiveDocument.selection.Text;
editor.ActiveDocument.selection.SetActivePoint(eePosView,Col,Row); //reset cursor to where it was prior to selectAll
}
}
return rcode;
}
function SendRCommand(wnd,rcmd,rcode) {
if (wnd) {
WshShell = new ActiveXObject( "WScript.Shell" )
if(rcode){
SendRC = rcmd + '+9' + rcode + '+0 ~'
} else {
SendRC = rcmd + ' ~'}
wnd.SetForeground();
if ((shell.ForegroundWindow).ProcessID != wnd.ProcessID) {
wnd.SetForeground();
while ((shell.ForegroundWindow).ProcessID != wnd.ProcessID || retry > 10) {sleep(100); wnd.SetForeground(); retry++}
}
WshShell.SendKeys( SendRC )
} else {
OutputBar.Writeln("No instance of R seems to be running") }
}
function getDocPath() {
docPath = editor.ActiveDocument.FullName;
if (docPath =='' || editor.ActiveDocument.selection.Text !=''){
docPath = createTempFile(getRCode())
}
return docPath;
}
function RunExe(rexe) {
var RunExeShell = new ActiveXObject( "WScript.Shell" );
// OutputBar.Writeln(rexepath);
x = RunExeShell.Run(rexe,1); //,1,false;
sleep(100);
}
function execRtoEmed(rpath,f){
results='';
exec = '"'+ rpath + 'rterm.exe" --vanilla -q --slave --file="' + f.replace(/\\/g,'/') +'"' //statement to execute
WshShell = new ActiveXObject( "WScript.Shell" );
OutputBar.Visible = true;
OutputBar.Clear()
OutputBar.Writeln(exec)
exec=WshShell.Exec(exec),0,true;
errmsg='';
while(!exec.stderr.AtEndOfStream){
errmsg = (exec.stderr.ReadAll() )
}
while(!exec.stdout.AtEndOfStream){
results = (exec.stdout.ReadAll() )
}
WshShell=null
if (errmsg!=''){
OutputBar.Writeln(errmsg);
OutputBar.Writeln(results)
} else {
OutputBar.Writeln(results)
}
}
function scrubSendKeys(skinput) {
return ((skinput.replace(/\(/g,"+9")).replace(/\)/g,"+0")).replace(/~/g,'{~}');
}
function loadDataFile(f,delim){
loadstring =''
varname='datafile'
if(f){
f = '"'+f+'"'
varname = (editor.ActiveDocument.Name).substring(0,30)
} else {
f='file.choose()'
}
delim?delim:''
switch (delim) {
case ',':
loadstring = varname + ' <- read.csv('+ f + '); str(' + varname + ')'
break;
case ';':
loadstring = varname + ' <- read.csv2('+ f + '); str(' + varname + ')'
break;
case '\t':
loadstring = varname + ' <- read.delim('+ f + '); str(' + varname + ')'
break;
default:
loadstring = varname + ' <- read.table('+ f + ', sep = "' + delim + '"); str(' + varname + ')'
}
return loadstring;
}
function RunUpgradeR() {
// Assists in migrating libraries from old R, to new R
// Based on http://www.r-statistics.com/2010/04/changing-your-r-upgrading-strategy-and-the-r-code-to-do-it-on-windows/
// http://stackoverflow.com/questions/1401904/painless-way-to-install-a-new-version-of-r
// http://cran.r-project.org/bin/windows/base/rw-FAQ.html#What_0027s-the-best-way-to-upgrade_003f
//1) download new R and install rcmd = (sort(.packages(all.available = TRUE)))
//2) copy the “library” content from the old R to the new R
//3) upgrade all of the packages (in the library folder) to the new version of R. rcmd = update.packages(checkBuilt=TRUE, ask=FALSE)
}