It is the next generation of what Rmarkdown, bookdown, blogdown (and all of the ādownsā) started.
Code chunks can target R, python, SQl, bash, juila, and so on.
This is an open source technical publishing system that is independent of the target language (i.e., R, python, etc.).
Historically, to let {people who are not you} run a shiny app, youād need:
shinyapps.io
, Posit Connect)The server runs your code and renders the results on your computer/phone/tv etc.
To use a Shiny app in Quarto, you would need to embed a frame that would show the app (from the external shiny server)
An example appā¦
#| label: fig-umap
#| viewerHeight: 550
#| standalone: true
library(shiny)
library(ggplot2)
library(bslib)
library(viridis)
# ------------------------------------------------------------------------------
light_bg <- "#fcfefe" # from aml4td.scss
grid_theme <- bs_theme(
bg = light_bg, fg = "#595959"
)
# ------------------------------------------------------------------------------
theme_light_bl<- function(...) {
ret <- ggplot2::theme_bw(...)
col_rect <- ggplot2::element_rect(fill = light_bg, colour = light_bg)
ret$panel.background <- col_rect
ret$plot.background <- col_rect
ret$legend.background <- col_rect
ret$legend.key <- col_rect
ret$legend.position <- "top"
ret
}
# ------------------------------------------------------------------------------
ui <- fluidPage(
theme = grid_theme,
fluidRow(
column(
width = 4,
sliderInput(
inputId = "min_dist",
label = "Min Distance",
min = 0.0,
max = 1.0,
value = 0.2,
width = "100%",
step = 0.2
)
), # min distance
column(
width = 4,
sliderInput(
inputId = "neighbors",
label = "Neighbors",
min = 5,
max = 45,
value = 5,
width = "100%",
step = 10
)
), # nearest neighbors
column(
width = 4,
sliderInput(
inputId = "supervised",
label = "Amount of Supervision",
min = 0.0,
max = 0.7,
value = 0,
width = "100%",
step = 0.1
)
),
fluidRow(
column(
width = 4,
radioButtons(
inputId = "initial",
label = "Initialization",
choices = list("Laplacian Eigenmap" = "spectral", "PCA" = "pca",
"Random" = "random")
)
),
column(
width = 6,
align = "center",
plotOutput('umap')
)
)
) # top fluid row
)
server <- function(input, output) {
load(url("https://raw.githubusercontent.com/aml4td/website/main/RData/umap_results.RData"))
output$umap <-
renderPlot({
dat <-
umap_results[
umap_results$neighbors == input$neighbors &
umap_results$min_dist == input$min_dist &
umap_results$initial == input$initial &
# log10(umap_results$learn_rate) == input$learn_rate &
umap_results$supervised == input$supervised,
]
p <-
ggplot(dat, aes(UMAP1, UMAP2, col = barley)) +
geom_point(alpha = 1 / 3, cex = 3) +
scale_color_viridis(option = "viridis") +
theme_light_bl() +
coord_fixed() +
labs(x = "UMAP Embedding #1", y = "UMAP Embedding #2") +
guides(col = guide_colourbar(barheight = 0.5))
print(p)
})
}
app <- shinyApp(ui = ui, server = server)
R can be compiled to WebAssembly (aka wasm)
See George Staggās video for more information.
You can see which R packages can be used at the repository site. In mid May:
A project for deploying Shiny applications that will run completely in the browser via webR
shinylive does almost all of the heavy lifting for you.
Iāll focus on adding shiny apps to Quarto documents (a book, in my case)
Download the extension for your Quarto project
Each shiny app goes in a single code chunk.
Instead of using {r}
in the header, it is {shinylive-r}
.
Also use the standalone
option:
To declare packages, use library()
calls
Currently
Upcoming
The one rule is:
You donāt get automatic access to your local objects in your workspace
Opening raw network sockets is not permitted from the WebAssembly sandbox (no curl).
webR patches download.file()
to use an XHR requests (JS API for HTTP requests).
Itās a work in progress. (GH issue: Add local folder support).
From Gordon Shotwell:
```{shinylive-r}
#| label: fig-potato
#| standalone: true
library(tidymodels)
1ļøā£ # other library() calls
2ļøā£ load("https://raw.githubusercontent.com/{user}/{repo}/{branch}/{file}")
3ļøā£ source("https://raw.githubusercontent.com/{user}/{repo}/{branch}/{file}")
4ļøā£ app
```
Packages should be listed in the code chunk.
Load the data in the code chunk.
source()
with a local file wonāt work.
Return the shiny app in the code chunk.
For many applications, shinylive is not the answer:
Client computing power may not be great.
Moving data to the client may be expensive.
Data/Code security is paramount to many.
Niceties of authentication, parameterization, support, etc that Connect offers.
Must watch: Running R-Shiny without a Server - posit::conf(2023)
Examples:
Specific examples by George Stagg:
Thanks for the invitation to speak again!
Thanks to George Stagg, Joe Cheng, Winston Chang, Gordon Shotwell, and everyone else who made webR
and shinylive
happen.