Skip to content

Commit 47fca50

Browse files
committed
feat: first commit asDataFrame
1 parent cfeaf56 commit 47fca50

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

R/ds.asDataFrame.R

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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{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{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 DataSHIELD Development Team
53+
#' @export
54+
#'
55+
ds.asDataFrame <- function(x.name=NULL, newobj=NULL, datasources=NULL){
56+
57+
# look for DS connections
58+
if(is.null(datasources)){
59+
datasources <- datashield.connections_find()
60+
}
61+
62+
# ensure datasources is a list of DSConnection-class
63+
if(!(is.list(datasources) && all(unlist(lapply(datasources, function(d) {methods::is(d,"DSConnection")}))))){
64+
stop("The 'datasources' were expected to be a list of DSConnection-class objects", call.=FALSE)
65+
}
66+
67+
if(is.null(x.name)){
68+
stop("Please provide the name of the input vector!", call.=FALSE)
69+
}
70+
71+
# check if the input object is defined in all the studies
72+
isDefined(datasources, x.name)
73+
74+
# create a name by default if user did not provide a name for the new variable
75+
if(is.null(newobj)){
76+
newobj <- "asdataframe.newobj"
77+
}
78+
79+
# call the server side function that does the job
80+
calltext <- call("asDataFrameDS", x.name)
81+
DSI::datashield.assign(datasources, newobj, calltext)
82+
83+
}

0 commit comments

Comments
 (0)