aboutsummaryrefslogtreecommitdiff
path: root/R/pkg/R/DataFrame.R
diff options
context:
space:
mode:
authorOscar D. Lara Yejas <olarayej@mail.usf.edu>2015-11-10 11:07:57 -0800
committerShivaram Venkataraman <shivaram@cs.berkeley.edu>2015-11-10 11:07:57 -0800
commit47735cdc2a878cfdbe76316d3ff8314a45dabf54 (patch)
tree114677527e3b83a605646107897c6d90f5182fca /R/pkg/R/DataFrame.R
parente0701c75601c43f69ed27fc7c252321703db51f2 (diff)
downloadspark-47735cdc2a878cfdbe76316d3ff8314a45dabf54.tar.gz
spark-47735cdc2a878cfdbe76316d3ff8314a45dabf54.tar.bz2
spark-47735cdc2a878cfdbe76316d3ff8314a45dabf54.zip
[SPARK-10863][SPARKR] Method coltypes() (New version)
This is a follow up on PR #8984, as the corresponding branch for such PR was damaged. Author: Oscar D. Lara Yejas <olarayej@mail.usf.edu> Closes #9579 from olarayej/SPARK-10863_NEW14.
Diffstat (limited to 'R/pkg/R/DataFrame.R')
-rw-r--r--R/pkg/R/DataFrame.R49
1 files changed, 49 insertions, 0 deletions
diff --git a/R/pkg/R/DataFrame.R b/R/pkg/R/DataFrame.R
index e9013aa34a..cc868069d1 100644
--- a/R/pkg/R/DataFrame.R
+++ b/R/pkg/R/DataFrame.R
@@ -2152,3 +2152,52 @@ setMethod("with",
newEnv <- assignNewEnv(data)
eval(substitute(expr), envir = newEnv, enclos = newEnv)
})
+
+#' Returns the column types of a DataFrame.
+#'
+#' @name coltypes
+#' @title Get column types of a DataFrame
+#' @family dataframe_funcs
+#' @param x (DataFrame)
+#' @return value (character) A character vector with the column types of the given DataFrame
+#' @rdname coltypes
+#' @examples \dontrun{
+#' irisDF <- createDataFrame(sqlContext, iris)
+#' coltypes(irisDF)
+#' }
+setMethod("coltypes",
+ signature(x = "DataFrame"),
+ function(x) {
+ # Get the data types of the DataFrame by invoking dtypes() function
+ types <- sapply(dtypes(x), function(x) {x[[2]]})
+
+ # Map Spark data types into R's data types using DATA_TYPES environment
+ rTypes <- sapply(types, USE.NAMES=F, FUN=function(x) {
+
+ # Check for primitive types
+ type <- PRIMITIVE_TYPES[[x]]
+
+ if (is.null(type)) {
+ # Check for complex types
+ for (t in names(COMPLEX_TYPES)) {
+ if (substring(x, 1, nchar(t)) == t) {
+ type <- COMPLEX_TYPES[[t]]
+ break
+ }
+ }
+
+ if (is.null(type)) {
+ stop(paste("Unsupported data type: ", x))
+ }
+ }
+ type
+ })
+
+ # Find which types don't have mapping to R
+ naIndices <- which(is.na(rTypes))
+
+ # Assign the original scala data types to the unmatched ones
+ rTypes[naIndices] <- types[naIndices]
+
+ rTypes
+ }) \ No newline at end of file