@@ -1192,23 +1192,10 @@ setMethod("$", signature(x = "DataFrame"),
11921192setMethod(" $<-" , signature(x = " DataFrame" ),
11931193 function (x , name , value ) {
11941194 stopifnot(class(value ) == " Column" || is.null(value ))
1195- cols <- columns(x )
1196- if (name %in% cols ) {
1197- if (is.null(value )) {
1198- cols <- Filter(function (c ) { c != name }, cols )
1199- }
1200- cols <- lapply(cols , function (c ) {
1201- if (c == name ) {
1202- alias(value , name )
1203- } else {
1204- col(c )
1205- }
1206- })
1207- nx <- select(x , cols )
1195+
1196+ if (is.null(value )) {
1197+ nx <- drop(x , name )
12081198 } else {
1209- if (is.null(value )) {
1210- return (x )
1211- }
12121199 nx <- withColumn(x , name , value )
12131200 }
12141201 x @ sdf <- nx @ sdf
@@ -1386,12 +1373,13 @@ setMethod("selectExpr",
13861373
13871374# ' WithColumn
13881375# '
1389- # ' Return a new DataFrame with the specified column added.
1376+ # ' Return a new DataFrame by adding a column or replacing the existing column
1377+ # ' that has the same name.
13901378# '
13911379# ' @param x A DataFrame
1392- # ' @param colName A string containing the name of the new column .
1380+ # ' @param colName A column name.
13931381# ' @param col A Column expression.
1394- # ' @return A DataFrame with the new column added.
1382+ # ' @return A DataFrame with the new column added or the existing column replaced .
13951383# ' @family DataFrame functions
13961384# ' @rdname withColumn
13971385# ' @name withColumn
@@ -1404,12 +1392,16 @@ setMethod("selectExpr",
14041392# ' path <- "path/to/file.json"
14051393# ' df <- read.json(sqlContext, path)
14061394# ' newDF <- withColumn(df, "newCol", df$col1 * 5)
1395+ # ' # Replace an existing column
1396+ # ' newDF2 <- withColumn(newDF, "newCol", newDF$col1)
14071397# ' }
14081398setMethod ("withColumn ",
14091399 signature(x = " DataFrame" , colName = " character" , col = " Column" ),
14101400 function (x , colName , col ) {
1411- select(x , x $ " *" , alias(col , colName ))
1401+ sdf <- callJMethod(x @ sdf , " withColumn" , colName , col @ jc )
1402+ dataFrame(sdf )
14121403 })
1404+
14131405# ' Mutate
14141406# '
14151407# ' Return a new DataFrame with the specified columns added.
@@ -2005,7 +1997,13 @@ setMethod("write.df",
20051997 signature(df = " DataFrame" , path = " character" ),
20061998 function (df , path , source = NULL , mode = " error" , ... ){
20071999 if (is.null(source )) {
2008- sqlContext <- get(" .sparkRSQLsc" , envir = .sparkREnv )
2000+ if (exists(" .sparkRSQLsc" , envir = .sparkREnv )) {
2001+ sqlContext <- get(" .sparkRSQLsc" , envir = .sparkREnv )
2002+ } else if (exists(" .sparkRHivesc" , envir = .sparkREnv )) {
2003+ sqlContext <- get(" .sparkRHivesc" , envir = .sparkREnv )
2004+ } else {
2005+ stop(" sparkRHive or sparkRSQL context has to be specified" )
2006+ }
20092007 source <- callJMethod(sqlContext , " getConf" , " spark.sql.sources.default" ,
20102008 " org.apache.spark.sql.parquet" )
20112009 }
@@ -2063,13 +2061,18 @@ setMethod("saveDF",
20632061# ' saveAsTable(df, "myfile")
20642062# ' }
20652063setMethod ("saveAsTable ",
2066- signature(df = " DataFrame" , tableName = " character" , source = " character" ,
2067- mode = " character" ),
2064+ signature(df = " DataFrame" , tableName = " character" ),
20682065 function (df , tableName , source = NULL , mode = " error" , ... ){
20692066 if (is.null(source )) {
2070- sqlContext <- get(" .sparkRSQLsc" , envir = .sparkREnv )
2071- source <- callJMethod(sqlContext , " getConf" , " spark.sql.sources.default" ,
2072- " org.apache.spark.sql.parquet" )
2067+ if (exists(" .sparkRSQLsc" , envir = .sparkREnv )) {
2068+ sqlContext <- get(" .sparkRSQLsc" , envir = .sparkREnv )
2069+ } else if (exists(" .sparkRHivesc" , envir = .sparkREnv )) {
2070+ sqlContext <- get(" .sparkRHivesc" , envir = .sparkREnv )
2071+ } else {
2072+ stop(" sparkRHive or sparkRSQL context has to be specified" )
2073+ }
2074+ source <- callJMethod(sqlContext , " getConf" , " spark.sql.sources.default" ,
2075+ " org.apache.spark.sql.parquet" )
20732076 }
20742077 jmode <- convertToJSaveMode(mode )
20752078 options <- varargsToEnv(... )
@@ -2401,4 +2404,47 @@ setMethod("str",
24012404 cat(paste0(" \n Displaying first " , ncol(localDF ), " columns only." ))
24022405 }
24032406 }
2404- })
2407+ })
2408+
2409+ # ' drop
2410+ # '
2411+ # ' Returns a new DataFrame with columns dropped.
2412+ # ' This is a no-op if schema doesn't contain column name(s).
2413+ # '
2414+ # ' @param x A SparkSQL DataFrame.
2415+ # ' @param cols A character vector of column names or a Column.
2416+ # ' @return A DataFrame
2417+ # '
2418+ # ' @family DataFrame functions
2419+ # ' @rdname drop
2420+ # ' @name drop
2421+ # ' @export
2422+ # ' @examples
2423+ # '\dontrun{
2424+ # ' sc <- sparkR.init()
2425+ # ' sqlCtx <- sparkRSQL.init(sc)
2426+ # ' path <- "path/to/file.json"
2427+ # ' df <- read.json(sqlCtx, path)
2428+ # ' drop(df, "col1")
2429+ # ' drop(df, c("col1", "col2"))
2430+ # ' drop(df, df$col1)
2431+ # ' }
2432+ setMethod ("drop ",
2433+ signature(x = " DataFrame" ),
2434+ function (x , col ) {
2435+ stopifnot(class(col ) == " character" || class(col ) == " Column" )
2436+
2437+ if (class(col ) == " Column" ) {
2438+ sdf <- callJMethod(x @ sdf , " drop" , col @ jc )
2439+ } else {
2440+ sdf <- callJMethod(x @ sdf , " drop" , as.list(col ))
2441+ }
2442+ dataFrame(sdf )
2443+ })
2444+
2445+ # Expose base::drop
2446+ setMethod ("drop ",
2447+ signature(x = " ANY" ),
2448+ function (x ) {
2449+ base :: drop(x )
2450+ })
0 commit comments