Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions R/pkg/R/DataFrame.R
Original file line number Diff line number Diff line change
Expand Up @@ -1075,12 +1075,20 @@ setMethod("subset", signature(x = "DataFrame"),
#' select(df, c("col1", "col2"))
#' select(df, list(df$name, df$age + 1))
#' # Similar to R data frames columns can also be selected using `$`
#' df$age
#' df[,df$age]
#' }
setMethod("select", signature(x = "DataFrame", col = "character"),
function(x, col, ...) {
sdf <- callJMethod(x@sdf, "select", col, list(...))
dataFrame(sdf)
if (length(col) > 1) {
if (length(list(...)) > 0) {
stop("To select multiple columns, use a character vector or list for col")
}

select(x, as.list(col))
} else {
sdf <- callJMethod(x@sdf, "select", col, list(...))
dataFrame(sdf)
}
})

#' @rdname select
Expand Down Expand Up @@ -1853,13 +1861,13 @@ setMethod("crosstab",
#' This function downloads the contents of a DataFrame into an R's data.frame.
#' Since data.frames are held in memory, ensure that you have enough memory
#' in your system to accommodate the contents.
#'
#'
#' @title Download data from a DataFrame into a data.frame
#' @param x a DataFrame
#' @return a data.frame
#' @rdname as.data.frame
#' @examples \dontrun{
#'
#'
#' irisDF <- createDataFrame(sqlContext, iris)
#' df <- as.data.frame(irisDF[irisDF$Species == "setosa", ])
#' }
Expand Down
9 changes: 8 additions & 1 deletion R/pkg/inst/tests/test_sparkSQL.R
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,13 @@ test_that("select with column", {
expect_equal(columns(df3), c("x"))
expect_equal(count(df3), 3)
expect_equal(collect(select(df3, "x"))[[1, 1]], "x")

df4 <- select(df, c("name", "age"))
expect_equal(columns(df4), c("name", "age"))
expect_equal(count(df4), 3)

expect_error(select(df, c("name", "age"), "name"),
"To select multiple columns, use a character vector or list for col")
})

test_that("subsetting", {
Expand Down Expand Up @@ -1336,4 +1343,4 @@ test_that("Method as.data.frame as a synonym for collect()", {

unlink(parquetPath)
unlink(jsonPath)
unlink(jsonPathNa)
unlink(jsonPathNa)