[1] 0.6000000000000000888178
Posit, PBC
A fundamental principle of the scientific method is peer preview and verification.
Researchers increasingly publishing data, infrastructure, and software.
In some areas, becoming an institutional or regulatory requirement.
Taking lessons and inspiration from free and open source software.
There is some level of subjectivity here: βeasilyβ
There are levels of reproducibility, some are easier than others.
More info:
π Building reproducible analytical pipelines with R, Bruno Rodrigues
π ONS RAP Companion
π Software Carpentry, Data Carpentry
Recognise there is no guarantee your script will run successfully anywhere else.
It works on my machine Β―\(γ)/Β―
Some simple things to watch out for:
Hard-coded paths, setwd()
, project organisation.
Ensure all source files and datasets can be loaded.
Workflow management, order of execution.
Avoidable programming errors.
A small effort here, even using automated tools, makes a big difference.
Source code management, git
, GitHub.
Defensive programming, handling error conditions.
Organising software into modules or packages.
Lifecycle management, avoid deprecated functionality.
Documentation and tests.
No magic bullet: Good software engineering leads to lasting projects.
Research Software Engineering: https://society-rse.org, https://us-rse.org
Many different computational environments exist, all with the potential to affect your analysis.
Software source code is compiled into a machine-language binary. But the same software can give different binaries depending on the type of hardware (ARM vs x86_64 vs RISC)
Also provides benefits for security in the form of containerisation and sandboxing.
The webR project is a version of the R interpreter built for WebAssembly.
Execute R code directly in a web browser, without a supporting R server. Alternatively, run an R process server-side using Node.js
Available on GitHub and NPM as a JavaScript & TypeScript library.
Run Shiny applications entirely in a web browser, without the need for a computational server.
Requires a reproducible workflow and software installation:
runApp()
, debugging.Wouldnβt it be great if we could run Shiny apps locally, without installing any extra software?
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.
```{shinylive-r}
#| standalone: true
library(shiny)
# Create Shiny UI
ui <- [...]
# Create Shiny server function
server <- function(input, output, session) {
[...]
}
# Build Shiny app
shinyApp(ui = ui, server = server)
```
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit laborum.
#| standalone: true
#| viewerHeight: 700
library(shiny)
library(bslib)
theme <- bs_theme(font_scale = 1.5)
# Define UI for app that draws a histogram ----
ui <- page_sidebar(theme = theme,
sidebar = sidebar(open = "open",
numericInput("n", "Sample count", 50),
checkboxInput("pause", "Pause", FALSE),
),
plotOutput("plot", width=1100)
)
server <- function(input, output, session) {
data <- reactive({
input$resample
if (!isTRUE(input$pause)) {
invalidateLater(1000)
}
rnorm(input$n)
})
output$plot <- renderPlot({
hist(data(),
breaks = 30,
xlim = c(-2, 2),
ylim = c(0, 1),
xlab = "value",
freq = FALSE,
main = ""
)
x <- seq(from = -2, to = 2, length.out = 500)
y <- dnorm(x)
lines(x, y, lwd=1.5)
lwd <- 5
abline(v=0, col="red", lwd=lwd, lty=2)
abline(v=mean(data()), col="blue", lwd=lwd, lty=1)
legend(legend = c("Normal", "Mean", "Sample mean"),
col = c("black", "red", "blue"),
lty = c(1, 2, 1),
lwd = c(1, lwd, lwd),
x = 1,
y = 0.9
)
}, res=140)
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)
Install the Shinylive R package:
Convert the app:
Binary bundle ready to transfer to another machine or host on a static web service.
Binary R packages for Wasm are available from a CRAN-like CDN:
R packages are always updating and changing.
Despite best efforts, changes and deprecations can break older code.
With the next version of Shinylive, Wasm R package binaries will be frozen and bundled with an app automatically.
Apps will continue to work in the future, without changes, even as the webR CRAN-like repository updates.
Wasm binaries will be downloaded from R-Universe personal package repositories.
Custom packages built for Wasm using r-wasm/actions will be downloaded from GitHub.
#| standalone: true
#| viewerHeight: 800
library(shiny)
library(dplyr)
ui <- fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30),
sliderInput(inputId = "filter",
label = "Filter:",
min = 45,
max = 99,
value = 90)
),
mainPanel(
plotOutput(outputId = "distPlot")
)
)
)
server <- function(input, output) {
output$distPlot <- renderPlot({
x <- faithful |> dplyr::pull(waiting)
xf <- faithful |> dplyr::filter(waiting < input$filter) |> dplyr::pull(waiting)
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(xf, breaks = bins, col = "#75AADB", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")
})
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)
Pilot Shiny app submissions to the FDA investigating containers and WebAssembly.
π Testing Containers and WebAssembly in Submissions to the FDA - pharmaverse.github.io
Not all R packages work under WebAssembly.
Building custom R packages using GitHub Actions is still experimental.
Documentation updates and clarifications to follow.
There will always be good reasons to use a traditional Shiny deployment.
Browser security restrictions: limited networking, no raw socket access.
π± There are no secrets with a Shinylive app!
All code and data is sent to the client, deploy accordingly.
https://webr.r-wasm.org/v0.3.2/
https://docs.r-wasm.org/webr/v0.3.2/
https://github.com/posit-dev/shinylive
https://github.com/quarto-ext/shinylive