Skip to content

Commit fc9dc2c

Browse files
Merge pull request datashield#570 from datashield/v6.4.0-dev-feat/as-data-frame
V6.4.0 dev feat/as data frame
2 parents 234e723 + d2e7cd0 commit fc9dc2c

3 files changed

Lines changed: 167 additions & 0 deletions

File tree

R/ds.asDataFrame.R

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 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+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#-------------------------------------------------------------------------------
2+
# Copyright (c) 2018-2022 University of Newcastle upon Tyne. All rights reserved.
3+
#
4+
# This program and the accompanying materials
5+
# are made available under the terms of the GNU Public License v3.0.
6+
#
7+
# You should have received a copy of the GNU General Public License
8+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
9+
#-------------------------------------------------------------------------------
10+
11+
#
12+
# Set up
13+
#
14+
15+
connect.studies.dataset.cnsim(list("LAB_TSC"))
16+
17+
#
18+
# Tests
19+
#
20+
21+
context("ds.asDataFrame::arg::test errors")
22+
test_that("asDataFrame_errors", {
23+
expect_error(ds.asDataMatrix(), "Please provide the name of the input vector!", fixed=TRUE)
24+
})
25+
26+
#
27+
# Done
28+
#
29+
30+
disconnect.studies.dataset.cnsim()
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#-------------------------------------------------------------------------------
2+
# Copyright (c) 2018-2022 University of Newcastle upon Tyne. All rights reserved.
3+
#
4+
# This program and the accompanying materials
5+
# are made available under the terms of the GNU Public License v3.0.
6+
#
7+
# You should have received a copy of the GNU General Public License
8+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
9+
#-------------------------------------------------------------------------------
10+
11+
#
12+
# Set up
13+
#
14+
15+
context("ds.asDataFrame::smk::setup")
16+
17+
connect.studies.dataset.cnsim(list("GENDER"))
18+
19+
test_that("setup", {
20+
ds_expect_variables(c("D"))
21+
})
22+
23+
#
24+
# Tests
25+
#
26+
27+
context("ds.asDataFrame::smk::simple test")
28+
test_that("simple test", {
29+
ds.asDataMatrix("D$GENDER")
30+
ds.asDataFrame(x.name="asdatamatrix.newobj")
31+
res.class <- ds.class("asdataframe.newobj")
32+
print(res.class)
33+
expect_length(res.class, 3)
34+
expect_length(res.class$sim1, 1)
35+
expect_true("data.frame" %in% res.class$sim1)
36+
expect_length(res.class$sim2, 1)
37+
expect_true("data.frame" %in% res.class$sim2)
38+
expect_length(res.class$sim3, 1)
39+
expect_true("data.frame" %in% res.class$sim3)
40+
})
41+
42+
#
43+
# Done
44+
#
45+
46+
context("ds.asDataFrame::smk::shutdown")
47+
48+
test_that("shutdown", {
49+
ds_expect_variables(c("D", "asdatamatrix.newobj", "asdataframe.newobj"))
50+
})
51+
52+
disconnect.studies.dataset.cnsim()
53+
54+
context("ds.asDataFrame::smk::done")
55+

0 commit comments

Comments
 (0)