Skip to content

Commit 01e64e1

Browse files
committed
add the new ds.sqrt/sqrtDS function
1 parent 7c760fb commit 01e64e1

3 files changed

Lines changed: 203 additions & 0 deletions

File tree

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export(ds.seq)
8484
export(ds.setDefaultOpals)
8585
export(ds.setSeed)
8686
export(ds.skewness)
87+
export(ds.sqrt)
8788
export(ds.subset)
8889
export(ds.subsetByClass)
8990
export(ds.summary)

R/ds.sqrt.R

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#'
2+
#' @title Computes the square root values of a variable
3+
#' @description Computes the square root values for a specified numeric or integer vector.
4+
#' This function is similar to R function \code{sqrt}.
5+
#' @details The function calls the server-side function \code{sqrtDS} that computes the
6+
#' square root values of the elements of a numeric or integer vector and assigns a new vector
7+
#' with those square root values on the server-side. The name of the new generated vector is
8+
#' specified by the user through the argument \code{newobj}, otherwise is named by default to
9+
#' \code{sqrt.newobj}.
10+
#' @param x a character string providing the name of a numeric or an integer vector.
11+
#' @param newobj a character string that provides the name for the output variable
12+
#' that is stored on the data servers. Default name is set to \code{sqrt.newobj}.
13+
#' @param datasources a list of \code{\link{DSConnection-class}} objects obtained after login.
14+
#' If the \code{datasources} argument is not specified the default set of connections will be
15+
#' used: see \code{\link{datashield.connections_default}}.
16+
#' @return \code{ds.sqrt} assigns a vector for each study that includes the square root values of
17+
#' the input numeric or integer vector specified in the argument \code{x}. The created vectors
18+
#' are stored in the servers.
19+
#' @author Demetris Avraam for DataSHIELD Development Team
20+
#' @export
21+
#' @examples
22+
#' \dontrun{
23+
#'
24+
#' # Connecting to the Opal servers
25+
#'
26+
#' require('DSI')
27+
#' require('DSOpal')
28+
#' require('dsBaseClient')
29+
#'
30+
#' builder <- DSI::newDSLoginBuilder()
31+
#' builder$append(server = "study1",
32+
#' url = "http://192.168.56.100:8080/",
33+
#' user = "administrator", password = "datashield_test&",
34+
#' table = "CNSIM.CNSIM1", driver = "OpalDriver")
35+
#' builder$append(server = "study2",
36+
#' url = "http://192.168.56.100:8080/",
37+
#' user = "administrator", password = "datashield_test&",
38+
#' table = "CNSIM.CNSIM2", driver = "OpalDriver")
39+
#' builder$append(server = "study3",
40+
#' url = "http://192.168.56.100:8080/",
41+
#' user = "administrator", password = "datashield_test&",
42+
#' table = "CNSIM.CNSIM3", driver = "OpalDriver")
43+
#'
44+
#' logindata <- builder$build()
45+
#'
46+
#' # Log onto the remote Opal training servers
47+
#' connections <- DSI::datashield.login(logins = logindata, assign = TRUE, symbol = "D")
48+
#'
49+
#' # Example 1: Get the square root of LAB_HDL variable
50+
#' ds.sqrt(x='D$LAB_HDL', newobj='LAB_HDL.sqrt', datasources=connections)
51+
#' # compare the mean of LAB_HDL and of LAB_HDL.sqrt
52+
#' # Note here that the number of missing values is bigger in the LAB_HDL.sqrt
53+
# # vector, because the variable LAB_HDL includes negative values which
54+
# # don't have real square roots and those values are returned as NAs.
55+
#' ds.mean(x='D$LAB_HDL', datasources=connections)
56+
#' ds.mean(x='LAB_HDL.sqrt', datasources=connections)
57+
#'
58+
#' # Example 2: Generate a repeated vector of the squares of integers from 1 to 10
59+
#' # and get their square roots
60+
#' ds.make(toAssign='rep((1:10)^2, times=10)', newobj='squares.vector', datasources=connections)
61+
#' ds.sqrt(x='squares.vector', newobj='sqrt.vector', datasources=connections)
62+
#' # check the behavior of that operation by comparing the tables of squares.vector and sqrt.vector
63+
#' ds.table(rvar='squares.vector')$output.list$TABLE_rvar.by.study_counts
64+
#' ds.table(rvar='sqrt.vector')$output.list$TABLE_rvar.by.study_counts
65+
#'
66+
#' # clear the Datashield R sessions and logout
67+
#' datashield.logout(connections)
68+
#'
69+
#' }
70+
#'
71+
ds.sqrt <- function(x=NULL, newobj=NULL, datasources=NULL){
72+
73+
# look for DS connections
74+
if(is.null(datasources)){
75+
datasources <- datashield.connections_find()
76+
}
77+
78+
if(is.null(x)){
79+
stop("Please provide the name of the input object!", call.=FALSE)
80+
}
81+
82+
# the input variable might be given as column table (i.e. D$x)
83+
# or just as a vector not attached to a table (i.e. x)
84+
# we have to make sure the function deals with each case
85+
xnames <- extract(x)
86+
varname <- xnames$elements
87+
obj2lookfor <- xnames$holders
88+
89+
# check if the input object(s) is(are) defined in all the studies
90+
if(is.na(obj2lookfor)){
91+
defined <- isDefined(datasources, varname)
92+
}else{
93+
defined <- isDefined(datasources, obj2lookfor)
94+
}
95+
96+
# call the internal function that checks the input object is of the same class in all studies.
97+
typ <- checkClass(datasources, x)
98+
99+
# call the internal function that checks the input object(s) is(are) of the same class in all studies.
100+
if(!('numeric' %in% typ) && !('integer' %in% typ)){
101+
stop("Only objects of type 'numeric' or 'integer' are allowed.", call.=FALSE)
102+
}
103+
104+
# create a name by default if the user did not provide a name for the new variable
105+
if(is.null(newobj)){
106+
newobj <- "sqrt.newobj"
107+
}
108+
109+
# call the server side function that does the operation
110+
cally <- call("sqrtDS", x)
111+
DSI::datashield.assign(datasources, newobj, cally)
112+
113+
# check that the new object has been created and display a message accordingly
114+
finalcheck <- isAssigned(datasources, newobj)
115+
116+
}

man/ds.sqrt.Rd

Lines changed: 86 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)