Skip to content

Instantly share code, notes, and snippets.

@AlbertRapp
Created March 21, 2023 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AlbertRapp/2f04b3e9cdf4a96cb4245b6e82de239f to your computer and use it in GitHub Desktop.
Save AlbertRapp/2f04b3e9cdf4a96cb4245b6e82de239f to your computer and use it in GitHub Desktop.
shiny_modules_60_seconds.R
setwd(here::here('02_shiny_modules'))
library(shiny)
library(ggplot2)
ui <- fluidPage(
theme = bslib::bs_theme(bootswatch = 'flatly'),
tabsetPanel(
tabPanel("Penguins", {
sidebarLayout(
sidebarPanel(
selectInput(
'select_var_1_penguins',
'Variable 1',
choices = names(palmerpenguins::penguins)
),
selectInput(
'select_var_2_penguins',
'Variable 2',
choices = names(palmerpenguins::penguins)
),
actionButton(
'draw_scatterplot_penguins',
'Draw scatterplot',
)
),
mainPanel(
plotOutput('scatterplot_penguins')
)
)
}),
tabPanel("Iris", {
sidebarLayout(
sidebarPanel(
selectInput(
'select_var_1_iris',
'Variable 1',
choices = names(iris)
),
selectInput(
'select_var_2_iris',
'Variable 2',
choices = names(iris)
),
actionButton(
'draw_scatterplot_iris',
'Draw scatterplot',
)
),
mainPanel(
plotOutput('scatterplot_iris')
)
)
})
)
)
server <- function(input, output, session) {
output$scatterplot_penguins <- renderPlot({
ggplot(palmerpenguins::penguins) +
geom_point(
aes_string(input$select_var_1_penguins, input$select_var_2_penguins),
size = 3,
alpha = 0.5,
col = 'dodgerblue4'
)
}) |> bindEvent(input$draw_scatterplot_penguins)
output$scatterplot_iris <- renderPlot({
ggplot(iris) +
geom_point(
aes_string(input$select_var_1_iris, input$select_var_2_iris),
size = 3,
alpha = 0.5,
col = 'dodgerblue4'
)
}) |> bindEvent(input$draw_scatterplot_iris)
}
shinyApp(ui, server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment