Workshops on Efficient Research Computing - WERC02
Oslo Centre for Biostatistics and Epidemiology
2024-12-13
Say you have something to show an advisee.
# loading data
data <- mtcars
# basic analysis
summary(data)
plot(data)
# regression model
with(data, plot(hp, mpg))
fit <- lm(mpg ~ hp, data)
abline(fit, col = 4)
Problem
This is a fixed script which requires programming knowledge to alter.
Also, one needs R (+ dependencies! 💀) to run this.
Solution
A graphical interface full of buttons that runs on any web browser.
library(shiny)
server <- function(input, output) {
output$smry <- renderPrint(summary(mtcars))
}
ui <- fluidPage(
verbatimTextOutput("smry")
)
shinyApp(ui, server)
See more Shiny components here
library(shiny)
server <- function(input, output) {
output$smry <- renderPrint(summary(mtcars))
output$scatter <- renderPlot(plot(mtcars))
output$reg <- renderPlot(
expr = {
with(mtcars, plot(hp, mpg))
fit <- lm(mpg ~ hp, mtcars)
abline(fit, col = 4)
}
)
}
ui <- fluidPage(
verbatimTextOutput("smry"),
plotOutput("scatter"),
plotOutput("reg")
)
shinyApp(ui, server)
library(shiny)
server <- function(input, output) {
output$smry <- renderPrint(summary(mtcars))
output$scatter <- renderPlot(plot(mtcars))
output$reg <- renderPlot(
expr = {
with(mtcars, plot(hp, mpg))
fit <- lm(mpg ~ hp, mtcars)
abline(fit, col = 4)
}
)
}
ui <- fluidPage(
titlePanel("Analysis of mtcars"),
h1("Summary"),
verbatimTextOutput("smry"),
h1("Scatterplots"),
"Two-by-two plots of all variables.",
plotOutput("scatter"),
h1("Regression results"),
plotOutput("reg")
)
shinyApp(ui, server)
library(shiny)
server <- function(input, output) {
output$smry <- renderPrint(summary(mtcars))
output$scatter <- renderPlot(plot(mtcars))
output$reg <- renderPlot(
expr = {
with(mtcars, plot(mpg, hp))
fit <- lm(hp ~ mpg, mtcars)
abline(fit, col = 4)
}
)
}
ui <- fluidPage(
titlePanel("Analysis of mtcars"),
sidebarLayout(
sidebarPanel(
h1("Settings"),
h2("Summary"),
radioButtons("summaryFun", "Summary function", list("summary", "str", "names")),
h2("Scatterplots"),
checkboxInput("removeDiscrete", "Remove discrete variables"),
h2("Regression"),
selectInput("x", "x-variable", as.list(names(mtcars))),
sliderInput("y-lims", "y-limits", 0, 500, 400),
sliderInput("x-lims", "x-limits", 0, 50, c(10, 40)),
actionButton("randomColor", "Randomize regression color")
),
mainPanel(
h1("Summary"),
verbatimTextOutput("smry"),
h1("Scatterplots"),
"Two-by-two plots of all variables.",
plotOutput("scatter"),
h1("Regression results"),
plotOutput("reg")
)
)
)
shinyApp(ui, server)
library(shiny)
server <- function(input, output) {
output$smry <- renderPrint(
expr = {
sumfun <- switch(input$summaryFun,
summary = summary,
str = str,
names = names
)
sumfun(mtcars)
}
)
output$scatter <- renderPlot(
expr = {
if (input$removeDiscrete) {
nonBinary <- c("mpg", "disp", "hp", "drat", "wt", "qsec")
plot(mtcars[nonBinary])
output$howManyVars <- renderText("continuous")
} else {
plot(mtcars)
output$howManyVars <- renderText("all")
}
}
)
output$reg <- renderPlot(
expr = {
xvar <- mtcars[, input$x]
plot(xvar, mtcars$hp, ylim = c(0, input$ylims), xlim = input$xlims)
fit <- lm(mtcars$hp ~ xvar, mtcars)
abline(fit, col = rv$regcol)
}
)
rv <- reactiveValues(regcol = 4)
observeEvent(input$randomColor, rv$regcol <- sample(1:10, size = 1))
}
ui <- fluidPage(
titlePanel("Analysis of mtcars"),
sidebarLayout(
sidebarPanel(
h1("Settings"),
h2("Summary"),
radioButtons("summaryFun", "Summary function", list("summary", "str", "names")),
h2("Scatterplots"),
checkboxInput("removeDiscrete", "Remove discrete variables"),
h2("Regression"),
selectInput("x", "x-variable", as.list(names(mtcars))),
sliderInput("ylims", "y-limits", 0, 500, 400),
sliderInput("xlims", "x-limits", 0, 50, c(10, 40)),
actionButton("randomColor", "Randomize regression color")
),
mainPanel(
h1("Summary"),
verbatimTextOutput("smry"),
h1("Scatterplots"),
"Two-by-two plots of ", textOutput("howManyVars", inline = TRUE), " variables.",
plotOutput("scatter"),
h1("Regression results"),
plotOutput("reg")
)
)
)
shinyApp(ui, server)
Notice how rv$regcol
was defined after it’s used in abline()
.
rsconnect
to publish on Shinyapps.iolibrary(rsconnect)
rsconnect::deployApp(
1 appFiles = "app.R",
2 account = "ocbe",
appName = "WERC"
)
deployApp()
is very finicky about file names (not well documented!)
App will be available on https://ocbe.shinyapps.io/WERC/
reactlog
ui.R
server.R
crosstalk
and flexdashboard
packages