Problem with LibLantern library during rsconnect deployment of R torch-based app.

I am trying to publish to rstudio connect server a shiny app, which is a torch-based function. I use R package torch. When I deploy the function to rsconnect, it gives an error logging: "Lantern is not loaded. Please use install_torch() to install additional dependencies." The server installs torch but not its dependencies "LibLantern". How to configure LibLatern installation in rsconnect?

The content of app.R is

embedding_model =  torch::jit_load("./pretrainted_models/immune_metabolism/embedding_model_scripted.pt")
ui <- fluidPage()
server <- function(input, output) {}
shinyApp(ui, server)

The app is deployed at RStudio Connect

Besides the torch R package you also need two libraries, libtorch and liblantern. These are automatically installed in interactive sessions. But a Shiny app on Connect is non-interactive, i.e.

... you need to set the TORCH_INSTALL env var to 1, so it’s automatically installed or manually call torch::install_torch().
[c.f. Installation • torch]

Adding torch::install_torch(reinstall = FALSE) before the tourch::jit_load() should solve this issue.

Adding torch::install_torch(reinstall = FALSE) to the code gives the following error:

Error in value[[3L]](cond) : No write permissions to install torch.
08/03 17:36:39.616 (GMT)
• Check that you can write to: /opt/rstudio-connect/mnt/packrat/4.1.3/v2/library/torch/a198eaa7738bee2d959f2a751ce0bf47/torch
08/03 17:36:39.616 (GMT)
• Or set the TORCH_HOME env var to a path with write permissions.
08/03 17:36:39.616 (GMT)
Calls: local ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>
08/03 17:36:39.616 (GMT)
Execution halted

Right, I forgot that the package library is read-only during app execution. You could try to execute Sys.setenv(TORCH_HOME="/some/path/with write/permissions"). It probably makes sense to incluse packageVersion("torch") somewhere in that path, since the versions of libtorch and liblantern may change with the version of the torch package used.

Alternatively, you could install torch in the system library, install both libtorch and liblantern there and mark torch as an External package, c.f. Package Management - RStudio Connect: Admin Guide.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.