# ==========================================
#   VALUE INVESTING STOCK SCREENER (SHINY)
#   Using your S&P500 sample dataset
# ==========================================

library(shiny)
## Warning: package 'shiny' was built under R version 4.5.2
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.5.2
## Warning: package 'stringr' was built under R version 4.5.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.0     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.1.0     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# ---------------------------
# LOAD DATA
# ---------------------------
df <- read.csv("sp500_sampled data_2020_2024.txt")

# ---------------------------
# TASK 1: DATA WRANGLING (PIVOT ROE)
# ---------------------------
df_long_roe <- df %>%
  pivot_longer(
    cols = starts_with("ROE_"),
    names_to = "Year",
    values_to = "ROE"
  ) %>%
  mutate(
    Year = as.numeric(gsub("ROE_", "", Year))
  )

# ---------------------------
# TASK 2: FINANCIAL LOGIC (GROWTH STATUS)
# ---------------------------
df <- df %>%
  mutate(
    Growth_Status = if_else(EPS_2024 > EPS_2020, "Growing", "Declining"),
    EPS_Growth_5yr = (EPS_2024 - EPS_2020) / EPS_2020
  )

# ---------------------------------
#            SHINY UI
# ---------------------------------
ui <- fluidPage(

  titlePanel("Value Investing Stock Screener — ROE Trend & EPS Growth"),

  sidebarLayout(
    sidebarPanel(
      selectInput("company", "Select Company:",
                  choices = df$Company)
    ),

    mainPanel(
      h3("5-Year ROE Trend"),
      plotOutput("roePlot"),

      h3("Current P/E vs 5-Year EPS Growth (All Companies)"),
      plotOutput("scatterPlot")
    )
  )
)

# ---------------------------------
#            SHINY SERVER
# ---------------------------------
server <- function(input, output) {

  # ---- ROE Trend Line Chart ----
  output$roePlot <- renderPlot({
    df_long_roe %>%
      filter(Company == input$company) %>%
      ggplot(aes(x = Year, y = ROE)) +
      geom_line(size = 1.2) +
      geom_point(size = 3) +
      labs(title = paste("ROE Trend:", input$company),
           x = "Year",
           y = "ROE")
  })

  # ---- Scatter: PE vs EPS Growth ----
  output$scatterPlot <- renderPlot({
    df %>%
      ggplot(aes(x = PE_Current, y = EPS_Growth_5yr,
                 color = Growth_Status, label = Symbol)) +
      geom_point(size = 4) +
      geom_text(nudge_y = 0.02, size = 3) +
      labs(x = "Current P/E Ratio",
           y = "5-Year EPS Growth",
           title = "P/E vs EPS Growth — All Companies")
  })
}

# ---------------------------------
# RUN APP
# ---------------------------------
shinyApp(ui = ui, server = server)
Shiny applications not supported in static R Markdown documents