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

part_sblock is using && operators with vectors that have length bigger than 1 and is causing an error #379

Open
victoragcosta opened this issue May 29, 2024 · 0 comments

Comments

@victoragcosta
Copy link

part_sblock is using && operators with vectors that have length bigger than 1 and is causing an error

Description of error

When calling part_sblock, the following error is printed to the console

Error in unique(Opt2$spa_auto) && unique(Opt2$env_sim) : 
  'length = 5' in coercion to 'logical(1)'

Possible cause

I've read a bit of the codebase searching for the line that is giving that error and found it inside the file R/part_sblock.R on line 606. Here's the line with surrounding code that might be relevant:

# file: R/part_sblock.R

  # Code...
  
  # SELLECTION OF THE BEST CELL SIZE----
  Opt2 <- Opt                                           # Line 564
  rownames(Opt2) <- colnames(part)
  Dup <-
    if (any(unique(pa) == 0)) {
      !duplicated(Opt2[c("spa_auto", "env_sim", "sd_p", "sd_a")])
    } else {
      !duplicated(Opt2[c("spa_auto", "env_sim", "sd_p")])
    }

  Opt2 <- Opt2[Dup, ]

  while (nrow(Opt2) > 1) {
    # I MORAN
    if (nrow(Opt2) == 1) {
      break
    }
    Opt2 <-
      Opt2[which(Opt2$spa_auto <= summary(Opt2$spa_auto)[2]), ]
    if (nrow(Opt2) == 1) {
      break
    }
    # Euclidean
    Opt2 <-
      Opt2[which(Opt2$env_sim >= summary(Opt2$env_sim)[5]), ]
    if (nrow(Opt2) == 1) {
      break
    }
    # SD presence
    Opt2 <-
      Opt2[which(Opt2$sd_p <= summary(Opt2$sd_p)[2]), ]
    if (nrow(Opt2) == 2) {
      break
    }
    # SD absences
    if (any(unique(pa) == 0)) {
      Opt2 <-
        Opt2[which(Opt2$sd_a <= summary(Opt2$sd_a)[2]), ]
      if (nrow(Opt2) == 2) {
        break
      }
    }
  
    if (unique(Opt2$spa_auto) &&                        # Line 606 - ERROR HAPPENS HERE!
      unique(Opt2$env_sim) && unique(Opt2$sd_p)) {
      Opt2 <- Opt2[nrow(Opt2), ]
    }
  }

  if (nrow(Opt2) > 1) {
    Opt2 <- Opt2[nrow(Opt2), ]
  }

  # More code...

I'm pretty sure the error is caused by this change in R version 4.3.0:

Calling && or || with LHS or (if evaluated) RHS of length greater than one is now always an error in version 4.3.0

You can access the page I got this information here.

Possible fix

Basically the problem is that && and || now give errors when one of the evaluated arguments has a length bigger than 1. Originally these operators would simply use the first element of the vector, so I believe the equivalent code would be:

    if (unique(Opt2$spa_auto)[1] &&
      unique(Opt2$env_sim)[1] && unique(Opt2$sd_p)[1]) {
      Opt2 <- Opt2[nrow(Opt2), ]
    }

Extra notes

I don't know how to program in R or what the intention of this statement is, I'm just trying to help a friend that has no formal education in programming and ended up getting this error. I'm sorry, but I'm not able to provide more context, like steps to reproduce or which data was fed into the function, right now. However I think that you can fix this without more context, but, if needed, just ask for it and I'll try to get it with my friend.

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

1 participant