-
Notifications
You must be signed in to change notification settings - Fork 59
/
R Integration.pyt
270 lines (218 loc) · 9.13 KB
/
R Integration.pyt
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import absolute_import
import os
import sys
import arcpy
import rtools
from rtools.utils import dedent
class Toolbox(object):
def __init__(self):
self.label = 'R Integration'
self.alias = 'rintegration'
self.tools = [UpdateBindings, InstallBindings, RInstallDetails, RVersion]
# Tool implementation code
class UpdateBindings(object):
def __init__(self):
self.label = 'Update R bindings'
self.description = dedent("""
Update the package that ArcGIS uses to communicate with R.
Checks with the server for any newer releases, and if
they exist, installs the new release.""")
self.canRunInBackground = False
def getParameterInfo(self):
version = arcpy.Parameter()
version.name = 'r_version'
version.displayName = 'Selected R Version (Set As Default)'
version.parameterType = 'Required'
version.direction = 'Input'
version.datatype = 'GPString'
return [version]
def isLicensed(self):
return True
def updateParameters(self, parameters):
validator = getattr(self, 'ToolValidator', None)
if not parameters[0].altered:
parameters[0] = get_rversion_param(parameters[0])
if validator:
return validator(parameters).updateParameters()
def updateMessages(self, parameters):
validator = getattr(self, 'ToolValidator', None)
if validator:
return validator(parameters).updateMessages()
def execute(self, parameters, messages):
if parameters[0].enabled:
set_default_r(parameters[0].value)
rtools.update_package()
class RVersion(object):
def __init__(self):
self.label = 'Print R Version'
self.description = dedent("""\
Print the version of R that ArcGIS is connected to.""")
self.canRunInBackground = False
def getParameterInfo(self):
return []
def isLicensed(self):
return True
def updateParameters(self, parameters):
validator = getattr(self, 'ToolValidator', None)
if validator:
return validator(parameters).updateParameters()
def updateMessages(self, parameters):
validator = getattr(self, 'ToolValidator', None)
if validator:
return validator(parameters).updateMessages()
def execute(self, parameters, messages):
if rtools.r_version() is None:
arcpy.AddError(dedent("""\
R not installed. Please install R prior to using
this toolbox. The R installation can be found at:
http://www.r-project.org/
"""))
else:
arcpy.AddMessage(rtools.r_version())
class RInstallDetails(object):
def __init__(self):
self.label = 'R Installation Details'
self.description = dedent("""\
Show details of R installation. Also detects
the presence of the ArcGIS R bridge, if installed.
""")
self.canRunInBackground = False
def getParameterInfo(self):
# detected R installation directory
r_install = arcpy.Parameter()
r_install.name = 'r_install'
r_install.displayName = 'R Installation Directory'
r_install.parameterType = 'Derived'
r_install.direction = 'Output'
r_install.datatype = 'GPString'
# detected R package library
r_pkgs = arcpy.Parameter()
r_pkgs.name = 'r_pkgs'
r_pkgs.displayName = 'R Package Library'
r_pkgs.parameterType = 'Derived'
r_pkgs.direction = 'Output'
r_pkgs.datatype = 'GPString'
# binding version
bind_ver = arcpy.Parameter()
bind_ver.name = 'bind_ver'
bind_ver.displayName = 'ArcGIS binding version'
bind_ver.parameterType = 'Derived'
bind_ver.direction = 'Output'
bind_ver.datatype = 'GPString'
# binding path
bind_path = arcpy.Parameter()
bind_path.name = 'bind_path'
bind_path.displayName = 'ArcGIS binding path'
bind_path.parameterType = 'Derived'
bind_path.direction = 'Output'
bind_path.datatype = 'GPString'
return [r_install, r_pkgs, bind_ver, bind_path]
def isLicensed(self):
return True
def updateParameters(self, parameters):
validator = getattr(self, 'ToolValidator', None)
if validator:
return validator(parameters).updateParameters()
def updateMessages(self, parameters):
validator = getattr(self, 'ToolValidator', None)
if validator:
return validator(parameters).updateMessages()
def execute(self, parameters, messages):
if rtools.r_path() is None:
arcpy.AddError(dedent("""\
R not installed. Please install R prior to using
this toolbox. The R installation can be found at:
http://www.r-project.org/
"""))
else:
arcpy.AddMessage("R (version {}), installed in: {}".format(
rtools.r_version(), rtools.r_path()))
parameters[0].value = rtools.r_path()
arcpy.AddMessage("R packages will be installed into: {}".format(
rtools.r_lib_path()))
parameters[1].value = rtools.r_lib_path()
arcpy.AddMessage("All R package libraries detected: {}".format(
";".join(rtools.r_all_lib_paths())))
current_package_path = rtools.r_pkg_path()
current_package_version = rtools.r_pkg_version()
if current_package_path is None or current_package_version is None:
arcpy.AddWarning("The ArcGIS R package is not installed."
" Use the 'Install R Bindings' tool to "
"install it.")
else:
arcpy.AddMessage(
"The ArcGIS R package (version {}) is installed at: {}".format(
current_package_version, current_package_path))
parameters[2].value = current_package_version
parameters[3].value = current_package_path
class InstallBindings(object):
def __init__(self):
self.label = 'Install R bindings'
self.description = dedent("""\
Install ArcGIS R bindings onto this machine. R must first
be installed for this command to correctly function.""")
self.canRunInBackground = False
def getParameterInfo(self):
# overwrite existing?
param_1 = arcpy.Parameter()
param_1.name = 'overwrite'
param_1.displayName = 'Overwrite Existing Installation?'
param_1.parameterType = 'Required'
param_1.direction = 'Input'
param_1.datatype = 'GPBoolean'
param_1.value = False
param_2 = arcpy.Parameter()
param_2.name = 'r_version'
param_2.displayName = 'Selected R Version (Set As Default)'
param_2.parameterType = 'Required'
param_2.direction = 'Input'
param_2.datatype = 'GPString'
return [param_1, param_2]
def isLicensed(self):
return True
def updateParameters(self, parameters):
validator = getattr(self, 'ToolValidator', None)
if not parameters[1].altered:
parameters[1] = get_rversion_param(parameters[1])
if validator:
return validator(parameters).updateParameters()
def updateMessages(self, parameters):
validator = getattr(self, 'ToolValidator', None)
if validator:
return validator(parameters).updateMessages()
def execute(self, parameters, messages):
if parameters[1].enabled:
set_default_r(parameters[1].value)
rtools.install_package(overwrite=parameters[0].value)
def set_default_r(current_version):
"""If the user doesn't have a default R configured,
they are asked to set one here in the tool. Update the related
registry keys as needed."""
arcpy.AddMessage("Updating default R to {}".format(current_version))
# get the related data for the version selected
install_path = rtools.r_version_dict()[current_version]
if install_path:
# insert the values into the registry
rtools.r_set_install(install_path, current_version)
def get_rversion_param(parameter):
r_versions = rtools.r_version_dict()
if not r_versions:
# can't find current version, nor recurse.
# R probably isn't installed.
parameter.value = 'R not detected'
parameter.enabled = False
else:
# check the registry 'Current' version only
selected_ver = rtools.r_version(True)
selected_path = r_versions[selected_ver]
if selected_path and os.path.exists(selected_path):
parameter.value = rtools.r_version(True)
parameter.enabled = False
else:
# otherwise, pull up the list of installed versions
# and allow the user to select one to set.
parameter.filter.list = [k for (k, v) in
r_versions.items() if v is not None]
return parameter