@@ -2299,3 +2299,76 @@ setMethod("with",
22992299 newEnv <- assignNewEnv(data )
23002300 eval(substitute(expr ), envir = newEnv , enclos = newEnv )
23012301 })
2302+
2303+ # ' Display the structure of a DataFrame, including column names, column types, as well as a
2304+ # ' a small sample of rows.
2305+ # ' @name str
2306+ # ' @title Compactly display the structure of a dataset
2307+ # ' @rdname str
2308+ # ' @family DataFrame functions
2309+ # ' @param object a DataFrame
2310+ # ' @examples \dontrun{
2311+ # ' # Create a DataFrame from the Iris dataset
2312+ # ' irisDF <- createDataFrame(sqlContext, iris)
2313+ # '
2314+ # ' # Show the structure of the DataFrame
2315+ # ' str(irisDF)
2316+ # ' }
2317+ setMethod ("str ",
2318+ signature(object = " DataFrame" ),
2319+ function (object ) {
2320+
2321+ # TODO: These could be made global parameters, though in R it's not the case
2322+ MAX_CHAR_PER_ROW <- 120
2323+ MAX_COLS <- 100
2324+
2325+ # Get the column names and types of the DataFrame
2326+ names <- names(object )
2327+ types <- coltypes(object )
2328+
2329+ # Get the first elements of the dataset. Limit number of columns accordingly
2330+ localDF <- if (ncol(object ) > MAX_COLS ) {
2331+ head(object [, c(1 : MAX_COLS )])
2332+ } else {
2333+ head(object )
2334+ }
2335+
2336+ # The number of observations will not be displayed as computing the
2337+ # number of rows is a very expensive operation
2338+ cat(paste0(" '" , class(object ), " ': " , length(names ), " variables:\n " ))
2339+
2340+ if (nrow(localDF ) > 0 ) {
2341+ for (i in 1 : ncol(localDF )) {
2342+ # Get the first elements for each column
2343+
2344+ firstElements <- if (types [i ] == " character" ) {
2345+ paste(paste0(" \" " , localDF [,i ], " \" " ), collapse = " " )
2346+ } else {
2347+ paste(localDF [,i ], collapse = " " )
2348+ }
2349+
2350+ # Add the corresponding number of spaces for alignment
2351+ spaces <- paste(rep(" " , max(nchar(names ) - nchar(names [i ]))), collapse = " " )
2352+
2353+ # Get the short type. For 'character', it would be 'chr';
2354+ # 'for numeric', it's 'num', etc.
2355+ dataType <- SHORT_TYPES [[types [i ]]]
2356+ if (is.null(dataType )) {
2357+ dataType <- substring(types [i ], 1 , 3 )
2358+ }
2359+
2360+ # Concatenate the colnames, coltypes, and first
2361+ # elements of each column
2362+ line <- paste0(" $ " , names [i ], spaces , " : " ,
2363+ dataType , " " ,firstElements )
2364+
2365+ # Chop off extra characters if this is too long
2366+ cat(substr(line , 1 , MAX_CHAR_PER_ROW ))
2367+ cat(" \n " )
2368+ }
2369+
2370+ if (ncol(localDF ) < ncol(object )) {
2371+ cat(paste0(" \n Displaying first " , ncol(localDF ), " columns only." ))
2372+ }
2373+ }
2374+ })
0 commit comments