|
| 1 | +--- |
| 2 | +title: Pairing {quarto-shinylive} and {quarto-webr} |
| 3 | +format: |
| 4 | + html: |
| 5 | + resources: |
| 6 | + - shinylive-sw.js |
| 7 | +engine: knitr |
| 8 | +filters: |
| 9 | + - webr |
| 10 | + - shinylive |
| 11 | +--- |
| 12 | + |
| 13 | +The document is split into two sections. The first section showcases the active |
| 14 | +code cells and the second portion of the document focuses on re-creating |
| 15 | +this document. |
| 16 | + |
| 17 | +# Working Demo |
| 18 | + |
| 19 | +Let's take a quick look at the technologies in action. |
| 20 | + |
| 21 | +:::{.callout-important} |
| 22 | +Shinylive applications are unable to share their contents with each other or |
| 23 | +`{quarto-webr}`. Thus, variables defined in a Shiny app or inside of a |
| 24 | +`{webr-r}` code cell are only available in the defining technology. |
| 25 | +::: |
| 26 | + |
| 27 | +## `{quarto-shinylive}` |
| 28 | + |
| 29 | +Example of a Shinylive application inside of the Quarto document. |
| 30 | + |
| 31 | +```{shinylive-r} |
| 32 | +#| standalone: true |
| 33 | +library(shiny) |
| 34 | +library(bslib) |
| 35 | +library(shinylive) |
| 36 | +
|
| 37 | +is_shinylive <- function() { R.Version()$arch == "wasm32"} |
| 38 | +
|
| 39 | +ui <- page_fillable( |
| 40 | + class = "d-flex flex-column text-center align-items-center justify-content-center", |
| 41 | + h1("Is this app running in shinylive?"), |
| 42 | + textOutput("in_shinylive", container = \(...) p(..., class="fs-2 fw-bold")) |
| 43 | +) |
| 44 | +
|
| 45 | +server <- function(input, output, session) { |
| 46 | + output$in_shinylive <- renderText({ |
| 47 | + if (is_shinylive()) "Yes." else "No." |
| 48 | + }) |
| 49 | +} |
| 50 | +
|
| 51 | +shinyApp(ui, server) |
| 52 | +``` |
| 53 | + |
| 54 | +## `{quarto-webr}` |
| 55 | + |
| 56 | +Example of the {quarto-webr} Extension working alongside a {quarto-shinylive} |
| 57 | +cell. |
| 58 | + |
| 59 | +### Interactive |
| 60 | + |
| 61 | +```{webr-r} |
| 62 | +print("hello quarto-webr world!") |
| 63 | +``` |
| 64 | + |
| 65 | +### Non-interactive |
| 66 | + |
| 67 | +```{webr-r} |
| 68 | +#| context: output |
| 69 | +cat("And, this is a non-interactive cell", fill = TRUE) |
| 70 | +``` |
| 71 | + |
| 72 | +# Walkthrough |
| 73 | + |
| 74 | +In this section, we go step-by-step to create portions of the above code cell |
| 75 | +demo. |
| 76 | + |
| 77 | +## Installation |
| 78 | + |
| 79 | +Please make sure to create a Quarto project and, then, install each extension inside of Terminal: |
| 80 | + |
| 81 | +```sh |
| 82 | +# For shinylive |
| 83 | +quarto add quarto-ext/shinylive |
| 84 | + |
| 85 | +# For quarto-webr |
| 86 | +quarto add coatless/quarto-webr |
| 87 | +``` |
| 88 | + |
| 89 | + |
| 90 | +Both extensions should be found in `_extensions` within the project directory. If not, please double-check your working project directory. |
| 91 | + |
| 92 | +## Quarto Document |
| 93 | + |
| 94 | +From there, please setup a Quarto document (`.qmd`) that contains: |
| 95 | + |
| 96 | +1. the Shiny app source inside of a code cell denoted with `{shinylive-r}`; and, |
| 97 | +2. the code you want webr to use with `{webr-r}`. |
| 98 | + |
| 99 | +Here is an example skeleton Quarto document that has both code cells present: |
| 100 | + |
| 101 | +````md |
| 102 | +--- |
| 103 | +title: Pairing {quarto-shinylive} and {quarto-webr} |
| 104 | +format: |
| 105 | + html: |
| 106 | + resources: |
| 107 | + - shinylive-sw.js |
| 108 | +engine: knitr |
| 109 | +filters: |
| 110 | + - webr |
| 111 | + - shinylive |
| 112 | +--- |
| 113 | + |
| 114 | +## `{quarto-shinylive}` |
| 115 | + |
| 116 | +```{shinylive-r} |
| 117 | +#| standalone: true |
| 118 | +library(shiny) |
| 119 | + |
| 120 | +ui <- fluidPage( |
| 121 | + titlePanel("Hello Shiny!") |
| 122 | +) |
| 123 | + |
| 124 | +server <- function(input, output, session) { |
| 125 | + # code |
| 126 | +} |
| 127 | + |
| 128 | +shinyApp(ui, server) |
| 129 | +``` |
| 130 | + |
| 131 | +## `{quarto-webr}` |
| 132 | + |
| 133 | +```{webr-r} |
| 134 | +print("hello quarto-webr world!") |
| 135 | +``` |
| 136 | + |
| 137 | +```` |
0 commit comments