library(shiny)
library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(RColorBrewer)

# Read the dataset
call_log <- read.csv("Call Log.new.csv")

# Convert Time to POSIXct format
call_log$Time <- as.POSIXct(call_log$Time, format="%I:%M %p")

# Extract the hour information
call_log$Hour <- as.integer(format(call_log$Time, "%H"))

# Filter only night calls (between 6 pm and 9 pm)
night_calls_data <- call_log %>%
  filter(between(call_log$Hour, 18, 21))

ui <- fluidPage(
  titlePanel("Night Calls Distribution"),
  sidebarLayout(
    sidebarPanel(
      selectInput("plot_type", "Choose Plot Type",
                  choices = c("Histogram", "Density"), selected = "Histogram"),
      selectInput("line_color", "Choose Line Color", choices = brewer.pal(11, "BrBG"), selected = "BrBG[1]"),
      selectInput("fill_color", "Choose Fill Color", choices = brewer.pal(11, "BrBG"), selected = "BrBG[2]"),
    ),
    mainPanel(
      plotOutput("night_calls_plot")
    )
  )
)

server <- function(input, output) {
  output$night_calls_plot <- renderPlot({
    ggplot(night_calls_data, aes(x = Duration..sec. / 60)) +
      switch(input$plot_type,
             "Histogram" = geom_histogram(aes(fill = input$fill_color), bins = 30, alpha = 0.7, position = "identity"),
             "Density" = geom_density(aes(color = input$line_color, fill = input$fill_color), alpha = 0.7)) +
      scale_color_manual(values = input$line_color) +
      scale_fill_manual(values = input$fill_color) +
      labs(title = "Night Calls Distribution",
           x = "Call Duration (Minutes)",
           y = "Frequency") +
      theme_minimal()
  })
}

shinyApp(ui = ui, server = server)
## PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
Shiny applications not supported in static R Markdown documents