@@ -254,6 +254,7 @@ setMethod("dtypes",
254254# ' @family DataFrame functions
255255# ' @rdname columns
256256# ' @name columns
257+
257258# ' @export
258259# ' @examples
259260# '\dontrun{
@@ -262,6 +263,7 @@ setMethod("dtypes",
262263# ' path <- "path/to/file.json"
263264# ' df <- jsonFile(sqlContext, path)
264265# ' columns(df)
266+ # ' colnames(df)
265267# '}
266268setMethod ("columns ",
267269 signature(x = " DataFrame" ),
@@ -290,6 +292,121 @@ setMethod("names<-",
290292 }
291293 })
292294
295+ # ' @rdname columns
296+ # ' @name colnames
297+ setMethod ("colnames ",
298+ signature(x = " DataFrame" ),
299+ function (x ) {
300+ columns(x )
301+ })
302+
303+ # ' @rdname columns
304+ # ' @name colnames<-
305+ setMethod(" colnames<-" ,
306+ signature(x = " DataFrame" , value = " character" ),
307+ function (x , value ) {
308+ sdf <- callJMethod(x @ sdf , " toDF" , as.list(value ))
309+ dataFrame(sdf )
310+ })
311+
312+ # ' coltypes
313+ # '
314+ # ' Get column types of a DataFrame
315+ # '
316+ # ' @param x A SparkSQL DataFrame
317+ # ' @return value A character vector with the column types of the given DataFrame
318+ # ' @rdname coltypes
319+ # ' @name coltypes
320+ # ' @family DataFrame functions
321+ # ' @export
322+ # ' @examples
323+ # '\dontrun{
324+ # ' irisDF <- createDataFrame(sqlContext, iris)
325+ # ' coltypes(irisDF)
326+ # '}
327+ setMethod ("coltypes ",
328+ signature(x = " DataFrame" ),
329+ function (x ) {
330+ # Get the data types of the DataFrame by invoking dtypes() function
331+ types <- sapply(dtypes(x ), function (x ) {x [[2 ]]})
332+
333+ # Map Spark data types into R's data types using DATA_TYPES environment
334+ rTypes <- sapply(types , USE.NAMES = F , FUN = function (x ) {
335+ # Check for primitive types
336+ type <- PRIMITIVE_TYPES [[x ]]
337+
338+ if (is.null(type )) {
339+ # Check for complex types
340+ for (t in names(COMPLEX_TYPES )) {
341+ if (substring(x , 1 , nchar(t )) == t ) {
342+ type <- COMPLEX_TYPES [[t ]]
343+ break
344+ }
345+ }
346+
347+ if (is.null(type )) {
348+ stop(paste(" Unsupported data type: " , x ))
349+ }
350+ }
351+ type
352+ })
353+
354+ # Find which types don't have mapping to R
355+ naIndices <- which(is.na(rTypes ))
356+
357+ # Assign the original scala data types to the unmatched ones
358+ rTypes [naIndices ] <- types [naIndices ]
359+
360+ rTypes
361+ })
362+
363+ # ' coltypes
364+ # '
365+ # ' Set the column types of a DataFrame.
366+ # '
367+ # ' @param x A SparkSQL DataFrame
368+ # ' @param value A character vector with the target column types for the given
369+ # ' DataFrame. Column types can be one of integer, numeric/double, character, logical, or NA
370+ # ' to keep that column as-is.
371+ # ' @rdname coltypes
372+ # ' @name coltypes<-
373+ # ' @export
374+ # ' @examples
375+ # '\dontrun{
376+ # ' sc <- sparkR.init()
377+ # ' sqlContext <- sparkRSQL.init(sc)
378+ # ' path <- "path/to/file.json"
379+ # ' df <- jsonFile(sqlContext, path)
380+ # ' coltypes(df) <- c("character", "integer")
381+ # ' coltypes(df) <- c(NA, "numeric")
382+ # '}
383+ setMethod(" coltypes<-" ,
384+ signature(x = " DataFrame" , value = " character" ),
385+ function (x , value ) {
386+ cols <- columns(x )
387+ ncols <- length(cols )
388+ if (length(value ) == 0 ) {
389+ stop(" Cannot set types of an empty DataFrame with no Column" )
390+ }
391+ if (length(value ) != ncols ) {
392+ stop(" Length of type vector should match the number of columns for DataFrame" )
393+ }
394+ newCols <- lapply(seq_len(ncols ), function (i ) {
395+ col <- getColumn(x , cols [i ])
396+ if (! is.na(value [i ])) {
397+ stype <- rToSQLTypes [[value [i ]]]
398+ if (is.null(stype )) {
399+ stop(" Only atomic type is supported for column types" )
400+ }
401+ cast(col , stype )
402+ } else {
403+ col
404+ }
405+ })
406+ nx <- select(x , newCols )
407+ dataFrame(nx @ sdf )
408+ })
409+
293410# ' Register Temporary Table
294411# '
295412# ' Registers a DataFrame as a Temporary Table in the SQLContext
@@ -2102,52 +2219,3 @@ setMethod("with",
21022219 newEnv <- assignNewEnv(data )
21032220 eval(substitute(expr ), envir = newEnv , enclos = newEnv )
21042221 })
2105-
2106- # ' Returns the column types of a DataFrame.
2107- # '
2108- # ' @name coltypes
2109- # ' @title Get column types of a DataFrame
2110- # ' @family dataframe_funcs
2111- # ' @param x (DataFrame)
2112- # ' @return value (character) A character vector with the column types of the given DataFrame
2113- # ' @rdname coltypes
2114- # ' @examples \dontrun{
2115- # ' irisDF <- createDataFrame(sqlContext, iris)
2116- # ' coltypes(irisDF)
2117- # ' }
2118- setMethod ("coltypes ",
2119- signature(x = " DataFrame" ),
2120- function (x ) {
2121- # Get the data types of the DataFrame by invoking dtypes() function
2122- types <- sapply(dtypes(x ), function (x ) {x [[2 ]]})
2123-
2124- # Map Spark data types into R's data types using DATA_TYPES environment
2125- rTypes <- sapply(types , USE.NAMES = F , FUN = function (x ) {
2126-
2127- # Check for primitive types
2128- type <- PRIMITIVE_TYPES [[x ]]
2129-
2130- if (is.null(type )) {
2131- # Check for complex types
2132- for (t in names(COMPLEX_TYPES )) {
2133- if (substring(x , 1 , nchar(t )) == t ) {
2134- type <- COMPLEX_TYPES [[t ]]
2135- break
2136- }
2137- }
2138-
2139- if (is.null(type )) {
2140- stop(paste(" Unsupported data type: " , x ))
2141- }
2142- }
2143- type
2144- })
2145-
2146- # Find which types don't have mapping to R
2147- naIndices <- which(is.na(rTypes ))
2148-
2149- # Assign the original scala data types to the unmatched ones
2150- rTypes [naIndices ] <- types [naIndices ]
2151-
2152- rTypes
2153- })
0 commit comments