An Introduction to R Shiny-Live Demo
R
Shiny
Interactive
Demontration
Meetup description
haddaden Mohamed El Fodil introduces RShiny that makes it easy to build interactive web apps. He then builds an impromptu shiny app for the meetup session.
About Speaker
He is a Shiny developer and an RStudio certified instructor. He also has a PhD in Economics and an avid R and Shiny developer.
Ihaddaden M. EL Fodil personal website
An Introduction to Shiny
Shiny allows you to interact with the data by changing the data and analysis. The following code is a demonstration of a shiny app from https://shiny.posit.co/.
Learn how to develop a shiny app from here
library(shiny)
library(bslib)
library(dplyr)
library(ggplot2)
library(ggExtra)
<- "https://raw.githubusercontent.com/jcheng5/simplepenguins.R/main/penguins.csv"
penguins_csv
<- readr::read_csv(penguins_csv)
df # Find subset of columns that are suitable for scatter plot
<- df |> select(where(is.numeric), -Year)
df_num
<- page_sidebar(
ui theme = bs_theme(bootswatch = "minty"),
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")
)
<- function(input, output, session) {
server <- reactive({
subsetted req(input$species)
|> filter(Species %in% input$species)
df
})
$scatter <- renderPlot({
output<- ggplot(subsetted(), aes(!!input$xvar, !!input$yvar)) + list(
p theme(legend.position = "bottom"),
if (input$by_species) aes(color = Species),
geom_point(),
if (input$smooth) geom_smooth()
)
if (input$show_margins) {
<- if (input$by_species) "density" else "histogram"
margin_type <- ggExtra::ggMarginal(p, type = margin_type, margins = "both",
p size = 8, groupColour = input$by_species, groupFill = input$by_species)
}
pres = 100)
},
}
shinyApp(ui, server)