r-wasm / webr

The statistical language R compiled to WebAssembly via Emscripten, for use in web browsers and Node.

Home Page:https://docs.r-wasm.org/webr/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Output buffer difference between `Shelter.captureR()` and `Console()`

coatless opened this issue · comments

Relevant ticket: coatless/quarto-webr#208

The main issue is the lines from writing a data.frame with gt() to STDOUT are exceeding the buffer associated with Shelter.captureR() but not Console().

So, the webR REPL is able to correctly output the results; but, the Shelter.captureR() is not able to.

webr::install("gt")
gt::gt(mtcars) |> print(view = FALSE)

Under REPL:

Screenshot showing the `gt()` class being output under the webR REPL environment that uses Console to process output

Under Shelter.captureR():

Screenshot showing the `gt()` class being output under a `quarto-webr` code cell that uses `Shelter.captureR()` to obtain and process output

This is a consequence of how R output is captured when running in the non-console context.

See:

#define BUFSIZE 10000
struct output_con_data {
SEXP output;
char buf[BUFSIZE];
char *line, *cur;
};

and:

if (res < 0) {
data->cur[0] = '\0';
} else if (res >= BUFSIZE - curlen) {
Rf_warning("printing of extremely long output is truncated");
data->buf[BUFSIZE - 1] = '\0';
res = BUFSIZE - curlen - 1;
}
data->cur += res < 0 ? 0 : res;


We should consider:

  • Increasing BUFSIZE in the short term.
  • Adding a mechanism to dynamically grow output_con_data in the longer term.

The OP issue would also be solved by handling htmlwidget output correctly, avoiding need to capture raw HTML and output: asis. I'm working on this separately as part of our previously discussed Quarto/webR integration project.