-
Notifications
You must be signed in to change notification settings - Fork 23
/
ds.matrix.R
291 lines (272 loc) · 12 KB
/
ds.matrix.R
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#' @title Creates a matrix on the server-side
#' @description Creates a matrix on the server-side
#' with dimensions specified by \code{nrows.scalar}
#' and \code{ncols.scalar} arguments and assigns the
#' values of all its elements based on the \code{mdata} argument.
#' @details This function is similar to the R native function \code{matrix()}.
#'
#' If in the \code{mdata} argument a vector is specified this should have
#' the same length as the total number of elements
#' in the matrix. If this is not TRUE the values in \code{mdata}
#' will be used repeatedly until all elements in the matrix are full.
#' If \code{mdata} argument is a scalar, all elements in the matrix will take that value.
#'
#'
#' In the \code{nrows.scalar} argument can be a character string specifying
#' the name of a server-side scalar. For example,
#' if a server-side scalar named \code{ss.scalar} exists and holds the value 23,
#' then by specifying \code{nrows.scalar = "ss.scalar"}, the matrix created will
#' have 23 rows. Also this argument can be
#' a numeric value from the
#' client-side. The same rules are applied to \code{ncols.scalar} argument but in this
#' case the column numbers are specified.
#' In both arguments a zero, negative, NULL or missing value is not permitted.
#'
#'
#' Server function called: \code{matrixDS}
#'
#' @param mdata a character string specifying
#' the name of a server-side scalar or vector. Also, a numeric value representing a
#' scalar specified from the client-side can be speficied.
#' Zeros, negative values and NAs are all allowed.
#' For more information see \strong{Details}.
#' @param from a character string specifying the source and nature of \code{mdata}.
#' This can be set as \code{"serverside.vector"}, \code{"serverside.scalar"}
#' or \code{"clientside.scalar"}. Default \code{"clientside.scalar"}.
#' @param nrows.scalar an integer or a character string that specifies the number
#' of rows in the matrix to be created.
#' For more information see \strong{Details}.
#' @param ncols.scalar an integer or a character string that specifies
#' the number of columns in the matrix to be created.
#' @param byrow logical. If TRUE and \code{mdata} is a vector the matrix
#' created should be filled row by row. If FALSE the matrix created should
#' be filled column by column. Default = FALSE.
#' @param dimnames a list of length 2 giving
#' the row and column names respectively.
#' @param newobj a character string that provides the name for the output
#' variable that is stored on the data servers. Default \code{matrix.newobj}.
#' @param datasources a list of \code{\link{DSConnection-class}}
#' objects obtained after login. If the \code{datasources} argument is not specified
#' the default set of connections will be used: see \code{\link{datashield.connections_default}}.
#' @return \code{ds.matrix} returns the created matrix which is written on the server-side.
#' In addition, two validity messages are returned
#' indicating whether the new matrix has been created in each data source and if so whether
#' it is in a valid form.
#' @author DataSHIELD Development Team
#' @examples
#' \dontrun{
#'
#' ## Version 6, for version 5 see the Wiki
#'
#' # connecting to the Opal servers
#'
#' require('DSI')
#' require('DSOpal')
#' require('dsBaseClient')
#'
#' builder <- DSI::newDSLoginBuilder()
#' builder$append(server = "study1",
#' url = "http://192.168.56.100:8080/",
#' user = "administrator", password = "datashield_test&",
#' table = "CNSIM.CNSIM1", driver = "OpalDriver")
#' builder$append(server = "study2",
#' url = "http://192.168.56.100:8080/",
#' user = "administrator", password = "datashield_test&",
#' table = "CNSIM.CNSIM2", driver = "OpalDriver")
#' builder$append(server = "study3",
#' url = "http://192.168.56.100:8080/",
#' user = "administrator", password = "datashield_test&",
#' table = "CNSIM.CNSIM3", driver = "OpalDriver")
#' logindata <- builder$build()
#'
#' connections <- DSI::datashield.login(logins = logindata, assign = TRUE, symbol = "D")
#'
#' #Example 1: create a matrix with -13 value in all elements
#'
#' ds.matrix(mdata = -13,
#' from = "clientside.scalar",
#' nrows.scalar = 3,
#' ncols.scalar = 8,
#' newobj = "cs.block",
#' datasources = connections)
#'
#' #Example 2: create a matrix of missing values
#'
#' ds.matrix(NA,
#' from = "clientside.scalar",
#' nrows.scalar = 4,
#' ncols.scalar = 5,
#' newobj = "cs.block.NA",
#' datasources = connections)
#'
#' #Example 3: create a matrix using a server-side vector
#' #create a vector in the server-side
#'
#' ds.rUnif(samp.size = 45,
#' min = -10.5,
#' max = 10.5,
#' newobj = "ss.vector",
#' seed.as.integer = 8321,
#' force.output.to.k.decimal.places = 0,
#' datasources = connections)
#'
#' ds.matrix(mdata = "ss.vector",
#' from = "serverside.vector",
#' nrows.scalar = 5,
#' ncols.scalar = 9,
#' newobj = "sv.block",
#' datasources = connections)
#'
#' #Example 4: create a matrix using a server-side vector and specifying
#' #the row a column names
#'
#' ds.rUnif(samp.size = 9,
#' min = -10.5,
#' max = 10.5,
#' newobj = "ss.vector.9",
#' seed.as.integer = 5575,
#' force.output.to.k.decimal.places = 0,
#' datasources = connections)
#'
#' ds.matrix(mdata = "ss.vector.9",
#' from = "serverside.vector",
#' nrows.scalar = 5,
#' ncols.scalar = 9,
#' byrow = TRUE,
#' dimnames = list(c("a","b","c","d","e")),
#' newobj = "sv.block.9.dimnames1",
#' datasources = connections)
#'
#'
#' # clear the Datashield R sessions and logout
#' datashield.logout(connections)
#' }
#'
#' @export
ds.matrix<-function(mdata = NA, from="clientside.scalar",nrows.scalar=NULL, ncols.scalar=NULL, byrow = FALSE,
dimnames = NULL, newobj=NULL, datasources=NULL){
# look for DS connections
if(is.null(datasources)){
datasources <- datashield.connections_find()
}
# check if a value has been provided for mdata
if(is.null(mdata)){
return("Error: mdata must be a character string, a numeric vector or a scalar")
}
# if no value spcified for output object, then specify a default
if(is.null(newobj))
{
newobj <- "matrix.newobj"
}
#process mdata to make transmittable depending on <from>
#Check that valid from has been specified
if(from!="serverside.vector"&&from!="serverside.scalar"&&from!="clientside.scalar")
{
cat(" FAILED: <from> must be specified as one of the following - 'serverside.vector',
'serverside.scalar', 'clientside.scalar'\n\n")
return('Please respecify')
}
if(from=="serverside.vector"||from=="serverside.scalar")
{
mdata.transmit<-mdata
}
if(from=="clientside.scalar")
{
mdata.transmit<-paste0(as.character(mdata),collapse=",")
}
#process <nrows> and <ncols> to make transmittable depending on specified from
#in principle acceptable values of <nrows> and <ncols> cannot be negative
if(is.null(nrows.scalar))
{
nrows.scalar<-c(-9)
}
nrows.transmit<-paste0(as.character(nrows.scalar),collapse=",")
if(is.null(ncols.scalar))
{
ncols.scalar<-c(-9)
}
ncols.transmit<-paste0(as.character(ncols.scalar),collapse=",")
# CALL THE MAIN SERVER SIDE FUNCTION
calltext <- call("matrixDS", mdata.transmit, from, nrows.transmit, ncols.transmit, byrow, dimnames)
DSI::datashield.assign(datasources, newobj, calltext)
#############################################################################################################
#DataSHIELD CLIENTSIDE MODULE: CHECK KEY DATA OBJECTS SUCCESSFULLY CREATED #
#
#SET APPROPRIATE PARAMETERS FOR THIS PARTICULAR FUNCTION #
test.obj.name<-newobj #
#
#TRACER #
#return(test.obj.name) #
#} #
#
#
# CALL SEVERSIDE FUNCTION #
calltext <- call("testObjExistsDS", test.obj.name) #
#
object.info<-DSI::datashield.aggregate(datasources, calltext) #
#
# CHECK IN EACH SOURCE WHETHER OBJECT NAME EXISTS #
# AND WHETHER OBJECT PHYSICALLY EXISTS WITH A NON-NULL CLASS #
num.datasources<-length(object.info) #
#
#
obj.name.exists.in.all.sources<-TRUE #
obj.non.null.in.all.sources<-TRUE #
#
for(j in 1:num.datasources){ #
if(!object.info[[j]]$test.obj.exists){ #
obj.name.exists.in.all.sources<-FALSE #
} #
if(is.null(object.info[[j]]$test.obj.class) || object.info[[j]]$test.obj.class=="ABSENT"){ #
obj.non.null.in.all.sources<-FALSE #
} #
} #
#
if(obj.name.exists.in.all.sources && obj.non.null.in.all.sources){ #
#
return.message<- #
paste0("A data object <", test.obj.name, "> has been created in all specified data sources") #
#
#
}else{ #
#
return.message.1<- #
paste0("Error: A valid data object <", test.obj.name, "> does NOT exist in ALL specified data sources") #
#
return.message.2<- #
paste0("It is either ABSENT and/or has no valid content/class,see return.info above") #
#
return.message.3<- #
paste0("Please use ds.ls() to identify where missing") #
#
#
return.message<-list(return.message.1,return.message.2,return.message.3) #
#
} #
#
calltext <- call("messageDS", test.obj.name) #
studyside.message<-DSI::datashield.aggregate(datasources, calltext) #
#
no.errors<-TRUE #
for(nd in 1:num.datasources){ #
if(studyside.message[[nd]]!="ALL OK: there are no studysideMessage(s) on this datasource"){ #
no.errors<-FALSE #
} #
} #
#
#
if(no.errors){ #
validity.check<-paste0("<",test.obj.name, "> appears valid in all sources") #
return(list(is.object.created=return.message,validity.check=validity.check)) #
} #
#
if(!no.errors){ #
validity.check<-paste0("<",test.obj.name,"> invalid in at least one source. See studyside.messages:") #
return(list(is.object.created=return.message,validity.check=validity.check, #
studyside.messages=studyside.message)) #
} #
#
#END OF CHECK OBJECT CREATED CORECTLY MODULE #
#############################################################################################################
}
#ds.matrix