I'm trying to save the equivalent of head
of an sf
object in a list. When you use head
on a sf
class it prints slightly misleading information to the console about the perimeter of the data, see bbox
information is different from the original:
library(sf)
library(tidyverse)
nc <- st_read(system.file("shape/nc.shp", package = "sf"))
# Simple feature collection with 100 features and 14 fields
# geometry type: MULTIPOLYGON
# dimension: XY
# bbox: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
# geographic CRS: NAD27
head(nc)
# Simple feature collection with 6 features and 14 fields
# geometry type: MULTIPOLYGON
# dimension: XY
# bbox: xmin: -81.74107 ymin: 36.07282 xmax: -75.77316 ymax: 36.58965
# geographic CRS: NAD27
# AREA PERIMETER CNTY_ CNTY_ID NAME FIPS FIPSNO CRESS_ID BIR74 SID74 NWBIR74 BIR79 SID79 NWBIR79 geometry
# 1 0.114 1.442 1825 1825 Ashe 37009 37009 5 1091 1 10 1364 0 19 MULTIPOLYGON (((-81.47276 3...
# 2 0.061 1.231 1827 1827 Alleghany 37005 37005 3 487 0 10 542 3 12 MULTIPOLYGON (((-81.23989 3...
# 3 0.143 1.630 1828 1828 Surry 37171 37171 86 3188 5 208 3616 6 260 MULTIPOLYGON (((-80.45634 3...
# 4 0.070 2.968 1831 1831 Currituck 37053 37053 27 508 1 123 830 2 145 MULTIPOLYGON (((-76.00897 3...
# 5 0.153 2.206 1832 1832 Northampton 37131 37131 66 1421 9 1066 1606 3 1197 MULTIPOLYGON (((-77.21767 3...
# 6 0.097 1.670 1833 1833 Hertford 37091 37091 46 1452 7 954 1838 5 1237 MULTIPOLYGON (((-76.74506 3...
That's because I think it calculates bbox
for the first 6 observation instead of the whole dataframe. As an alternative you can run the following print
(see bbox
is the same for the full dataset):
print(nc, n = getOption("sf_max_print", default = 6))
# Simple feature collection with 100 features and 14 fields
# geometry type: MULTIPOLYGON
# dimension: XY
# bbox: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
# geographic CRS: NAD27
# First 6 features:
# AREA PERIMETER CNTY_ CNTY_ID NAME FIPS FIPSNO CRESS_ID BIR74 SID74 NWBIR74 BIR79 SID79 NWBIR79 geometry
# 1 0.114 1.442 1825 1825 Ashe 37009 37009 5 1091 1 10 1364 0 19 MULTIPOLYGON (((-81.47276 3...
# 2 0.061 1.231 1827 1827 Alleghany 37005 37005 3 487 0 10 542 3 12 MULTIPOLYGON (((-81.23989 3...
# 3 0.143 1.630 1828 1828 Surry 37171 37171 86 3188 5 208 3616 6 260 MULTIPOLYGON (((-80.45634 3...
# 4 0.070 2.968 1831 1831 Currituck 37053 37053 27 508 1 123 830 2 145 MULTIPOLYGON (((-76.00897 3...
# 5 0.153 2.206 1832 1832 Northampton 37131 37131 66 1421 9 1066 1606 3 1197 MULTIPOLYGON (((-77.21767 3...
# 6 0.097 1.670 1833 1833 Hertford 37091 37091 46 1452 7 954 1838 5 1237 MULTIPOLYGON (((-76.74506 3...
How can I save this print object in a list (which is going to be used in a function afterwards) without printing it to the console?
When I crudely put it in a list without trying to suppress the output it prints as expected:
a <- lst(dim(nc), head_sf = print(nc, n = getOption("sf_max_print", default = 6)))
# Simple feature collection with 100 features and 14 fields
# geometry type: MULTIPOLYGON
# dimension: XY
# bbox: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
# geographic CRS: NAD27
# First 6 features:
# AREA PERIMETER CNTY_ CNTY_ID NAME FIPS FIPSNO CRESS_ID BIR74 SID74 NWBIR74 BIR79 SID79 NWBIR79 geometry
# 1 0.114 1.442 1825 1825 Ashe 37009 37009 5 1091 1 10 1364 0 19 MULTIPOLYGON (((-81.47276 3...
# 2 0.061 1.231 1827 1827 Alleghany 37005 37005 3 487 0 10 542 3 12 MULTIPOLYGON (((-81.23989 3...
# 3 0.143 1.630 1828 1828 Surry 37171 37171 86 3188 5 208 3616 6 260 MULTIPOLYGON (((-80.45634 3...
# 4 0.070 2.968 1831 1831 Currituck 37053 37053 27 508 1 123 830 2 145 MULTIPOLYGON (((-76.00897 3...
# 5 0.153 2.206 1832 1832 Northampton 37131 37131 66 1421 9 1066 1606 3 1197 MULTIPOLYGON (((-77.21767 3...
# 6 0.097 1.670 1833 1833 Hertford 37091 37091 46 1452 7 954 1838 5 1237 MULTIPOLYGON (((-76.74506 3...
I thought invisible
or sink
would suppress the output but I can't figure it out, none of these work:
a <- invisible(lst(dim(nc), head_sf = print(nc, n = getOption("sf_max_print", default = 6))))
a <- lst(dim(nc), head_sf = invisible(print(nc, n = getOption("sf_max_print", default = 6))))
a <- lst(dim(nc), invisible(head_sf = print(nc, n = getOption("sf_max_print", default = 6))))
Any suggestions? thanks
EDIT:
Using invisible
and capture.output
gets pretty much what I wanted (the output wasn't correct when I originally posted this - mistake on my part)
a <- lst(dim(nc), invisible(capture.output(head_sf = print(nc, n = getOption("sf_max_print", default = 6)))))
a
$`dim(nc)`
[1] 100 15
$`invisible(...)`
[1] "Simple feature collection with 100 features and 14 fields"
[2] "geometry type: MULTIPOLYGON"
[3] "dimension: XY"
[4] "bbox: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965"
[5] "geographic CRS: NAD27"
[6] "First 6 features:"
[7] " AREA PERIMETER CNTY_ CNTY_ID NAME FIPS FIPSNO CRESS_ID BIR74 SID74 NWBIR74 BIR79 SID79 NWBIR79 geometry"
[8] "1 0.114 1.442 1825 1825 Ashe 37009 37009 5 1091 1 10 1364 0 19 MULTIPOLYGON (((-81.47276 3..."
[9] "2 0.061 1.231 1827 1827 Alleghany 37005 37005 3 487 0 10 542 3 12 MULTIPOLYGON (((-81.23989 3..."
[10] "3 0.143 1.630 1828 1828 Surry 37171 37171 86 3188 5 208 3616 6 260 MULTIPOLYGON (((-80.45634 3..."
[11] "4 0.070 2.968 1831 1831 Currituck 37053 37053 27 508 1 123 830 2 145 MULTIPOLYGON (((-76.00897 3..."
[12] "5 0.153 2.206 1832 1832 Northampton 37131 37131 66 1421 9 1066 1606 3 1197 MULTIPOLYGON (((-77.21767 3..."
[13] "6 0.097 1.670 1833 1833 Hertford 37091 37091 46 1452 7 954 1838 5 1237 MULTIPOLYGON (((-76.74506 3..."