forked from GoldenCheetah/GoldenCheetah
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREmbed.cpp
212 lines (189 loc) · 6.22 KB
/
REmbed.cpp
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright (c) 2016 Mark Liversedge ([email protected])
*
* Additionally, for the original source used as a basis for this (RInside.cpp)
* Released under the same GNU public license.
*
* Copyright (C) 2009 Dirk Eddelbuettel
* Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "REmbed.h"
#include "RTool.h"
#include "Settings.h"
#include <stdexcept>
#include <QMessageBox>
static const char *name = "GoldenCheetah";
// no setenv on windows
#if WIN32
int setenv(QString name, QString value, bool overwrite)
{
int errcode = 0;
if(!overwrite) {
size_t envsize = 0;
errcode = getenv_s(&envsize, NULL, 0, name.toLatin1().constData());
if(errcode || envsize) return errcode;
}
// make the update
return _putenv_s(name.toLatin1().constData(), value.toLatin1().constData());
}
#endif
REmbed::~REmbed()
{
R_dot_Last();
R_RunExitFinalizers();
R_CleanTempDir();
Rf_endEmbeddedR(0);
}
// Windows API defines Read and Write Console macros
// You think they'd realise by now that generic terms
// should be avoided. But no, blissfully unaware that
// there are millions of lines of code out there they
// can wantanly break. Fuckwits.
#ifdef WIN32
#ifdef ReadConsole
#undef ReadConsole
#undef WriteConsole
#endif
#endif
REmbed::REmbed(const bool verbose, const bool interactive) : verbose(verbose), interactive(interactive)
{
loaded = false;
// need to load the library
RLibrary rlib;
if (!rlib.load()) {
// disable for future
appsettings->setValue(GC_EMBED_R, false);
rlib.errors.append("\nR has now been disabled in options.");
QMessageBox msg(QMessageBox::Information,
"Failed to load R library",
rlib.errors.join("\n"));
msg.exec();
return;
}
// we need to tell embedded R where to work
QString envR_HOME(getenv("R_HOME"));
QString configR_HOME = appsettings->value(NULL,GC_R_HOME,"").toString();
if (envR_HOME == "") {
if (configR_HOME == "") {
qDebug()<<"R HOME not set, R disabled";
return;
} else {
setenv("R_HOME", configR_HOME.toLatin1().constData(), true);
}
}
// fire up R
const char *R_argv[] = {name, "--gui=none", "--no-save",
"--no-readline", "--silent", "--vanilla", "--slave"};
int R_argc = sizeof(R_argv) / sizeof(R_argv[0]);
Rf_initEmbeddedR(R_argc, (char**)R_argv);
R_ReplDLLinit(); // this is to populate the repl console buffers
structRstart Rst;
R_DefParams(&Rst);
#ifdef WIN32
Rst.rhome = getenv("R_HOME");
Rst.home = getRUser();
Rst.CharacterMode = LinkDLL;
Rst.ReadConsole = &RTool::R_ReadConsoleWin;
Rst.WriteConsole = &RTool::R_WriteConsole;
Rst.WriteConsoleEx = &RTool::R_WriteConsoleEx;
Rst.CallBack = &RTool::R_ProcessEvents;
Rst.ShowMessage = &RTool::R_ShowMessage;
Rst.YesNoCancel = &RTool::R_YesNoCancel;
Rst.Busy = &RTool::R_Busy;
#endif
Rst.R_Interactive = (Rboolean) interactive; // sets interactive() to eval to false
R_SetParams(&Rst);
loaded = true;
}
// this is a non-throwing version returning an error code
int REmbed::parseEval(QString line, SEXP & ans) {
ParseStatus status;
SEXP cmdSexp, cmdexpr = R_NilValue;
int i, errorOccurred;
program << line;
PROTECT(cmdSexp = Rf_allocVector(STRSXP, 1));
SET_STRING_ELT(cmdSexp, 0, Rf_mkChar(program.join(" ").toStdString().c_str()));
cmdexpr = PROTECT(R_ParseVector(cmdSexp, -1, &status, R_NilValue));
switch (status){
case PARSE_OK:
// Loop is needed here as EXPSEXP might be of length > 1
for(i = 0; i < Rf_length(cmdexpr); i++){
ans = R_tryEval(VECTOR_ELT(cmdexpr, i), R_GlobalEnv, &errorOccurred);
if (errorOccurred) {
if (verbose) Rf_warning("%s: Error in evaluating R code (%d)\n", name, status);
UNPROTECT(2);
program.clear();
return 1;
}
if (verbose) {
Rf_PrintValue(ans);
}
}
program.clear();
break;
case PARSE_INCOMPLETE:
// need to read another line
break;
case PARSE_NULL:
if (verbose) Rf_warning("%s: ParseStatus is null (%d)\n", name, status);
UNPROTECT(2);
program.clear();
return 1;
break;
case PARSE_ERROR:
if (verbose) Rf_error("Parse Error: \"%s\"\n", line.toStdString().c_str());
UNPROTECT(2);
program.clear();
return 1;
break;
case PARSE_EOF:
if (verbose) Rf_warning("%s: ParseStatus is eof (%d)\n", name, status);
break;
default:
if (verbose) Rf_warning("%s: ParseStatus is not documented %d\n", name, status);
UNPROTECT(2);
program.clear();
return 1;
break;
}
UNPROTECT(2);
return 0;
}
void REmbed::parseEvalQ(QString line) {
SEXP ans;
int rc = parseEval(line, ans);
if (rc != 0) {
throw std::runtime_error(std::string("Error evaluating: ") + line.toStdString());
}
}
void REmbed::parseEvalQNT(QString line) {
SEXP ans;
parseEval(line, ans);
}
SEXP REmbed::parseEval(QString line) {
SEXP ans;
int rc = parseEval(line, ans);
if (rc != 0) {
throw std::runtime_error(std::string("Error evaluating: ") + line.toStdString());
}
return ans;
}
SEXP REmbed::parseEvalNT(QString line) {
SEXP ans;
parseEval(line, ans);
return ans;
}