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?
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.