#library(AlteryxRDataX) #---------------------------------------------------------------------------------- #write.Alteryx # --- the starting point for the data frame coercion function (data, nOutput = 1, bIncludeRowNames = FALSE, source = "") { #----------------- # Check Inputs #----------------- if (AlteryxFullUpdate) { stop.Alteryx("'write.Alteryx' cannot be used when 'AlteryxFullUpdate' is TRUE") } if (missing(data)) { stop.Alteryx("'data' parameter not supplied") } if (is.null(data)) { stop.Alteryx("'data' parameter cannot be NULL") } cat("__AlteryxDataRows", nOutput, length(data), "\n") AlteryxOutputPipeName <- switch(nOutput, AlteryxOutput1, AlteryxOutput2, AlteryxOutput3, AlteryxOutput4, AlteryxOutput5) #------------------ # Write Out Results #------------------ if (inherits(data, "data.frame")) # if it is a data frame we call write.yxdbS directly return(write.yxdbS(data, rep(1, length(data)), AlteryxOutputPipeName, bIncludeRowNames, source)) encResult <- encodeForWrite(data) # otherwise we encode the data so it can be written as a data frame write.yxdbS(data.frame(encResult[[1]]), encResult[[2]], AlteryxOutputPipeName, bIncludeRowNames, source) } #---------------------------------------------------------------------------------- #encodeForWrite # a wrapper for serializeObject that determines/maintains appropriate meta-info function (the.table) { #--------------- # Supporting Functions #--------------- determineColEncoding <- function(the.column) { if (is.atomic(the.column[[1]]) && (length(the.column[[1]]) <= 1)) return(1) if (inherits(the.column[[1]], "Spatial")) return(3) if (class(the.column) == "list" && length(the.column) == 1) { tryCatch({ if (the.column[[1]][[1]] == "POINT EMPTY") return(3) }, error = function(err) { }) } return(2) } # The main processing step calls serializeObject on the data encodeColumn <- function(the.column, encodeType) { return(switch(encodeType, the.column, sapply(the.column, serializeObject), sapply(the.column, serializeObject))) } #---------- # Begin Code #----------- numCols <- length(the.table) retVal <- list(vector(mode = "list", length = numCols), vector(mode = "integer", length = numCols)) retVal[[2]] <- sapply(the.table, determineColEncoding) for (colCount in (1:numCols)) { retVal[[1]][[colCount]] <- encodeColumn(the.table[[colCount]], retVal[[2]][[colCount]]) } names(retVal[[1]]) <- names(the.table) return(retVal) } #---------------------------------------------------------------------------------- #serializeObject # converts the data to an exportable type function (the.object) { if (inherits(the.object, "Spatial")) { return(writeWKT(the.object)) } else if (class(the.object) == "data.frame" && length(the.object) == 2 && the.object[[1]] == "POINT EMPTY" && the.object[[2]] == "SpatialIsNull") { return(as.character(the.object[[1]])) } else { return(rawToChar(serialize(the.object, connection = NULL, ascii = TRUE))) } } #---------------------------------------------------------------------------------- #write.yxdbS # call the compiled C function WriteYXDBStreaming which performs the heavy liftying function (data, encodingFlags, name, bAddRowNames = FALSE, source) { # - optional user input to include data frame row names!! if (bAddRowNames) { RowNames <- as.character(row.names(data)) data <- cbind(RowNames, data) encodingFlags <- cbind(1, encodingFlags) } # - necessary meta-information GetType <- function(obj) { if (inherits(obj, "character")) return("character") else return(class(obj)[[1]]) } types <- sapply(data, GetType) if (!.Call("WriteYXDBStreaming", data, types, encodingFlags, name, source, PACKAGE = "AlteryxRDataX")) stop.Alteryx("There was an error in WriteYXDBStreaming") }