Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webr chunks in a document with shiny #4

Open
sachsmc opened this issue Mar 13, 2023 · 2 comments
Open

webr chunks in a document with shiny #4

sachsmc opened this issue Mar 13, 2023 · 2 comments

Comments

@sachsmc
Copy link

sachsmc commented Mar 13, 2023

I'm having some difficulty getting webr to work in a document with some shiny reactive elements, that I run with quarto serve. Here is an simple example. When I run this with quarto serve, the webr chunk is missing. If I remove the shiny stuff and run with quarto preview, the webr chunk appears as expected. I'm not seeing any error messages in the javascript console. Any ideas what might be going wrong?

---
title: "Testing"
format: html
server: shiny
filters:
  - webr
---


## Webr

```{r}
rnorm(1)
```


```{webr}
fit = lm(mpg ~ am, data = mtcars)
summary(fit)
```



## shiny

```{r}
library(shiny)
sliderInput("bins", "Number of bins:", 
            min = 1, max = 50, value = 30)
plotOutput("distPlot")
```

```{r}
#| context: server
output$distPlot <- renderPlot({
  x <- faithful[, 2]  # Old Faithful Geyser data
  bins <- seq(min(x), max(x), length.out = input$bins + 1)
  hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
```
@coatless
Copy link
Owner

coatless commented Mar 15, 2023

What server: shiny is doing is suppressing the echo of all code cells. So, if the code cell is set to echo: true, then the webR box should open up. Thus, with this knowledge we now have:

shiny-backend-webr-js-look-up-issue

However, the Shiny app embedded in Quarto is not able to find the necessary .js files. I've tried placing them in /js, /www, and _files to no avail based on:

https://quarto.org/docs/interactive/shiny/resources.html#asset-resources

Please note, we need the echo component as quarto extensions come into play in the later stage of the render where pandoc is:
rstudio-qmd-how-it-works

With this being said, if you can figure out where the Shiny app is launching from, I can try and figure out how to make sure the worker files are in the correct location by default for the server:shiny backend.

Feel free to explore with:

---
title: "Testing with Shiny Backend"
format: html
keep-md: true
server: shiny
filters:
  - webr
---

```{r}
knitr::knit_engines$set(webr = knitr:::eng_verbatim)
```

## Webr

```{r}
rnorm(1)
```

Note, we added the `#| echo: true` remark:

```{webr}
#| echo: true
1 + 1
```

## shiny

```{r}
library(shiny)

sliderInput("bins", "Number of bins:", 
            min = 1, max = 50, value = 30)
plotOutput("distPlot")
```

```{r}
#| context: server
output$distPlot <- renderPlot({
  x <- faithful[, 2]  # Old Faithful Geyser data
  bins <- seq(min(x), max(x), length.out = input$bins + 1)
  hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
```

@sachsmc
Copy link
Author

sachsmc commented Mar 15, 2023

Thanks for your response. I tried the following and it seems that it is able to find the worker files:

  1. Put the webr-worker.js and webr-servicework.js files in a js/ subdirectory
  2. Modify line 21 of the webr-init.html file to:
    globalThis.webR = new WebR({SW_URL: 'js/'});

Then when I run quarto serve test.qmd, I can see the following messages in the javascript console:

webR service worker installed serviceworker.ts:15
webR service worker activating serviceworker.ts:15

However the Run button seems to be stuck at "Loading webR...", and I am never able to run the R code. Does this give you any clues? Could it be something in the shiny javascript code that is blocking the webR? Sorry, I am no js expert.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants