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

Failure to load Lantern error escapes tryCatch() function #1157

Closed
trinetra75 opened this issue May 14, 2024 · 7 comments
Closed

Failure to load Lantern error escapes tryCatch() function #1157

trinetra75 opened this issue May 14, 2024 · 7 comments

Comments

@trinetra75
Copy link

trinetra75 commented May 14, 2024

Hello,

I'm trying to optionally integrate torch in my R package, but,
when I tried to check whether the torch library is effectively available to run,
the following code did still fail to execute:

    tryCatch({
      library("torch", character.only = TRUE)
    },
    error = function(err) {
      useTorch <- FALSE
   })

with this error:

Error: processing vignette 'Guided_tutorial_v2.Rmd' failed with diagnostics:
Lantern is not loaded. Please use `install_torch()` to install additional dependencies.

Would it be possible to have this error fall within the range of the tryCatch() function?

PS: Maybe I'm doing something wrong, but I tried to look anywhere on the resources and didn't find anything about this case.
So I'm asking here: which is the best way to have the torch as optional feature?

@dfalbel
Copy link
Member

dfalbel commented May 14, 2024

This doesn't look like the error that torch would show when failing to start, but an error caused by trying to use torch functions with lantern not installed.

Are you sure that all torch calls are avoided when useTorch = FALSE?

@trinetra75
Copy link
Author

trinetra75 commented May 14, 2024

I'm pretty sure that no torch command is executed once the flag is set to FALSE.
And I'm also relatively sure the above is the offending command (it is in a vignette running on a Bioconductor machine...)

Again I'm pretty open to suggestions on how to query whether the torch package is ready to be used or not :)

@dfalbel
Copy link
Member

dfalbel commented May 14, 2024

torch::torch_is_installed() is the function that checks if torch binaries are correctly installed. You can use it to check for toprch in your vignette.

I see the problem now. Actually we are not allowed to raise an error in library() for CRAN rules, so we just allow library() to just ignore the error on non interactive environments.

@trinetra75
Copy link
Author

Thank you for your prompt answers!

Does your suggestion implies I can use torch::torch_is_installed() before/instead of library("torch")?
Naturally assuming I already checked the presence of the torch library with requireNamespace("torch", quietly = TRUE)...

@dfalbel
Copy link
Member

dfalbel commented May 14, 2024

Yes, I think you can do something like:

useTorch <- requireNamespace("torch", quietly = TRUE) && torch::torch_is_installed()

@trinetra75
Copy link
Author

Thank you!

I'll try asap and report if it worked...

@trinetra75
Copy link
Author

Hi, the code you suggested, worked perfectly:

useTorch <- requireNamespace("torch", quietly = TRUE) && torch::torch_is_installed()

Thank you very much!

Might I suggest to include this code in the documentation/examples?
I suppose I'm not the only one that might need this... Thanks again!

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