@@ -1727,14 +1727,21 @@ setMethod("$", signature(x = "SparkDataFrame"),
1727
1727
getColumn(x , name )
1728
1728
})
1729
1729
1730
- # ' @param value a Column or \code{NULL}. If \code{NULL}, the specified Column is dropped.
1730
+ # ' @param value a Column or an atomic vector in the length of 1 as literal value, or \code{NULL}.
1731
+ # ' If \code{NULL}, the specified Column is dropped.
1731
1732
# ' @rdname select
1732
1733
# ' @name $<-
1733
1734
# ' @aliases $<-,SparkDataFrame-method
1734
1735
# ' @note $<- since 1.4.0
1735
1736
setMethod(" $<-" , signature(x = " SparkDataFrame" ),
1736
1737
function (x , name , value ) {
1737
- stopifnot(class(value ) == " Column" || is.null(value ))
1738
+ if (class(value ) != " Column" && ! is.null(value )) {
1739
+ if (isAtomicLengthOne(value )) {
1740
+ value <- lit(value )
1741
+ } else {
1742
+ stop(" value must be a Column, literal value as atomic in length of 1, or NULL" )
1743
+ }
1744
+ }
1738
1745
1739
1746
if (is.null(value )) {
1740
1747
nx <- drop(x , name )
@@ -1947,10 +1954,10 @@ setMethod("selectExpr",
1947
1954
# '
1948
1955
# ' @param x a SparkDataFrame.
1949
1956
# ' @param colName a column name.
1950
- # ' @param col a Column expression.
1957
+ # ' @param col a Column expression, or an atomic vector in the length of 1 as literal value .
1951
1958
# ' @return A SparkDataFrame with the new column added or the existing column replaced.
1952
1959
# ' @family SparkDataFrame functions
1953
- # ' @aliases withColumn,SparkDataFrame,character,Column -method
1960
+ # ' @aliases withColumn,SparkDataFrame,character-method
1954
1961
# ' @rdname withColumn
1955
1962
# ' @name withColumn
1956
1963
# ' @seealso \link{rename} \link{mutate}
@@ -1963,11 +1970,16 @@ setMethod("selectExpr",
1963
1970
# ' newDF <- withColumn(df, "newCol", df$col1 * 5)
1964
1971
# ' # Replace an existing column
1965
1972
# ' newDF2 <- withColumn(newDF, "newCol", newDF$col1)
1973
+ # ' newDF3 <- withColumn(newDF, "newCol", 42)
1966
1974
# ' }
1967
1975
# ' @note withColumn since 1.4.0
1968
1976
setMethod ("withColumn ",
1969
- signature(x = " SparkDataFrame" , colName = " character" , col = " Column " ),
1977
+ signature(x = " SparkDataFrame" , colName = " character" ),
1970
1978
function (x , colName , col ) {
1979
+ if (class(col ) != " Column" ) {
1980
+ if (! isAtomicLengthOne(col )) stop(" Literal value must be atomic in length of 1" )
1981
+ col <- lit(col )
1982
+ }
1971
1983
sdf <- callJMethod(x @ sdf , " withColumn" , colName , col @ jc )
1972
1984
dataFrame(sdf )
1973
1985
})
0 commit comments