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

If coords has wrong dimensions get_biomass() should throw an error #235

Open
ValentineHerr opened this issue Oct 21, 2021 · 2 comments
Open
Labels

Comments

@ValentineHerr
Copy link

I just updated my allodb packages and now the function get_biomass does not work if a matrix is passed to the coords argument. So you may want to change the description here

example:

dat <- structure(list(dbh = c(21.0194119634624, 22.2634119634624), 
                      genus = c("Jacaranada", "Jacaranada"), 
                      species = c("copaia", "copaia"), 
                      Longitude = c(-79.8461, -79.8461), 
                      Latitude = c(9.1543, 9.1543)), 
                 row.names = c("1",  "2"), 
                 class = "data.frame")

# I used to need to use "as.matrix" in coords
get_biomass(dbh=dat$dbh,
            genus=dat$genus,
            species = dat$species,
            coords = as.matrix(dat[, c("Longitude", "Latitude")]) ) 

# now it only works without "as.matrix"
get_biomass(dbh=dat$dbh,
            genus=dat$genus,
            species = dat$species,
            coords = dat[, c("Longitude", "Latitude")]) 


@gonzalezeb
Copy link
Contributor

Hi @maurolepore, please would you take a look at valentine's suggestion?

@maurolepore
Copy link
Member

Thanks!

The reprex below suggests the problem above is not the class of the object ("matrix") but its dimensions. If coords takes a matrix of wrong dimensions, get_biomass() should likely throw an error. @gonzalezeb, do you agree?

However, I would not support a "matrix" because a simpler numeric vector can do the same job. Dropping support for matrix removes code and leaves less room for bugs like this one.

@gonzalezeb, let me know what you think.

devtools::load_all()
#> ℹ Loading allodb
packageVersion("allodb")
#> [1] '0.0.1.9000'

# FIXME: If we support matrix, this should throw an error
(matrix_2x2 <- matrix(rep(c(-79.8461, 9.1543), each = 2), ncol = 2))
#>          [,1]   [,2]
#> [1,] -79.8461 9.1543
#> [2,] -79.8461 9.1543
dim(matrix_2x2)
#> [1] 2 2
get_biomass(
  dbh = 21,
  genus = "Jacaranada",
  species = "copaia",
  coords = matrix_2x2
)
#> numeric(0)

# A matrix works as long as it has correct dimensions
(matrix_1x2 <- unique(matrix_2x2))
#>          [,1]   [,2]
#> [1,] -79.8461 9.1543
dim(matrix_1x2)
#> [1] 1 2
get_biomass(
  dbh = 21,
  genus = "Jacaranada",
  species = "copaia",
  coords = matrix_1x2
)
#> [1] 336.4563

(matrix_2x1 <- t(matrix_1x2))
#>          [,1]
#> [1,] -79.8461
#> [2,]   9.1543
dim(matrix_2x1)
#> [1] 2 1
get_biomass(
  dbh = 21,
  genus = "Jacaranada",
  species = "copaia",
  coords = matrix_2x1
)
#> [1] 336.4563

Created on 2021-10-24 by the reprex package (v2.0.1)

@maurolepore maurolepore changed the title need minor update in help file of get_biomass coords with wrong dimensions should throw an error Oct 24, 2021
@maurolepore maurolepore changed the title coords with wrong dimensions should throw an error if coords has wrong dimensions get_biomass() should throw an error Oct 24, 2021
@maurolepore maurolepore changed the title if coords has wrong dimensions get_biomass() should throw an error If coords has wrong dimensions get_biomass() should throw an error Oct 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants