Odd sf::st_crop() behaviour - r

I want to crop one sf object by another, which I can do with sf::st_crop() or sf::st_intersection(). However, the resulting cropped object, doesn't match the object it was cropped by (see below reprex). Instead it looks like the crop happened on the top left corner and the bottom right. How do I get it to actually clip to the outline of poly1?
library(sf); library(rnaturalearth)
# polygon to clip by
poly1 <- matrix(c(-113, 23.5,
-113, -23.5,
-34, -23.5,
-34, 23.5,
-113, 23.5),
byrow=T, ncol=2) %>%
list() %>%
st_polygon() %>%
st_sfc(., crs = "epsg:4326")
countries <- ne_countries(continent = c("South America", "North America"),
returnclass = "sf") %>%
st_transform(., crs="epsg:4326")
plot(countries$geometry)
plot(poly1, add=T, border = "red")
cr1 <- st_crop(countries, poly1)
plot(cr1$geometry, add=T, col="red")
# intersection does the same
cr2 <- st_intersection(countries, poly1)
plot(cr2, add=T, col="green")
EDIT: it turns out that if I revert to a previous version of R (which also has different package versions attached), this error goes away.
Session info where issue arises:
> sessionInfo()
R version 4.1.0 (2021-05-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8.1 x64 (build 9600)
Matrix products: default
locale:
[1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252 LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C LC_TIME=English_United Kingdom.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rnaturalearth_0.1.0 sf_1.0-0
loaded via a namespace (and not attached):
[1] Rcpp_1.0.7 magrittr_2.0.1 units_0.7-2 tidyselect_1.1.1 lattice_0.20-44 R6_2.5.1 rlang_0.4.11
[8] fansi_0.5.0 s2_1.0.6 dplyr_1.0.7 wk_0.4.1 tools_4.1.0 grid_4.1.0 KernSmooth_2.23-20
[15] utf8_1.2.2 e1071_1.7-7 DBI_1.1.1 rgeos_0.5-5 ellipsis_0.3.2 class_7.3-19 assertthat_0.2.1
[22] tibble_3.1.4 lifecycle_1.0.0 crayon_1.4.1 purrr_0.3.4 vctrs_0.3.8 glue_1.4.2 sp_1.4-5
[29] proxy_0.4-26 compiler_4.1.0 pillar_1.6.2 generics_0.1.0 classInt_0.4-3 pkgconfig_2.0.3
Session info where it doesn't:
> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8.1 x64 (build 9600)
Matrix products: default
locale:
[1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252 LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C LC_TIME=English_United Kingdom.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rnaturalearth_0.1.0 sf_0.9-7
loaded via a namespace (and not attached):
[1] Rcpp_1.0.6 pillar_1.4.6 compiler_3.6.3 plyr_1.8.6 class_7.3-15 tools_3.6.3 lifecycle_0.2.0
[8] tibble_3.0.4 gtable_0.3.0 lattice_0.20-41 pkgconfig_2.0.3 rlang_0.4.10 DBI_1.1.1 rstudioapi_0.11
[15] yaml_2.2.1 e1071_1.7-4 dplyr_1.0.0 stringr_1.4.0 rgeos_0.5-2 generics_0.1.0 vctrs_0.3.4
[22] classInt_0.4-3 grid_3.6.3 tidyselect_1.1.0 glue_1.4.2 ggnewscale_0.4.3 R6_2.4.1 sp_1.4-5
[29] purrr_0.3.4 reshape2_1.4.4 ggplot2_3.3.2.9000 magrittr_2.0.1 scales_1.1.1 ellipsis_0.3.1 units_0.6-7
[36] rsconnect_0.8.16 colorspace_1.4-1 KernSmooth_2.23-16 stringi_1.5.3 munsell_0.5.0 crayon_1.3.4

This turned out to be an issue with sf_1.0-0 following the move to using s2. It's fixed by running sf_use_s2(FALSE) at the start of the script.

Related

could not find function "dbconnect" in R

I am running RStudio Version 1.4.1106 and R 4.0.4. Additionally, have deleted the following packages and reinstalled them; odbc, DBI, and data.table. Next, I load them (library(odbc) and library(DBI)) and so on. However, when I run the following command, I still get the following error. What else do I need to do
Error in dbConnect(odbc::odbc(), "DB") :
could not find function "dbConnect"
sessioninfo() gives the following result
R version 4.0.4 (2021-02-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] DBI_1.1.1 odbc_1.3.0
loaded via a namespace (and not attached):
[1] Rcpp_1.0.5 rstudioapi_0.11 magrittr_1.5 hms_0.5.3
[5] bit_4.0.4 tidyselect_1.1.0 munsell_0.5.0 colorspace_1.4-1
[9] R6_2.4.1 rlang_0.4.8 fansi_0.4.1 blob_1.2.1
[13] dplyr_1.0.2 tools_4.0.4 grid_4.0.4 gtable_0.3.0
[17] xfun_0.19 tinytex_0.27 cli_2.1.0 ellipsis_0.3.1
[21] bit64_4.0.5 assertthat_0.2.1 tibble_3.0.4 lifecycle_0.2.0
[25] crayon_1.3.4 purrr_0.3.4 ggplot2_3.3.2 vctrs_0.3.4
[29] glue_1.4.2 compiler_4.0.4 pillar_1.4.6 generics_0.0.2
[33] scales_1.1.1 pkgconfig_2.0.3
>

R studio just stops plotting - repeatedly, how to fix

I have a recurring problem where R studio just stops plotting. The code for plots works fine, as the plots are saving to file, but they just stop showing up in the plot pane.
Restarting the whole session seems to fix, but then I have to reload everything and it happens again almost immediately.
Would be grateful for any insight.
> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)
Matrix products: default
locale:
[1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C
[5] LC_TIME=English_United Kingdom.1252
attached base packages:
[1] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] RColorBrewer_1.1-2 pheatmap_1.0.12 stringr_1.4.0 gplots_3.1.1 viridis_0.5.1
[6] viridisLite_0.3.0 VennDiagram_1.6.20 futile.logger_1.4.3 tibble_3.0.5 fuzzyjoin_0.1.6
[11] ggthemes_4.2.4 tidyr_1.1.2 dplyr_1.0.3 data.table_1.13.6 readr_1.4.0
[16] ggVennDiagram_0.3 ggrepel_0.9.1 ggplot2_3.3.3 cowplot_1.1.1
loaded via a namespace (and not attached):
[1] gtools_3.8.2 tidyselect_1.1.0 purrr_0.3.4 sf_0.9-7 colorspace_2.0-0
[6] vctrs_0.3.6 generics_0.1.0 utf8_1.1.4 rlang_0.4.10 e1071_1.7-4
[11] pillar_1.4.7 glue_1.4.2 withr_2.4.0 DBI_1.1.1 lambda.r_1.2.4
[16] lifecycle_0.2.0 munsell_0.5.0 gtable_0.3.0 caTools_1.18.1 labeling_0.4.2
[21] class_7.3-17 fansi_0.4.2 Rcpp_1.0.6 KernSmooth_2.23-17 scales_1.1.1
[26] classInt_0.4-3 formatR_1.7 farver_2.0.3 gridExtra_2.3 digest_0.6.27
[31] hms_1.0.0 stringi_1.5.3 cli_2.2.0 tools_4.0.3 bitops_1.0-6
[36] magrittr_2.0.1 futile.options_1.0.1 crayon_1.3.4 pkgconfig_2.0.3 ellipsis_0.3.1
[41] assertthat_0.2.1 rstudioapi_0.13 R6_2.5.0 units_0.6-7 compiler_4.0.3

Why is coord_sf() throwing an ERROR: st_normalize.sfc...domain must have a positive range?

My code for creating some maps stopped working after I updated R and libraries recently.
geom_sf() is throwing an error which id did not have before.
library("sf")
library("ggplot2")
library("maps")
usa = st_as_sf(maps::map('state',region = c("WA", "OR"), plot = FALSE, fill = TRUE))
base <-
ggplot() +
geom_sf(data = usa)
base +
coord_sf(
xlim = c(-123.4,-121.8),
ylim = c(48.99, 47.6),
expand = FALSE
)
Error in st_normalize.sfc(x, c(x_range[1], y_range[1], x_range[2], y_range[2])) :
domain must have a positive range
A similar question was posted here but there was a problem with reproducibility and no real answer.
My session info:
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] maps_3.3.0 ggplot2_3.3.3 sf_0.9-7
loaded via a namespace (and not attached):
[1] Rcpp_1.0.6 rstudioapi_0.13 magrittr_2.0.1 units_0.6-7 tidyselect_1.1.0
[6] munsell_0.5.0 ggspatial_1.1.5 colorspace_2.0-0 here_1.0.1 R6_2.5.0
[11] rlang_0.4.10 dplyr_1.0.3 tools_4.0.3 grid_4.0.3 gtable_0.3.0
[16] KernSmooth_2.23-18 e1071_1.7-4 DBI_1.1.1 withr_2.4.0 class_7.3-17
[21] ellipsis_0.3.1 yaml_2.2.1 rprojroot_2.0.2 assertthat_0.2.1 tibble_3.0.5
[26] lifecycle_0.2.0 crayon_1.3.4 farver_2.0.3 purrr_0.3.4 vctrs_0.3.6
[31] glue_1.4.2 compiler_4.0.3 pillar_1.4.7 generics_0.1.0 scales_1.1.1
[36] classInt_0.4-3 pkgconfig_2.0.3
Because you inverted the latitudes' values.
This should work:
base +
coord_sf(
xlim = c(-123.4,-121.8),
ylim = c(47.6, 48.99),
#ylim = c(48.99, 47.6), ## here was the error
expand = FALSE
)

Why doesn't 'Tidy' in Broom work as intended for an epi.2by2 object?

I am using the package epiR to calculate measures of association (rate ratio and attributable risk). Results are outputted to a list. I want to get these estimates into one overall dataframe - I thought Broom could do this - see here.
This is what I have attached:
R version 3.5.3 (2019-03-11)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] forcats_0.4.0 stringr_1.4.0 purrr_0.3.2 readr_1.3.1 tidyr_0.8.3 tibble_3.0.1 ggplot2_3.1.1
[8] tidyverse_1.2.1 dplyr_0.8.1 broom_0.5.6
loaded via a namespace (and not attached):
[1] Rcpp_1.0.1 cellranger_1.1.0 pillar_1.4.4 compiler_3.5.3 plyr_1.8.4 tools_3.5.3 jsonlite_1.6
[8] lubridate_1.7.4 lifecycle_0.2.0 nlme_3.1-137 gtable_0.3.0 lattice_0.20-38 pkgconfig_2.0.2 rlang_0.4.5
[15] Matrix_1.2-17 cli_1.1.0 rstudioapi_0.10 haven_2.1.0 withr_2.1.2 xml2_1.2.0 httr_1.4.0
[22] generics_0.0.2 vctrs_0.2.4 hms_0.4.2 grid_3.5.3 tidyselect_0.2.5 glue_1.3.1 R6_2.4.0
[29] readxl_1.3.1 modelr_0.1.4 magrittr_1.5 backports_1.1.4 scales_1.0.0 ellipsis_0.3.0 rvest_0.3.4
[36] assertthat_0.2.1 colorspace_1.4-1 stringi_1.4.3 lazyeval_0.2.2 munsell_0.5.0 crayon_1.3.4
>
This is the code I have run:
tabh7 <- table(ispa.h7$mat2,ispa.h7$cov,ispa.h7$Sex)
rh7 <- epi.2by2(dat = tabh7, method = "cohort.count",
conf.level = 0.95, units = 100, outcome = "as.columns")
tidy(rh7, parameters = "moa")
And this is the message:
Error: No tidy method for objects of class epi.2by2
I have tried deleting my .RHistory. I never save my workspaces. What could be the problem?

