# Define server logic required to sort data and draw plot
shinyServer(function(input, output) {
output$diamondsPlot <- renderPlot({
# gather info from user but only when asked
num_diamonds <- isolate(input$diamonds)
types_cut <- isolate(input$cut)
# listen to go button
input$go
# sample correct number
diamonds_to_plot <- sample_n(diamonds, num_diamonds)
# filter cut
diamonds_to_plot <- filter(diamonds_to_plot, cut %in% types_cut)
# draw correct plot
if (input$plot_type == "Barplot") {
# draw barplot
ggplot(diamonds_to_plot, aes(x = cut, fill = cut)) +
geom_bar()
} else {
# draw scatterplot
ggplot(diamonds_to_plot, aes(x = carat, y = price, colour = cut)) +
geom_point(size = 2.5)
}
})
})
# load packages
library(shiny)
library(tidyverse)
# put diamond cut in alphabetical order
diamond_choices <- levels(diamonds$cut)
# Define what the user sees
shinyUI(fluidPage(
# Application title
titlePanel("Diamonds"),
# Sidebar with inputs for user to control
sidebarLayout(
sidebarPanel(
selectInput(label = "Type of plot", inputId = "plot_type",
choices = c("Scatterplot", "Barplot")),
sliderInput(label = "Number of Diamonds",
inputId = "diamonds",
min = 1, max = 10000, value = 500),
selectizeInput(label = "Choose cut",
inputId = "cut",
choices = diamond_choices,
selected = diamond_choices,
multiple = TRUE),
actionButton(inputId = "go", label = "Plot Data")
),
# Show the generated plot
mainPanel(plotOutput("diamondsPlot"))
)
))