aboutsummaryrefslogtreecommitdiff
path: root/R
diff options
context:
space:
mode:
authorPrashant Sharma <prashsh1@in.ibm.com>2016-06-28 17:11:06 +0530
committerPrashant Sharma <prashsh1@in.ibm.com>2016-06-28 17:11:06 +0530
commitf6b497fcdddc705a9e1022e20b0dbc15da1b5a5a (patch)
treeb28b241100d7ec0cbd79b0e7b79ac3f0a98dae04 /R
parent4cbf611c1dc88111ff49d005e902ad5864799ede (diff)
downloadspark-f6b497fcdddc705a9e1022e20b0dbc15da1b5a5a.tar.gz
spark-f6b497fcdddc705a9e1022e20b0dbc15da1b5a5a.tar.bz2
spark-f6b497fcdddc705a9e1022e20b0dbc15da1b5a5a.zip
[SPARK-16128][SQL] Allow setting length of characters to be truncated to, in Dataset.show function.
## What changes were proposed in this pull request? Allowing truncate to a specific number of character is convenient at times, especially while operating from the REPL. Sometimes those last few characters make all the difference, and showing everything brings in whole lot of noise. ## How was this patch tested? Existing tests. + 1 new test in DataFrameSuite. For SparkR and pyspark, existing tests and manual testing. Author: Prashant Sharma <prashsh1@in.ibm.com> Author: Prashant Sharma <prashant@apache.org> Closes #13839 from ScrapCodes/add_truncateTo_DF.show.
Diffstat (limited to 'R')
-rw-r--r--R/pkg/R/DataFrame.R11
-rw-r--r--R/pkg/inst/tests/testthat/test_sparkSQL.R8
2 files changed, 16 insertions, 3 deletions
diff --git a/R/pkg/R/DataFrame.R b/R/pkg/R/DataFrame.R
index f856979c2a..61d47a8c2d 100644
--- a/R/pkg/R/DataFrame.R
+++ b/R/pkg/R/DataFrame.R
@@ -176,8 +176,8 @@ setMethod("isLocal",
#' @param x A SparkDataFrame
#' @param numRows The number of rows to print. Defaults to 20.
#' @param truncate Whether truncate long strings. If true, strings more than 20 characters will be
-#' truncated and all cells will be aligned right
-#'
+#' truncated. However, if set greater than zero, truncates strings longer than `truncate`
+#' characters and all cells will be aligned right.
#' @family SparkDataFrame functions
#' @rdname showDF
#' @name showDF
@@ -193,7 +193,12 @@ setMethod("isLocal",
setMethod("showDF",
signature(x = "SparkDataFrame"),
function(x, numRows = 20, truncate = TRUE) {
- s <- callJMethod(x@sdf, "showString", numToInt(numRows), truncate)
+ if (is.logical(truncate) && truncate) {
+ s <- callJMethod(x@sdf, "showString", numToInt(numRows), numToInt(20))
+ } else {
+ truncate2 <- as.numeric(truncate)
+ s <- callJMethod(x@sdf, "showString", numToInt(numRows), numToInt(truncate2))
+ }
cat(s)
})
diff --git a/R/pkg/inst/tests/testthat/test_sparkSQL.R b/R/pkg/inst/tests/testthat/test_sparkSQL.R
index 74def5ce42..7562fa95e3 100644
--- a/R/pkg/inst/tests/testthat/test_sparkSQL.R
+++ b/R/pkg/inst/tests/testthat/test_sparkSQL.R
@@ -1582,7 +1582,15 @@ test_that("showDF()", {
"| 30| Andy|\n",
"| 19| Justin|\n",
"+----+-------+\n", sep = "")
+ expected2 <- paste("+---+----+\n",
+ "|age|name|\n",
+ "+---+----+\n",
+ "|nul| Mic|\n",
+ "| 30| And|\n",
+ "| 19| Jus|\n",
+ "+---+----+\n", sep = "")
expect_output(showDF(df), expected)
+ expect_output(showDF(df, truncate = 3), expected2)
})
test_that("isLocal()", {