Skip to content

Commit

Permalink
Add format_list_item() method for simple feature geometry columns
Browse files Browse the repository at this point in the history
By adding `format_list_item.sfg()`, a method for the recently added
`format_list_item()` S3 generic, we can ensure that any simple feature
geometry columns (with elements of class `"sfg"`) in a data.table will
be pretty printed using the **sf** package's `format.sfg()`. To see
what that looks like, see the example below

```r
library(sf)
library(data.table)
nc <- st_read(system.file("shape/nc.shp", package = "sf"))
DT <- data.table(nc)
DT[1:3, .(NAME, FIPS, geometry)]
 ##         NAME  FIPS                       geometry
 ## 1:      Ashe 37009 MULTIPOLYGON (((-81.47276 3...
 ## 2: Alleghany 37005 MULTIPOLYGON (((-81.23989 3...
 ## 3:     Surry 37171 MULTIPOLYGON (((-80.45634 3...
```

FR Rdatatable#2273, starting
[here](Rdatatable#2273 (comment)),
includes a discussion of the pros and cons of adding this method, for
a data type defined in the **sf** package, to **data.table**.
  • Loading branch information
JoshOBrien authored and MichaelChirico committed Oct 21, 2021
1 parent e88826e commit 7914fae
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ S3method(format_col, POSIXct)
S3method(format_col, expression)
export(format_list_item)
S3method(format_list_item, default)
S3method(format_list_item, sfg)

export(fdroplevels)
S3method(droplevels, data.table)
5 changes: 5 additions & 0 deletions R/print.data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ format_list_item.default = function(x, ...) {
paste0("<", class(x)[1L], paste_dims(x), ">")
}

# FR #2273 -- Use sf package's formatting method for simple feature geometry columns
format_list_item.sfg = function(x, ...) {
format(x)
}

# FR #1091 for pretty printing of character
# TODO: maybe instead of doing "this is...", we could do "this ... test"?
char.trunc <- function(x, trunc.char = getOption("datatable.prettyprint.char")) {
Expand Down
3 changes: 2 additions & 1 deletion man/print.data.table.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

format_list_item(x, \dots)
\method{format_list_item}{default}(x, \dots)
\method{format_list_item}{sfg}(x, \dots)
}
\arguments{
\item{x}{ A \code{data.table}. }
Expand All @@ -57,7 +58,7 @@
\details{
By default, with an eye to the typically large number of observations in a \code{data.table}, only the beginning and end of the object are displayed (specifically, \code{head(x, topn)} and \code{tail(x, topn)} are displayed unless \code{nrow(x) < nrows}, in which case all rows will print).

\code{format_col} is applied at a column level; for example, \code{format_col.POSIXct} is used to tag the time zones of \code{POSIXct} columns. \code{format_list_item} is applied to the elements (rows) of \code{list} columns; see Examples.
\code{format_col} is applied at a column level; for example, \code{format_col.POSIXct} is used to tag the time zones of \code{POSIXct} columns. \code{format_list_item} is applied to the elements (rows) of \code{list} columns; see Examples. \code{format_list_item.sfg} uses the \pkg{sf} package's formatting method to print simple feature geometry columns (of class \code{"sfg"}).
}
\seealso{\code{\link{print.default}}}
\examples{
Expand Down

0 comments on commit 7914fae

Please sign in to comment.