Setting server headings to allow use of webr (R in browser)

Short version: I am trying to load R and use it in an iframe inside an anvil app, but I get errors related to headers and do not know if we can change these or work around the problem.

Details
I have the following in the header of the html:

    import('https://webr.r-wasm.org/latest/webr.mjs').then(
      async ({ WebR }) => {
        const webR = new WebR();
        await webR.init();
      }
    );

This gives an error:

R.bin.js:2061 Refused to get unsafe header “Content-Encoding”

Which may be related to a security requirement when loading webR:

To ensure a web page is cross-origin isolated, serve the page with COOP and COEP HTTP headers set to:

Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp

Question: Before I try to fix it, does anyone know whether it is possible to make something like this work? Seems to me that we cannot change the headers in this way in Anvil, or?

I just tried running a minimal example and it seems to still work despite the console error you’re seeing

    def button_click(self, **event_args):
        from anvil.js import import_from
        mod = import_from('https://webr.r-wasm.org/v0.4.2/webr.mjs')
        webR = mod.WebR();
        webR.init()
        x = webR.evalR("rnorm(10,5,1)")
        print(x.toArray())

Interesting. I cannot use import_from since I am using html in a string that appears inside an iframe, but this is more than enough to give me hope that it is possible to make it work. I’ll report back. Thank you!

1 Like

if you share a clone link we may be able to help you restructure the code to make it easier to do so.