Running ‘cast.R’ [1s/1s]
  Comparing ‘cast.Rout’ to ‘cast.Rout.save’ ...4c4
< Linking to GEOS 3.5.1, GDAL 2.1.2, proj.4 4.9.3
---
> Linking to GEOS 3.5.1, GDAL 2.1.3, proj.4 4.9.2
  Running ‘crs.R’ [0s/1s]
  Comparing ‘crs.Rout’ to ‘crs.Rout.save’ ... OK
  Running ‘dist.R’ [1s/1s]
  Comparing ‘dist.Rout’ to ‘dist.Rout.save’ ... OK
  Running ‘dplyr.R’ [2s/2s]
Running the tests in ‘tests/dplyr.R’ failed.
Complete output:
  > suppressPackageStartupMessages(library(sf))
  > library(dplyr)
  
  Attaching package: 'dplyr'
  
  The following objects are masked from 'package:stats':
  
      filter, lag
  
  The following objects are masked from 'package:base':
  
      intersect, setdiff, setequal, union
  
  > nc = st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
  > nc %>% filter(AREA > .1) %>% plot()
  Warning message:
  plotting the first 10 out of 14 attributes; use max.plot = 14 to plot all 
  > 
  > # plot 10 smallest counties in grey:
  > nc %>% 
  +   select(BIR74, geometry) %>% 
  +   plot()
  > 
  > nc %>% 
  +   select(AREA, geometry) %>% 
  +   arrange(AREA) %>% 
  +   slice(1:10) %>% 
  +   plot(add = TRUE, col = 'grey', main ="")
  > 
  > # select: check both when geometry is part of the selection, and when not:
  > nc %>% select(SID74, SID79) %>% names()
  [1] "SID74"    "SID79"    "geometry"
  > nc %>% select(SID74, SID79, geometry) %>% names()
  [1] "SID74"    "SID79"    "geometry"
  > nc %>% select(SID74, SID79) %>% class()
  [1] "sf"         "data.frame"
  > nc %>% select(SID74, SID79, geometry) %>% class()
  [1] "sf"         "data.frame"
  > 
  > # group_by:
  > nc$area_cl = cut(nc$AREA, c(0, .1, .12, .15, .25))
  > nc %>% group_by(area_cl) %>% class()
  [1] "sf"         "grouped_df" "tbl_df"     "tbl"        "data.frame"
  > 
  > # mutate:
  > nc2 <- nc %>% mutate(area10 = AREA/10)
  > 
  > # transmute:
  > nc %>% transmute(AREA = AREA/10, geometry = geometry) %>% class()
  [1] "sf"         "data.frame"
  > nc %>% transmute(AREA = AREA/10) %>% class()
  [1] "sf"         "data.frame"
  > 
  > # rename:
  > nc2 <- nc %>% rename(area = AREA)
  > 
  > # distinct:
  > nc[c(1:100,1:10),] %>% distinct() %>% nrow()
  [1] 100
  > 
  > # summarize:
  > nc$area_cl = cut(nc$AREA, c(0, .1, .12, .15, .25))
  > nc.g <- nc %>% group_by(area_cl)
  > nc.g %>% summarise(mean(AREA))
  Simple feature collection with 4 features and 2 fields
  geometry type:  MULTIPOLYGON
  dimension:      XY
  bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
  epsg (SRID):    4267
  proj4string:    +proj=longlat +datum=NAD27 +no_defs
  # A tibble: 4 x 3
        area_cl `mean(AREA)`          geometry
         <fctr>        <dbl>  <simple_feature>
  1     (0,0.1]   0.07602857 <MULTIPOLYGON...>
  2  (0.1,0.12]   0.11206667 <MULTIPOLYGON...>
  3 (0.12,0.15]   0.13422727 <MULTIPOLYGON...>
  4 (0.15,0.25]   0.19039286 <MULTIPOLYGON...>
  > nc.g %>% summarize(mean(AREA)) %>% plot(col = 3:6/7)
  > 
  > library(tidyr)
  > 
  > # time-wide to long table, using tidyr::gather
  > # stack the two SID columns for the July 1, 1974 - June 30, 1978 and July 1, 1979 - June 30, 1984 periods
  > # (see https://cran.r-project.org/web/packages/spdep/vignettes/sids.pdf)
  > nc %>% select(SID74, SID79, geometry) %>% gather(VAR, SID, -geometry) %>% summary()
       VAR                 SID                  geometry  
   Length:200         Min.   : 0.000   MULTIPOLYGON :200  
   Class :character   1st Qu.: 2.000   epsg:4267    :  0  
   Mode  :character   Median : 5.000   +proj=long...:  0  
                      Mean   : 7.515                      
                      3rd Qu.: 9.000                      
                      Max.   :57.000                      
  > 
  > # spread:
  > nc$row = 1:100
  > nc.g <- nc %>% select(SID74, SID79, row) %>% gather(VAR, SID, -row, -geometry)
  > nc.g %>% tail()
  Simple feature collection with 6 features and 3 fields
  geometry type:  MULTIPOLYGON
  dimension:      XY
  bbox:           xmin: -79.0745 ymin: 33.88199 xmax: -76.2877 ymax: 35.01666
  epsg (SRID):    4267
  proj4string:    +proj=longlat +datum=NAD27 +no_defs
      row   VAR SID                       geometry
  195  95 SID79   4 MULTIPOLYGON(((-77.14895629...
  196  96 SID79   5 MULTIPOLYGON(((-78.26149749...
  197  97 SID79   3 MULTIPOLYGON(((-78.02592468...
  198  98 SID79  17 MULTIPOLYGON(((-78.65571594...
  199  99 SID79   9 MULTIPOLYGON(((-77.96073150...
  200 100 SID79   6 MULTIPOLYGON(((-78.65571594...
  > nc.g %>% spread(VAR, SID) %>% head()
  Simple feature collection with 6 features and 3 fields
  geometry type:  MULTIPOLYGON
  dimension:      XY
  bbox:           xmin: -81.74107 ymin: 36.07282 xmax: -75.77316 ymax: 36.58965
  epsg (SRID):    4267
  proj4string:    +proj=longlat +datum=NAD27 +no_defs
    row SID74 SID79                       geometry
  1   1     1     0 MULTIPOLYGON(((-81.47275543...
  2   2     0     3 MULTIPOLYGON(((-81.23989105...
  3   3     5     6 MULTIPOLYGON(((-80.45634460...
  4   4     1     2 MULTIPOLYGON(((-76.00897216...
  5   5     9     3 MULTIPOLYGON(((-77.21766662...
  6   6     7     5 MULTIPOLYGON(((-76.74506378...
  > nc %>% select(SID74, SID79, geometry, row) %>% gather(VAR, SID, -geometry, -row) %>% spread(VAR, SID) %>% head()
  Simple feature collection with 6 features and 3 fields
  geometry type:  MULTIPOLYGON
  dimension:      XY
  bbox:           xmin: -81.74107 ymin: 36.07282 xmax: -75.77316 ymax: 36.58965
  epsg (SRID):    4267
  proj4string:    +proj=longlat +datum=NAD27 +no_defs
    row SID74 SID79                       geometry
  1   1     1     0 MULTIPOLYGON(((-81.47275543...
  2   2     0     3 MULTIPOLYGON(((-81.23989105...
  3   3     5     6 MULTIPOLYGON(((-80.45634460...
  4   4     1     2 MULTIPOLYGON(((-76.00897216...
  5   5     9     3 MULTIPOLYGON(((-77.21766662...
  6   6     7     5 MULTIPOLYGON(((-76.74506378...
  > 
  > # test st_set_crs in pipe:
  > sfc = st_sfc(st_point(c(0,0)), st_point(c(1,1)))
  > x <- sfc %>% st_set_crs(4326) %>% st_transform(3857)
  > x
  Geometry set for 2 features 
  geometry type:  POINT
  dimension:      XY
  bbox:           xmin: 0 ymin: -7.081155e-10 xmax: 111319.5 ymax: 111325.1
  epsg (SRID):    3857
  proj4string:    +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs
  POINT(0 -7.08115455161362e-10)
  POINT(111319.490793274 111325.142866385)
  > 
  > library(units)
  > person = make_unit("person")
  > nc = st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
  > nc.merc <- st_transform(nc, 32119) # NC State Plane
  > 
  > nc.merc <- nc.merc %>% mutate(area = st_area(nc.merc), dens = BIR74 * person /area)
  > 
  > # summary(nc.merc$dens) # requires units 0.4-2
  > nc.merc$area_cl <- cut(nc$AREA, c(0, .1, .12, .15, .25))
  > nc.grp <- nc.merc %>% group_by(area_cl)
  > 
  > out <- nc.grp %>% summarise(A = sum(area), pop = sum(dens * area), 
  + 	new_dens = sum(dens * area)/sum(area)) 
  > 
  > # mean densities depend on grouping:
  > nc.merc %>% summarize(mean(dens))
  Simple feature collection with 1 feature and 1 field
  geometry type:  MULTIPOLYGON
  dimension:      XY
  bbox:           xmin: 123829 ymin: 14744.69 xmax: 930521.8 ymax: 318259.9
  epsg (SRID):    32119
  proj4string:    +proj=lcc +lat_1=36.16666666666666 +lat_2=34.33333333333334 +lat_0=33.75 +lon_0=-79 +x_0=609601.22 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
                 mean(dens)                       geometry
  1 2.593234e-06 person/m^2 MULTIPOLYGON(((834893.70840...
  > out %>% summarise(mean(new_dens))
  Simple feature collection with 1 feature and 1 field
  geometry type:  MULTIPOLYGON
  dimension:      XY
  bbox:           xmin: 123829 ymin: 14744.69 xmax: 930521.8 ymax: 318259.9
  epsg (SRID):    32119
  proj4string:    +proj=lcc +lat_1=36.16666666666666 +lat_2=34.33333333333334 +lat_0=33.75 +lon_0=-79 +x_0=609601.22 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
  # A tibble: 1 x 2
           `mean(new_dens)`          geometry
                    <units>  <simple_feature>
  1 2.589362e-06 person/m^2 <MULTIPOLYGON...>
  > 
  > # total densities don't:
  > nc.merc %>% summarise(sum(area * dens))
  Simple feature collection with 1 feature and 1 field
  geometry type:  MULTIPOLYGON
  dimension:      XY
  bbox:           xmin: 123829 ymin: 14744.69 xmax: 930521.8 ymax: 318259.9
  epsg (SRID):    32119
  proj4string:    +proj=lcc +lat_1=36.16666666666666 +lat_2=34.33333333333334 +lat_0=33.75 +lon_0=-79 +x_0=609601.22 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
    sum(area * dens)                       geometry
  1    329962 person MULTIPOLYGON(((834893.70840...
  > out %>% summarise(sum(A * new_dens))
  Simple feature collection with 1 feature and 1 field
  geometry type:  MULTIPOLYGON
  dimension:      XY
  bbox:           xmin: 123829 ymin: 14744.69 xmax: 930521.8 ymax: 318259.9
  epsg (SRID):    32119
  proj4string:    +proj=lcc +lat_1=36.16666666666666 +lat_2=34.33333333333334 +lat_0=33.75 +lon_0=-79 +x_0=609601.22 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
  # A tibble: 1 x 2
    `sum(A * new_dens)`          geometry
                <units>  <simple_feature>
  1       329962 person <MULTIPOLYGON...>
  > 
  > conn = system.file("gpkg/nc.gpkg", package = "sf")
  > db = src_sqlite(conn)
  Error: The dbplyr package is required to communicate with database backends.
  Execution halted