r-wasm / jupyterlite-webr-kernel

An R kernel for JupyterLite, based on webR.
https://r-wasm.github.io/jupyterlite-webr-kernel/
MIT License
37 stars 7 forks source link

Error in plot /pdf(...): failed to load default encoding #10

Open psychemedia opened 1 week ago

psychemedia commented 1 week ago

I am trying to run a local version of webr via the JupyterLite (3.3) kernel in a self-contained package served usig flask and bundled using pyinstaller. It works well enough on a Mac, but on Windows I get an Error: Error in pdf(...): failed to load default encoding when I try a simple plot.

image

Any suggestions about workarounds / fixes much appreciated.

If I run:

Sys.getlocale()
localeToCharset()

On both Mac and Win I get: [1] "UTF-8" "ISO8859-1"

psychemedia commented 1 week ago

Encodings in: /vfs/usr/lib/R/library/grDevices/enc.js.metadata:

{"files":[{"filename":"/AdobeStd.enc","start":0,"end":2094},
{"filename":"/AdobeSym.enc","start":2094,"end":4608},
{"filename":"/CP1250.enc","start":4608,"end":6832},
{"filename":"/CP1251.enc","start":6832,"end":9221},
{"filename":"/CP1253.enc","start":9221,"end":11406},
{"filename":"/CP1257.enc","start":11406,"end":13653},
{"filename":"/Cyrillic.enc","start":13653,"end":16002},
{"filename":"/Greek.enc","start":16002,"end":18214},
{"filename":"/ISOLatin1.enc","start":18214,"end":20427},
{"filename":"/ISOLatin2.enc","start":20427,"end":22614},
{"filename":"/ISOLatin7.enc","start":22614,"end":24886},
{"filename":"/ISOLatin9.enc","start":24886,"end":27082},
{"filename":"/KOI8-R.enc","start":27082,"end":29372},
{"filename":"/KOI8-U.enc","start":29372,"end":31680},
{"filename":"/MacRoman.enc","start":31680,"end":33942}
{"filename":"/PDFDoc.enc","start":33942,"end":36149},
{"filename":"/TeXtext.enc","start":36149,"end":38240},
{"filename":"/WinAnsi.enc","start":38240,"end":40551}]
,"remote_package_size":40551}

Settings in webkernel.ts::setupEnvironment():

options(device = function(...){
        pdf(...)
        dev.control("enable")
      }, webr.plot.new = FALSE)

Using constructions of the form pdf(encoding="CP1250.enc", ...) have no effect other than changing the form of the error message (Error: Error in pdf(encoding = "CP1250.enc", ...): failed to load default encoding etc.).

psychemedia commented 1 week ago

Try with canvas? Notes on setting driver options to canvas here: https://docs.r-wasm.org/webr/latest/plotting.html

psychemedia commented 1 week ago

Exploring:

georgestagg commented 1 week ago

Could you post an image of the browser's network log from DevTools? Are there any downloading errors as you run the plot() command (as in the first example)?

Does setting the device option to the following instead work?

options(device = function(...){
    png(...)
    dev.control("enable")
 }, webr.plot.new = FALSE)

plot(1)

It seems strange that OS matters here. I wonder if there is some issue with line ending in the served VFS content.

psychemedia commented 6 days ago

Ok - the above device option works - thanks for that tip...

There were no issues in the dev log otherwise, eg from the following in a new kernel:

image

So the encoding file is read but then there's some sort of mismatch presumably against the claimed default encoding and the encoding catalogue?