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
804 stars 54 forks source link

webr support for {gt} package #431

Open rainer-rq-koelle opened 1 month ago

rainer-rq-koelle commented 1 month ago

Congrats! I am just fascinated by what can be done with wasm/webr. I embrace tinkering with it and see a huge potential for teaching/demoing "many things R coding" related. However, I admit that the "magic of its working" is outside my comprehension.

Following some of the tutorials/introductory material, I wanted to use webr to showcase {gt}.
When setting up the Quarto document and rendering it, I hoped that the following would work like a charm. I have failed to find any meaningful pointer(s).

Question on stackoverflow: https://stackoverflow.com/questions/78534930/webr-and-gt-throws-error-error-browser-must-be-a-non-empty-character-stri

The error message thrown from the web-r chunk output reads: Error: 'browser' must be a non-empty character string

Quarto yaml:

engine: knitr filters:

And then let's trial {gt}

my_tbl <- mtcars[1:10,] |> tibble()

my_tbl |> gt()
Screenshot 2024-06-01 at 09 21 34

Calling ?gt in the webr-container on a rendered webpage results in the standard (embedded) help page for {gt}. Thus, the package {gt} appears to be loaded and available in the {webr} session.

Could someone put me on the right track? Thanks.

timelyportfolio commented 1 month ago

@rainer-rq-koelle I am not on the official team, but I do have some experience with html and R, so I'll try to chime in with some hopefully helpful context. This issue relates to the fact that webr does not have a browseURL equivalent viewer (such as RStudio Viewer tab in RStudio or chrome/edge/firefox).

The gt:::print.gt_tbl method in an interactive session will effectively create an html file which then opens with browseURL except we do not have a browser in webr. For instance, htmlwidgets use a very similar mechanism, and these also will not work in a webr Quarto context failing with this same error.

This is a challenging issue, but there might be some hope. I started to explore how we might resolve with htmlwidgets in this post, but unfortunately this is not automated and requires special effort customized to each htmlwidget. Using an example from gt we can see that we ultimately can get the html with view=FALSE (similar to producing gt in Rmd or Quarto without webr).

gt:::print.gt_tbl(exibble |> gt(), view = FALSE)

image

webr just doesn't know what to do with an html file. iframe might be a solution but also can be very clunky.

georgestagg commented 1 month ago

@timelyportfolio is correct. Unfortunately webR does not currently support displaying htmltools or htmlwidgets output in the containing web page. AFAIK this is unsupported in the community-driven quarto-webr extension (https://github.com/coatless/quarto-webr) right now too, though it might be on their radar for future work.


Saying that... I am right now working on significantly improving webR and Quarto integration, including support for displaying HTML widgets from webR in Quarto documents, without any of the overhead work mentioned above. Here is a preview:

Screenshot 2024-06-02 at 18 10 30

This new framework is not yet ready for release, but I'll be continuing to work on it and will release more information about it in the coming weeks.

timelyportfolio commented 1 month ago

@georgestagg this looks amazing!! Please let me know how I can help. I am more than happy to support in any way possible.