Working code stopped working when reloading rStudio

I have several chunks of code that use pipes and many of them have stopped working when I reloaded rStudio that did not happen before. The following code was working before:
library(dplyr)
df <- data.frame(a = c("A", "B", "A","B" ,"A" ,"B"), b = c(1, 2, 3,4,5,6))
df %>% count(a)
but now it tells me Error in UseMethod("as.quoted") : no applicable method for 'as.quoted' applied to an object of class "function" has to be written as:
count(df$a)
or:
df %>% count(vars="a")
any ideas on what I could have done? rStudio says it's up to date, I'm using R 3.6.1 and I also tried reinstalling dplyr.
EDIT:
here is information from sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] shiny_1.4.0 lubridate_1.7.4 tidyr_1.0.0 plyr_1.8.4
[5] dplyr_0.8.3
loaded via a namespace (and not attached):
[1] Rcpp_1.0.3 rstudioapi_0.10 knitr_1.26 magrittr_1.5
[5] tidyselect_0.2.5 xtable_1.8-4 R6_2.4.1 rlang_0.4.2
[9] fastmap_1.0.1 stringr_1.4.0 tools_3.6.1 xfun_0.11
[13] htmltools_0.4.0 assertthat_0.2.1 digest_0.6.23 tibble_2.1.3
[17] lifecycle_0.1.0 crayon_1.3.4 later_1.0.0 purrr_0.3.3
[21] promises_1.1.0 vctrs_0.2.0 rsconnect_0.8.15 zeallot_0.1.0
[25] mime_0.7 glue_1.3.1 stringi_1.4.3 compiler_3.6.1
[29] pillar_1.4.2 backports_1.1.5 httpuv_1.5.2 pkgconfig_2.0.3

Resources