r-wasm / webr

The statistical language R compiled to WebAssembly via Emscripten, for use in web browsers and Node.
https://docs.r-wasm.org/webr/latest/
Other
884 stars 69 forks source link

Confused on how to access packages in webr-0.4.0/vfs/usr/lib/R/library folder when hosting webR on local server #465

Open SugarRayLua opened 3 months ago

SugarRayLua commented 3 months ago

Recently trying out self-hosting webR on local server on mobile device (iOS iPad).

Downloaded releases version of webR and hosting on iOS iPad local server app by then opening webr-0.4.0 folder on the local server and pointing the server to index.html. WebR works great by that process and am able to install and use packages from the webR repository.

However, as per many other requests that have been posted, I'm interested in having my most commonly used R packages persist on my device instead of needing to download every time I open up my local server and browser and am more limited by mobile device sandboxing. I noticed that the webr-0.4.0 release that I downloaded includes a library folder:

webr-0.4.0/vfs/usr/lib/R/library

with what appeared to be the webR libraries that are standardly loaded when webR starts, and I presumed that webR loaded its libraries into the virtual FS by loading the libraries in the above folder. If so, I figured I could just download additional webR packaged libraries and add them to the above library folder and then webR would load with the additional packages preconfigured when I start webR. However, that doesn't occur-- despite having a webR package in the above folder on my local server (e.g. cli) it doesn't show up in the virtual FS for me to use when I start webR.

What then are the packages in the above library of the release version of webR for self-hosting used for? Would it be possible to point webR to that folder so that I can add additional libraries to it that would be automatically mounted to the VFS when webR loads on my local hosted server?

thanks!

georgestagg commented 3 months ago

webr-0.4.0/vfs/usr/lib/R/library

Adding to this directory unfortunately won't work, because webR is configured at compile time to load the package files there. It is possible to expand the library, but it requires building webR from scratch.


You may have already noticed that a webR VFS image consists of two files, a .js.metadata file and a .data file. For example, for the {cli} package the VFS image files can be downloaded from the public webR repo:

https://repo.r-wasm.org/bin/emscripten/contrib/4.4/cli_3.6.3.js.metadata https://repo.r-wasm.org/bin/emscripten/contrib/4.4/cli_3.6.3.data

Once you have these files, put them in a subdirectory under index.html, named say packages. Your served directory should now look something like this:

webr-0.4.0/
|-- vfs
|-- R.bin.js
|-- R.bin.wasm
|-- ...some_other_files
|-- index.html
|-- packages/
    |-- cli_3.6.3.js.metadata
    |-- cli_3.6.3.data
    |-- ...

Now, in webR you should be able to mount your packages by creating a new package library, and mounting each package in the VFS. You can run this R script at startup to prepare your filesystem for use[^1].

[^1]: Probably this script can be easily expanded to first download a file containing a list of available package URLs, such as a MANIFEST file, then loop through the URLs mounting each one. It's also possible to create a single VFS image for a collection of packages using the {rwasm} R package.

# Create package library
dir.create("/packages")
.libPaths(c("/packages", .libPaths()))

# Mount each package
dir.create("/packages/cli")
webr::mount("/packages/cli", "./packages/cli_3.6.3.data")

Finally, once that's done you should be able to load your package without downloading from the public webR package repo.

# Load your package(s)
library(cli)
SugarRayLua commented 3 months ago

Okay, thanks, I'll try that! 😊