R/dataFrameFillDS.R

Defines functions dataFrameFillDS

Documented in dataFrameFillDS

#' 
#' @title dataFrameFillDS
#' @description An assign function called by the clientside ds.dataFrameFill function.
#' @details This function checks if each study has all the variables compared to the other studies
#' in the analysis. If 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. 
#' Then, the "complete" in terms of the columns dataframe is saved in each server with a name
#' specified by the argument \code{newobj} on the clientside. 
#' @param df.name a character string representing the name of the input data frame that will be
#' filled with extra columns with missing values if a number of variables is missing from it
#' compared to the data frames of the other studies used in the analysis.
#' @param allNames.transmit unique names of all the variables that are included in the input 
#' data frames from all the used datasources. 
#' @return Nothing is returned to the client. The generated object is written to the serverside.
#' @author Demetris Avraam for DataSHIELD Development Team
#' @export
#' 
dataFrameFillDS <- function(df.name, allNames.transmit){
  
  datatext <- paste0("data.frame(",df.name,")")
  data <- eval(parse(text=datatext))

  if(!is.null(allNames.transmit)){
    allNames <- unlist(strsplit(allNames.transmit, split=","))
  }else{
    allNames <- NULL
  }
  
  study.colnames <- colnames(data)
  missingVars <- allNames[-which(allNames %in% study.colnames)]
  
  numRows <- dim(data)[1]
  numCols <- length(missingVars)
  
  mat.new <- matrix(NA, ncol=numCols, nrow=numRows)
  
  df.new <- as.data.frame(mat.new)
  colnames(df.new) <- missingVars
  df.new <- lapply(df.new, as.numeric)

  df.out <- cbind(data, df.new)
  
  return(df.out)

}
# ASSIGN FUNCTION
# dataFrameFillDS
datashield/dsBetaTest documentation built on Nov. 7, 2019, 5:40 p.m.