|
| 1 | +#' @title Converts a server-side R object into a data frame |
| 2 | +#' @description Coerces an R object into a data frame maintaining original |
| 3 | +#' class for all columns in data frames. |
| 4 | +#' @details This function is based on the native R function \code{data.frame}. |
| 5 | +#' |
| 6 | +#' Server function called: \code{asDataFrameDS}. |
| 7 | +#' @param x.name a character string providing the name of the input object to be coerced to |
| 8 | +#' a data frame |
| 9 | +#' @param newobj a character string that provides the name for the output object |
| 10 | +#' that is stored on the data servers. Default \code{asdataframe.newobj}. |
| 11 | +#' @param datasources a list of \code{\link[DSI]{DSConnection-class}} |
| 12 | +#' objects obtained after login. If the \code{datasources} argument is not specified |
| 13 | +#' the default set of connections will be used: see \code{\link[DSI]{datashield.connections_default}}. |
| 14 | +#' @return \code{ds.asDataFrame} returns the object converted into a data frame |
| 15 | +#' that is written to the server-side. |
| 16 | +#' @examples |
| 17 | +#' \dontrun{ |
| 18 | +#' ## Version 6, for version 5 see the Wiki |
| 19 | +#' |
| 20 | +#' # connecting to the Opal servers |
| 21 | +#' |
| 22 | +#' require('DSI') |
| 23 | +#' require('DSOpal') |
| 24 | +#' require('dsBaseClient') |
| 25 | +#' |
| 26 | +#' builder <- DSI::newDSLoginBuilder() |
| 27 | +#' builder$append(server = "study1", |
| 28 | +#' url = "http://192.168.56.100:8080/", |
| 29 | +#' user = "administrator", password = "datashield_test&", |
| 30 | +#' table = "CNSIM.CNSIM1", driver = "OpalDriver") |
| 31 | +#' builder$append(server = "study2", |
| 32 | +#' url = "http://192.168.56.100:8080/", |
| 33 | +#' user = "administrator", password = "datashield_test&", |
| 34 | +#' table = "CNSIM.CNSIM2", driver = "OpalDriver") |
| 35 | +#' builder$append(server = "study3", |
| 36 | +#' url = "http://192.168.56.100:8080/", |
| 37 | +#' user = "administrator", password = "datashield_test&", |
| 38 | +#' table = "CNSIM.CNSIM3", driver = "OpalDriver") |
| 39 | +#' logindata <- builder$build() |
| 40 | +#' |
| 41 | +#' connections <- DSI::datashield.login(logins = logindata, assign = TRUE, symbol = "D") |
| 42 | +#' |
| 43 | +#' # Converting the R object into a data frame |
| 44 | +#' ds.asDataFrame(x.name = "D", |
| 45 | +#' newobj = "mat.obj", |
| 46 | +#' datasources = connections[1]) #only the first server is used ("study1") |
| 47 | +#' |
| 48 | +#' # Clear the Datashield R sessions and logout |
| 49 | +#' datashield.logout(connections) |
| 50 | +#' |
| 51 | +#' } |
| 52 | +#' @author Tim Cadman |
| 53 | +#' @export |
| 54 | +ds.asDataFrame <- function(x.name=NULL, newobj=NULL, datasources=NULL){ |
| 55 | + |
| 56 | + # look for DS connections |
| 57 | + if(is.null(datasources)){ |
| 58 | + datasources <- datashield.connections_find() |
| 59 | + } |
| 60 | + |
| 61 | + # ensure datasources is a list of DSConnection-class |
| 62 | + if(!(is.list(datasources) && all(unlist(lapply(datasources, function(d) {methods::is(d,"DSConnection")}))))){ |
| 63 | + stop("The 'datasources' were expected to be a list of DSConnection-class objects", call.=FALSE) |
| 64 | + } |
| 65 | + |
| 66 | + if(is.null(x.name)){ |
| 67 | + stop("Please provide the name of the input vector!", call.=FALSE) |
| 68 | + } |
| 69 | + |
| 70 | + # check if the input object is defined in all the studies |
| 71 | + isDefined(datasources, x.name) |
| 72 | + |
| 73 | + # create a name by default if user did not provide a name for the new variable |
| 74 | + if(is.null(newobj)){ |
| 75 | + newobj <- "asdataframe.newobj" |
| 76 | + } |
| 77 | + |
| 78 | + # call the server side function that does the job |
| 79 | + calltext <- call("asDataFrameDS", x.name) |
| 80 | + DSI::datashield.assign(datasources, newobj, calltext) |
| 81 | + |
| 82 | +} |
0 commit comments