Skip to content

Commit

Permalink
adds shiny R example
Browse files Browse the repository at this point in the history
  • Loading branch information
edublancas committed Feb 2, 2024
1 parent 0da6eaa commit 196c853
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/shiny-r/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM r-base
COPY . .
RUN Rscript install.R
ENTRYPOINT ["Rscript", "startApp.R"]
54 changes: 54 additions & 0 deletions examples/shiny-r/app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
library(shiny)
library(bslib)
library(dplyr)
library(ggplot2)
library(ggExtra)

penguins_csv <- "https://raw.githubusercontent.com/jcheng5/simplepenguins.R/main/penguins.csv"

df <- readr::read_csv(penguins_csv)
# Find subset of columns that are suitable for scatter plot
df_num <- df |> select(where(is.numeric), -Year)

ui <- page_sidebar(
sidebar = sidebar(
varSelectInput("xvar", "X variable", df_num, selected = "Bill Length (mm)"),
varSelectInput("yvar", "Y variable", df_num, selected = "Bill Depth (mm)"),
checkboxGroupInput(
"species", "Filter by species",
choices = unique(df$Species),
selected = unique(df$Species)
),
hr(), # Add a horizontal rule
checkboxInput("by_species", "Show species", TRUE),
checkboxInput("show_margins", "Show marginal plots", TRUE),
checkboxInput("smooth", "Add smoother"),
),
plotOutput("scatter")
)

server <- function(input, output, session) {
subsetted <- reactive({
req(input$species)
df |> filter(Species %in% input$species)
})

output$scatter <- renderPlot({
p <- ggplot(subsetted(), aes(!!input$xvar, !!input$yvar)) + list(
theme(legend.position = "bottom"),
if (input$by_species) aes(color = Species),
geom_point(),
if (input$smooth) geom_smooth()
)

if (input$show_margins) {
margin_type <- if (input$by_species) "density" else "histogram"
p <- ggExtra::ggMarginal(p, type = margin_type, margins = "both",
size = 8, groupColour = input$by_species, groupFill = input$by_species)
}

p
}, res = 100)
}

shinyApp(ui, server)
6 changes: 6 additions & 0 deletions examples/shiny-r/install.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
install.packages("shiny")
install.packages("bslib")
install.packages("dplyr")
install.packages("ggplot2")
install.packages("ggExtra")
install.packages("readr")
4 changes: 4 additions & 0 deletions examples/shiny-r/startApp.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
library(shiny)
options(shiny.host = '0.0.0.0')
options(shiny.port = 80)
runApp('app.R')

0 comments on commit 196c853

Please sign in to comment.