Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
^docker-compose_armadillo\.yml$
^docker-compose_opal\.yml$
^docker-compose\.yml$
^R/secure.global.ranking.md$
^_pkgdown\.yml$
^docs$
^dsBase_7.0.0\.tar\.gz$
Expand Down
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ Imports:
ggplot2,
gridExtra,
data.table,
methods,
dplyr,
cli
lifecycle
Suggests:
lme4,
httr,
Expand Down
22 changes: 0 additions & 22 deletions R/colPercent.R

This file was deleted.

180 changes: 9 additions & 171 deletions R/ds.dataFrameFill.R
Original file line number Diff line number Diff line change
@@ -1,176 +1,14 @@
#'
#' @title Creates missing values columns in the server-side
#' @description Adds extra columns with missing values in a data frame on the server-side.
#' @details This function checks if the input data frames have the same variables (i.e. the same
#' column names) in all of the used studies. When a study does not have some of the variables, the
#' function generates those variables as vectors of missing values and combines them as columns to
#' the input data frame. If any of the generated variables are of class factor, the function
#' assigns to those the corresponding levels of the factors given from the studies where such
#' factors exist.
#'
#' Server function called: \code{dataFrameFillDS}
#' @param df.name a character string representing the name of the input data frame that will be
#' filled with extra columns of missing values.
#' @param newobj a character string that provides the name for the output data frame
#' that is stored on the data servers. Default value is "dataframefill.newobj".
#' @param classConsistencyCheck logical. If TRUE, verifies that each input object has the same class across all studies. Default TRUE.
#' @param datasources a list of \code{\link[DSI]{DSConnection-class}} objects obtained after login.
#' If the \code{datasources} argument is not specified
#' the default set of connections will be used: see \code{\link[DSI]{datashield.connections_default}}.
#' @return \code{ds.dataFrameFill} returns the object specified by the \code{newobj} argument which
#' is written to the server-side.
#' @description
#' `r lifecycle::badge("deprecated")`
#'
#' This function is defunct. Please use `ds.standardiseDf()` instead.
#' @param ... Not used.
#' @author Demetris Avraam for DataSHIELD Development Team
#' @author Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands
#'
#' @examples
#' \dontrun{
#'
#' ## Version 6, for version 5 see the Wiki
#' # Connecting to the Armadillo/Opal servers
#'
#' require('DSI')
#' require('DSMolgenisArmadillo')
#' require('DSOpal')
#' require('dsBaseClient')
#'
#' armadillo_url <- "http://192.168.56.100:8080/"
#' token <- armadillo.get_credentials(armadillo_url)
#' builder <- DSI::newDSLoginBuilder()
#' builder$append(server = "study1",
#' url = armadillo_url,
#' token = token@access_token,
#' table = "datashield/cnsim/CNSIM1", driver = "ArmadilloDriver")
#' builder$append(server = "study2",
#' url = "https://192.168.56.100:8443/",
#' user = "administrator", password = "datashield_test&",
#' table = "CNSIM.CNSIM2", driver = "OpalDriver",
#' options = "list(ssl_verifyhost=0, ssl_verifypeer=0)")
#'
#' logindata <- builder$build()
#'
#' # Log onto the remote Armadillo/Opal training servers
#' connections <- DSI::datashield.login(logins = logindata, assign = TRUE, symbol = "D")
#'
#' # Create two data frames with one different column
#'
#' ds.dataFrame(x = c("D$LAB_TSC","D$LAB_TRIG","D$LAB_HDL",
#' "D$LAB_GLUC_ADJUSTED","D$PM_BMI_CONTINUOUS"),
#' newobj = "df1",
#' datasources = connections[1])
#'
#' ds.dataFrame(x = c("D$LAB_TSC","D$LAB_TRIG","D$LAB_HDL","D$LAB_GLUC_ADJUSTED"),
#' newobj = "df1",
#' datasources = connections[2])
#'
#' # Fill the data frame with NA columns
#'
#' ds.dataFrameFill(df.name = "df1",
#' newobj = "D.Fill",
#' datasources = connections[c(1,2)]) # Two servers are used
#'
#'
#' # Clear the Datashield R sessions and logout
#' datashield.logout(connections)
#' }
#' @md
#' @keywords internal
#' @export
#'
ds.dataFrameFill <- function(df.name=NULL, newobj=NULL, datasources=NULL, classConsistencyCheck=TRUE){

datasources <- .set_datasources(datasources)

# check if user has provided the name of the data.frame to be subsetted
if(is.null(df.name)){
stop("Please provide the name of the data.frame to be filled as a character string: eg 'xxx'", call.=FALSE)
}

# if no value spcified for output object, then specify a default
if(is.null(newobj)){
newobj <- "dataframefill.newobj"
}

if(classConsistencyCheck){
# call the internal function that checks the input object is of the same class in all studies.
typ <- checkClass(datasources, df.name)

# if the input object is not a matrix or a dataframe stop
if(!('data.frame' %in% typ) && !('matrix' %in% typ)){
stop("The input vector must be of type 'data.frame' or a 'matrix'!", call.=FALSE)
}
}

column.names <- lapply(datasources, function(dts){DSI::datashield.aggregate(dts, call("colnamesDS", df.name))})

allNames <- unique(unlist(column.names))

# if the datasets share the same variables then the function stops
check.indicator <- c()
for (i in 1:length(datasources)){
if(length(setdiff(allNames,column.names[[i]])) > 0){
check.indicator[i] <- 1
}else{
check.indicator[i] <- 0}
}

if(sum(check.indicator)==0){
stop("The dataframes have the same variables. There are no missing variables to fill!", call.=FALSE)
}

if(!is.null(allNames)){
allNames.transmit <- paste(allNames,collapse=",")
}else{
allNames.transmit <- NULL
}

defined.list <- lapply(allNames, function(x){isDefined(datasources=datasources, obj=paste0(df.name, '$', x), error.message=FALSE)})
defined.vect1 <- lapply(defined.list, function(x){unlist(x)})
defined.vect2 <- lapply(defined.vect1, function(x){which(x == FALSE)})

# get the class of each variable in the dataframes, skipping servers where the column doesn't exist
class.list <- lapply(seq_along(allNames), function(idx){
sapply(seq_along(datasources), function(ds_idx){
if(ds_idx %in% defined.vect2[[idx]]){
"NULL"
} else {
DSI::datashield.aggregate(datasources[ds_idx], call('classDS', paste0(df.name, '$', allNames[idx])))[[1]]
}
})
})
class.vect1 <- class.list
# the loop below is to avoid autocompletion of variable name
for (i in 1:length(allNames.transmit)){
if(length(defined.vect2[[i]])>0){class.vect1[[i]][defined.vect2[[i]]]<-'NULL'}
}
class.vect2 <- lapply(class.vect1, function(x){x[which(x != 'NULL')[[1]]]})
class.vect2 <- unname(unlist(class.vect2))

# check if any of the elements in class.vect2 are factor
# and if yes then get their levels
df.indicator <- list()
levels.vec <- list()
if(length(class.vect2)>0){
anyFactors <- allNames[which(class.vect2 == 'factor')]
if(length(anyFactors)>0){
for(i in 1:length(anyFactors)){
df.indicator[[i]] <- lapply(column.names, function(x){is.element(anyFactors[i],unlist(x))})
}
df.indicator.index <- lapply(df.indicator, function(x){which(x==TRUE)})
for(i in 1:length(anyFactors)){
levels.vec[[i]] <- dsBaseClient::ds.levels(x=paste0(df.name, '$', anyFactors[i]), datasources=datasources[df.indicator.index[[i]]])
levels.vec[[i]] <- as.numeric(levels.vec[[i]][[1]][[1]])
}
}
}
levels.vec.transmit <- unlist(lapply(levels.vec, function(x){paste(x,collapse=",")}))

if(!is.null(class.vect2)){
class.vect.transmit <- paste(class.vect2,collapse=",")
}else{
class.vect.transmit <- NULL
}

calltext <- call("dataFrameFillDS", df.name, allNames.transmit, class.vect.transmit, levels.vec.transmit)
DSI::datashield.assign(datasources, newobj, calltext)


ds.dataFrameFill <- function(...) {
lifecycle::deprecate_stop("7.0.0", "ds.dataFrameFill()", "ds.standardiseDf()")
}
# ds.dataFrameFill
Loading
Loading