Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transition() error on pc with R 4.2.2 #15

Closed
gavincotterill opened this issue Jan 13, 2023 · 4 comments
Closed

transition() error on pc with R 4.2.2 #15

gavincotterill opened this issue Jan 13, 2023 · 4 comments

Comments

@gavincotterill
Copy link

I'm getting a generic error from gdistance::transition(). It fails on my pc with R 4.2.2, works on my mac running R 4.2.1, and works on a linux machine running R 4.2.0. Because the error refers to 'extent', I thought it might be coming from raster but after testing a couple of raster versions I think it's more likely from gdistance.

Mac (works) is running gdistance 1.6 and raster 3.5-21.

Pc (fails) using gdistance 1.6 and raster 3.6-11 or raster 3.5-21. But WORKS with gdistance 1.3-6 and raster 3.5-21 or 3.6-11.

No idea what's going on. I'm fine using the older version but thought I'd post this in case it helps sort out an issue that extends beyond my machine.

set.seed(1)

# sapply(c("gdistance"), require, character = T) # also loads raster, sp, igraph, Matrix

beta1 <- 2
n <- 1 

# simulate uniform landscape
npixels <- 60
r <- raster::raster(nrows = npixels, ncols = npixels)
raster::projection(r) <- "+proj=utm +zone=12 +datum=WGS84"
raster::extent(r) <- c(0, npixels, 0, npixels)
raster::values(r) <- 0
sP <- raster::rasterToPoints(r, spatial = TRUE)

# simulate categorical landscape
r.sm <- raster::crop(r, raster::extent(15, 45, 15, 45)) # use only center of raster to choose origins from, so no edge effects

sP2 <- raster::rasterToPoints(r.sm, spatial  = TRUE)
s <- sample(2e3, size = 1) # choose a raster cell as origin of a clump of non-habitat
s2 <- c(rep(s, 10) + seq(1, 10), rep(s + 30, 10) + seq(1, 10), rep(s-30, 10) + seq(1, 10),
        rep(s + 60, 10) + seq(1, 10), rep(s - 60) + seq(1, 10)) # expand into a clump

for(i in 1:8){ # make 8 more clumps
  s <- sample(2e3, size = 1)
  s2 <- c(rep(s, 10) + seq(1, 10), rep(s + 30, 10) + seq(1, 10), rep(s-30, 10) + seq(1, 10), rep(s + 60, 10) + seq(1, 10), rep(s - 60) + seq(1, 10), s2)

}
 
s2 <- s2 + 916
sP$layer[(s2)] <- 1
r <- raster::setValues(r, sP$layer)
var2 <- raster::getValues(r, format = "matrix")
r1 <- r
costcell <- exp(beta1 * var2)
r1 <- raster::setValues(r1, costcell)

is(r1)

#> [1] "RasterLayer" "Raster"      "BasicRaster"

raster::plot(r1)

 

tr1 <- gdistance::transition(x = r1, transitionFunction = function(x) 1/mean(x), directions = 8)

#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'extent': 'srs' is not a slot in class "BasicRaster"

 

Created on 2023-01-13 with reprex v2.0.2

 

Session info

 

sessioninfo::session_info()

#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.2.2 (2022-10-31 ucrt)
#>  os       Windows 10 x64 (build 19044)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  English_United States.utf8
#>  ctype    English_United States.utf8
#>  tz       America/Chicago
#>  date     2023-01-13
#>  pandoc   2.19.2 @ C:/Program Files/RStudio/bin/quarto/bin/tools/ (via rmarkdown)
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.4.1   2022-09-23 [1] CRAN (R 4.2.2)
#>  codetools     0.2-18  2020-11-04 [2] CRAN (R 4.2.2)
#>  curl          4.3.3   2022-10-06 [1] CRAN (R 4.2.2)
#>  digest        0.6.30  2022-10-18 [1] CRAN (R 4.2.2)
#>  evaluate      0.18    2022-11-07 [1] CRAN (R 4.2.2)
#>  fastmap       1.1.0   2021-01-25 [1] CRAN (R 4.2.2)
#>  fs            1.5.2   2021-12-08 [1] CRAN (R 4.2.2)
#>  gdistance     1.6     2022-10-10 [1] CRAN (R 4.2.2)
#>  glue          1.6.2   2022-02-24 [1] CRAN (R 4.2.2)
#>  highr         0.9     2021-04-16 [1] CRAN (R 4.2.2)
#>  htmltools     0.5.3   2022-07-18 [1] CRAN (R 4.2.2)
#>  httr          1.4.4   2022-08-17 [1] CRAN (R 4.2.2)
#>  igraph        1.3.5   2022-09-22 [1] CRAN (R 4.2.2)
#>  knitr         1.41    2022-11-18 [1] CRAN (R 4.2.2)
#>  lattice       0.20-45 2021-09-22 [2] CRAN (R 4.2.2)
#>  lifecycle     1.0.3   2022-10-07 [1] CRAN (R 4.2.2)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.2.2)
#>  Matrix        1.5-1   2022-09-13 [2] CRAN (R 4.2.2)
#>  mime          0.12    2021-09-28 [1] CRAN (R 4.2.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.2.2)
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.2.2)
#>  raster        3.6-11  2022-11-28 [1] CRAN (R 4.2.2)
#>  Rcpp          1.0.9   2022-07-08 [1] CRAN (R 4.2.2)
#>  reprex        2.0.2   2022-08-17 [1] CRAN (R 4.2.2)
#>  rgdal         1.6-3   2022-12-14 [1] CRAN (R 4.2.2)
#>  rlang         1.0.6   2022-09-24 [1] CRAN (R 4.2.2)
#>  rmarkdown     2.18    2022-11-09 [1] CRAN (R 4.2.2)
#>  rstudioapi    0.14    2022-08-22 [1] CRAN (R 4.2.2)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.2.2)
#>  sp            1.5-1   2022-11-07 [1] CRAN (R 4.2.2)
#>  stringi       1.7.8   2022-07-11 [1] CRAN (R 4.2.1)
#>  stringr       1.4.1   2022-08-20 [1] CRAN (R 4.2.2)
#>  terra         1.6-47  2022-12-02 [1] CRAN (R 4.2.2)
#>  withr         2.5.0   2022-03-03 [1] CRAN (R 4.2.2)
#>  xfun          0.35    2022-11-16 [1] CRAN (R 4.2.2)
#>  xml2          1.3.3   2021-11-30 [1] CRAN (R 4.2.2)
#>  yaml          2.3.6   2022-10-18 [1] CRAN (R 4.2.1)
#>
#>  [1] C:/Users/xxx/AppData/Local/R/win-library/4.2
#>  [2] C:/Program Files/R/R-4.2.2/library
#>
#> ──────────────────────────────────────────────────────────────────────────────
@kauedesousa
Copy link
Member

This seems to be similar to this issue #14 . Have you tried this approach?

@gavincotterill
Copy link
Author

Dang. Sorry I missed that, seems like exactly the same issue.
I also failed to notice that I switched raster to 3.6-13 which was released last week. This works with gdistance 1.6, so the bug may have been raster afterall?

@kauedesousa
Copy link
Member

I don't know, I still need to investigate. I had a similar error with the chirps package using S3 methods (gdistance uses S4).

@andrewmarx
Copy link
Contributor

See #14

I'm going to go ahead and close this. I haven't encountered this issue, and I suspect (I haven't verified) the source might have been the terra package, which the raster package uses under the hood.

If anyone encounters this issue again, make sure that terra and raster are both updated. If it's still an issue after that, go ahead and open a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants