|
| 1 | +#' |
| 2 | +#' @title Computes the absolute values of a variable |
| 3 | +#' @description Computes the absolute values for a specified numeric or integer vector. |
| 4 | +#' This function is similar to R function \code{abs}. |
| 5 | +#' @details The function calls the server-side function \code{absDS} that computes the |
| 6 | +#' absolute values of the elements of a numeric or integer vector and assigns a new vector |
| 7 | +#' with those absolute 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{abs.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{abs.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.abs} assigns a vector for each study that includes the absolute 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: Generate a normally distributed variable with zero mean and variance equal |
| 50 | +#' # to one and then get their absolute values |
| 51 | +#' ds.rNorm(samp.size=100, mean=0, sd=1, newobj='var.norm', datasources=connections) |
| 52 | +#' # check the quantiles |
| 53 | +#' ds.summary(x='var.norm', datasources=connections) |
| 54 | +#' ds.abs(x='var.norm', newobj='var.norm.abs', datasources=connections) |
| 55 | +#' # check now the changes in the quantiles |
| 56 | +#' ds.summary(x='var.norm.abs', datasources=connections) |
| 57 | +#' |
| 58 | +#' # Example 2: Generate a sequence of negative integer numbers from -200 to -100 |
| 59 | +#' # and then get their absolute values |
| 60 | +#' ds.seq(FROM.value.char = '-200', TO.value.char = '-100', BY.value.char = '1', |
| 61 | +#' newobj='negative.integers', datasources=connections) |
| 62 | +#' # check the quantiles |
| 63 | +#' ds.summary(x='negative.integers', datasources=connections) |
| 64 | +#' ds.abs(x='negative.integers', newobj='positive.integers', datasources=connections) |
| 65 | +#' # check now the changes in the quantiles |
| 66 | +#' ds.summary(x='positive.integers', datasources=connections) |
| 67 | +#' |
| 68 | +#' # clear the Datashield R sessions and logout |
| 69 | +#' datashield.logout(connections) |
| 70 | +#' |
| 71 | +#' } |
| 72 | +#' |
| 73 | +ds.abs <- function(x=NULL, newobj=NULL, datasources=NULL){ |
| 74 | + |
| 75 | + # look for DS connections |
| 76 | + if(is.null(datasources)){ |
| 77 | + datasources <- datashield.connections_find() |
| 78 | + } |
| 79 | + |
| 80 | + if(is.null(x)){ |
| 81 | + stop("Please provide the name of the input object!", call.=FALSE) |
| 82 | + } |
| 83 | + |
| 84 | + # the input variable might be given as column table (i.e. D$x) |
| 85 | + # or just as a vector not attached to a table (i.e. x) |
| 86 | + # we have to make sure the function deals with each case |
| 87 | + xnames <- extract(x) |
| 88 | + varname <- xnames$elements |
| 89 | + obj2lookfor <- xnames$holders |
| 90 | + |
| 91 | + # check if the input object(s) is(are) defined in all the studies |
| 92 | + if(is.na(obj2lookfor)){ |
| 93 | + defined <- isDefined(datasources, varname) |
| 94 | + }else{ |
| 95 | + defined <- isDefined(datasources, obj2lookfor) |
| 96 | + } |
| 97 | + |
| 98 | + # call the internal function that checks the input object is of the same class in all studies. |
| 99 | + typ <- checkClass(datasources, x) |
| 100 | + |
| 101 | + # call the internal function that checks the input object(s) is(are) of the same class in all studies. |
| 102 | + if(!('numeric' %in% typ) && !('integer' %in% typ)){ |
| 103 | + stop("Only objects of type 'numeric' or 'integer' are allowed.", call.=FALSE) |
| 104 | + } |
| 105 | + |
| 106 | + # create a name by default if the user did not provide a name for the new variable |
| 107 | + if(is.null(newobj)){ |
| 108 | + newobj <- "abs.newobj" |
| 109 | + } |
| 110 | + |
| 111 | + # call the server side function that does the operation |
| 112 | + cally <- call("absDS", x) |
| 113 | + DSI::datashield.assign(datasources, newobj, cally) |
| 114 | + |
| 115 | + # check that the new object has been created and display a message accordingly |
| 116 | + finalcheck <- isAssigned(datasources, newobj) |
| 117 | + |
| 118 | +} |
0 commit comments