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

Variable lookup within indexing is broken #69

Open
mschubert opened this issue Jul 8, 2020 · 1 comment
Open

Variable lookup within indexing is broken #69

mschubert opened this issue Jul 8, 2020 · 1 comment

Comments

@mschubert
Copy link

mschubert commented Jul 8, 2020

This is with the current git master.

Consider the following example file (borrowed from the open PR):

library(rhdf5)
f = h5createFile("ex_hdf5file.h5")
B = array(seq(0.1,2.0,by=0.1),dim=c(5,4))
h5write(B, "ex_hdf5file.h5","B")
h5closeAll()

Subsetting this file works fine if I use:

file = rhdf5::H5Fopen("ex_hdf5file.h5")
myx = 1:2
data = file&"/B"
data[myx,]
h5closeAll()

However, if I wrap the loading in a function it fails:

loadx = function() {
    file = rhdf5::H5Fopen("ex_hdf5file.h5")
    on.exit(rhdf5::H5Fclose(file))
    myx = 1:2
    data = file&"/B"
    data[myx,]
}
loadx()
# Error in eval(index[[i]]) : object 'myx' not found
@mgperry
Copy link

mgperry commented Sep 18, 2023

Hi, I've just experienced the same bug.

The cause seems to be line 17 in h5read.R using eval(): for whatever reason, this is called in a different environment to the function, so it won't find the variables. Setting them in the global environment will work, but is obviously not the desired behaviour, since global vars will shadow function arguments.

I don't know if the best solution would be to capture the calling environment, eval the index in a different place, or use a less magical solution. For now you can work around this using un-exported functions (note the triple colon :::) from the package like so:

rhdf5:::h5readDataset(my_dataset, start=a, count=N)

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