r-wasm / quarto-live

WebAssembly powered code blocks and exercises for both the R and Python languages in Quarto documents
https://r-wasm.github.io/quarto-live/
MIT License
100 stars 6 forks source link

FR : A way to disable runtime errors in OJS for R functions defined in webr #43

Open arnaud-feldmann opened 1 month ago

arnaud-feldmann commented 1 month ago

I've discovered the webr/ojs interactivity possibilities. It's nice. I've tested something like that :

```{webr}
#| edit: false
#| output: false
#| define:
#|   - ok_reponse
library(htmltools)
ok_reponse <- function(reponse, n) {
  if (is.na(reponse)) HTML("")
  else if (reponse == n) div(HTML("Bonne réponse ✓"), style = "color: green")
  else div(HTML("Mauvaise réponse ✗"), style = "color: red")
}
//| echo: false
viewof reponse = Inputs.radio(
new Map([
["On n'utilise que des procédures (des fonctions sans valeur de retour).", 1],
["On n'utilise que des fonctions dites pures, c'est-à-dire des fonctions purement combinatoires.", 2],
["On n'utilise que des fonctions, pures ou non.", 3],
["Différentes étapes successives qu'on appelle fonctions. On les applique sur des objets qu'elles modifient progressivement.", 4]
])
);
ok_reponse(reponse, 2);
This is a funny way to enable easy interactivity with R. However before the loading, the R functions lead to OJS runtime errors. Is there a way to disable these ones ?
So far  I've been able to get the intended result by writing an additional ojs cell.
function ok_reponse(reponse, n) {
  return html``;
}

This writes nothing and gets replaced when webr is loaded. Hence avoiding runtime errors.

However, it looks like an ugly patch. Is there, or could it be, a good practice to do the same thing ? Like a default value for a definition ?
georgestagg commented 3 weeks ago

However before the loading, the R functions lead to OJS runtime errors. Is there a way to disable these ones ?

No, but it is likely that in the future we'll try to replace the error message with something more informative, letting the user know that the WebAssembly engine is still loading and to wait.

This writes nothing and gets replaced when webr is loaded. Hence avoiding runtime errors. However, it looks like an ugly patch.

I know it seems strange, but I can't think of a better way to handle this with Observable itself. IIUC even Quarto has to make some tweaks to hide these kind of error messages as the page loads.

It does seem like this is the idiomatic way to do this in OJS. See, for example, the example notebook about how to catch errors: https://observablehq.com/d/43fb4c8c2f600135


Here is how I would implement it in Quarto Live, by adding an extra mutable variable to the first line of the block:

```{ojs}
//| echo: false
mutable ok_reponse = (reponse, n) => { return html`Loading...` };
viewof reponse = Inputs.radio(
  new Map([
  ["On n'utilise que des procédures (des fonctions sans valeur de retour).", 1],
  ["On n'utilise que des fonctions dites pures, c'est-à-dire des fonctions purement combinatoires.", 2],
  ["On n'utilise que des fonctions, pures ou non.", 3],
  ["Différentes étapes successives qu'on appelle fonctions. On les applique sur des objets qu'elles modifient progressivement.", 4]
  ])
);
ok_reponse(reponse, 2);


I think the above could possibly even be implemented automatically as part of the quarto-live filter, I will have a think about how best to do so.