Skip to content

Commit 93ef246

Browse files
yanboliangshivaram
authored andcommitted
[SPARK-12234][SPARKR] Fix subset function error when only set select argument
Fix ```subset``` function error when only set ```select``` argument. Please refer to the [JIRA](https://issues.apache.org/jira/browse/SPARK-12234) about the error and how to reproduce it. cc sun-rui felixcheung shivaram Author: Yanbo Liang <ybliang8@gmail.com> Closes apache#10217 from yanboliang/spark-12234. (cherry picked from commit d9d354e) Signed-off-by: Shivaram Venkataraman <shivaram@cs.berkeley.edu>
1 parent e65c885 commit 93ef246

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

R/pkg/R/DataFrame.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,7 @@ setMethod("[", signature(x = "DataFrame", i = "Column"),
11851185
#'
11861186
#' Return subsets of DataFrame according to given conditions
11871187
#' @param x A DataFrame
1188-
#' @param subset A logical expression to filter on rows
1188+
#' @param subset (Optional) A logical expression to filter on rows
11891189
#' @param select expression for the single Column or a list of columns to select from the DataFrame
11901190
#' @return A new DataFrame containing only the rows that meet the condition with selected columns
11911191
#' @export
@@ -1206,10 +1206,15 @@ setMethod("[", signature(x = "DataFrame", i = "Column"),
12061206
#' df[df$age %in% c(19, 30), 1:2]
12071207
#' subset(df, df$age %in% c(19, 30), 1:2)
12081208
#' subset(df, df$age %in% c(19), select = c(1,2))
1209+
#' subset(df, select = c(1,2))
12091210
#' }
12101211
setMethod("subset", signature(x = "DataFrame"),
12111212
function(x, subset, select, ...) {
1212-
x[subset, select, ...]
1213+
if (missing(subset)) {
1214+
x[, select, ...]
1215+
} else {
1216+
x[subset, select, ...]
1217+
}
12131218
})
12141219

12151220
#' Select

R/pkg/inst/tests/testthat/test_sparkSQL.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,10 @@ test_that("subsetting", {
799799
expect_equal(count(df6), 1)
800800
expect_equal(columns(df6), c("name", "age"))
801801

802+
df7 <- subset(df, select = "name")
803+
expect_equal(count(df7), 3)
804+
expect_equal(columns(df7), c("name"))
805+
802806
# Test base::subset is working
803807
expect_equal(nrow(subset(airquality, Temp > 80, select = c(Ozone, Temp))), 68)
804808
})

0 commit comments

Comments
 (0)