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

Fix is.vector() #318

Open
lgatto opened this issue Mar 26, 2024 · 3 comments
Open

Fix is.vector() #318

lgatto opened this issue Mar 26, 2024 · 3 comments

Comments

@lgatto
Copy link
Member

lgatto commented Mar 26, 2024

This line uses is.vector() to check if a variable is an atomic vector, not only a vector (including a list):

> is.vector(1:5)
[1] TRUE
> is.vector(list())
[1] TRUE
> is.vector(1:5, "numeric")
[1] TRUE
> is.vector(list(), "list")
[1] TRUE

The solution here is the also specify a mode:

> is.vector(1:5, "numeric")
[1] TRUE
> is.vector(list(), "list")
[1] TRUE
> is.vector(1:5, "list")
[1] FALSE

I would suggest to use

  • !is.vector(list(), "list")
  • !is.list()

in such cases.

For that specific code chunk, there's no final else. We could probably test for

  1. is.list(.)
  2. inherits(., "List")
  3. assume the rest are atomic vectors.

@jorainer @sgibb - what do you think?

@jorainer
Copy link
Member

Is there a problem with the original code or why do you suggest to change this?

@lgatto
Copy link
Member Author

lgatto commented Mar 27, 2024

I have not observed an issue with the original code.

I just wanted to highlight that is.vector() doesn't test if we have an atomic vector. It doesn't matter here, given that we check if it's a list first, so it implicitly test for an atomic vector.

And while looking at the code, I thought that it could be re-written with a final else. But it doesn't have to be changed; I don't think there's a but - at least in the anticipated use-cases :-)

I thought that additional pairs of (critical and expert) eyes should look at it.

@jorainer
Copy link
Member

I'm OK with the original code - it looks clean/clear and easy to understand. But I'm also OK with changing it is you think it is necessary? Generally I would prefer is.list and similar instead of their negation !is.list - but that's maybe just my poor brain that has sometimes problems thinking around negations, double negations etc.

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

2 participants