## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = FALSE
)

## ----load-libraries, message=FALSE, warning=FALSE-----------------------------
# # Install required packages if not already installed:
# # install.packages(c("yahoofinancer", "shiny", "dplyr", "ggplot2", "scales"))
# 
# library(yahoofinancer)
# library(shiny)
# library(dplyr)
# library(ggplot2)
# library(scales)

## ----ui-layout----------------------------------------------------------------
# ui <- fluidPage(
#   title = "Live Market Dashboard",
#   titlePanel("Live Market Dashboard"),
#   sidebarLayout(
#     sidebarPanel(
#       width = 3,
#       selectInput(
#         inputId  = "symbol",
#         label    = "Ticker",
#         choices  = c("AAPL", "MSFT", "GOOG", "AMZN", "NVDA", "META")
#       ),
#       selectInput(
#         inputId = "period",
#         label = "History length",
#         choices = c(
#           "1 Month" = "1mo", "3 Months" = "3mo",
#           "6 Months" = "6mo", "1 Year" = "1y",
#           "2 Years" = "2y", "5 Years" = "5y"
#         ),
#         selected = "6mo"
#       ),
#       selectInput(
#         inputId = "interval",
#         label   = "Bar interval",
#         choices = c("Daily" = "1d", "Weekly" = "1wk", "Monthly" = "1mo")
#       ),
#       actionButton("refresh", "Refresh data", class = "btn-primary")
#     ),
#     mainPanel(
#       width = 9,
# 
#       # Quote cards row
#       fluidRow(
#         column(4, wellPanel(
#           h4(textOutput("price_card")),
#           p(uiOutput("change_card"), style = "margin-bottom: 0;")
#         )),
#         column(4, wellPanel(
#           h4("52-Week Range"),
#           p(textOutput("range_card"), style = "margin-bottom: 0;")
#         )),
#         column(4, wellPanel(
#           h4("Volume"),
#           p(textOutput("volume_card"), style = "margin-bottom: 0;")
#         ))
#       ),
# 
#       # Price history chart
#       plotOutput("price_chart", height = "360px")
#     )
#   )
# )

## ----server-reactives---------------------------------------------------------
# server <- function(input, output, session) {
#   stats <- eventReactive(input$refresh, {
#     yf_get_market_stats(input$symbol)
#   })
# 
#   prices <- eventReactive(input$refresh, {
#     yf_download_prices(
#       tickers  = input$symbol,
#       period   = input$period,
#       interval = input$interval
#     )
#   })
# 
#   # ... output renderers added in sections 4 and 5 ...
# }

## ----stats-preview------------------------------------------------------------
# yf_get_market_stats("AAPL")
# #> # A tibble: 1 × 7
# #>   symbol regular_market_price fifty_two_week_high fifty_two_week_low
# #>   <chr>                 <dbl>               <dbl>              <dbl>
# #> 1 AAPL                   232.                237.               169.
# #> # i 3 more variables: regular_market_volume <dbl>, previous_close <dbl>,
# #> #   currency <chr>

## ----quote-cards--------------------------------------------------------------
# output$price_card <- renderText({
#   s <- req(stats())
#   req(nrow(s) > 0)
#   sprintf("%s · %s", s$symbol, dollar(s$regular_market_price))
# })
# 
# output$change_card <- renderUI({
#   s <- req(stats())
#   req(nrow(s) > 0)
#   pct <- 100 * (s$regular_market_price - s$previous_close) / s$previous_close
#   color <- if (pct >= 0) "#2e7d32" else "#c62828"
#   tags$span(
#     style = sprintf("color:%s", color),
#     sprintf("%+.2f%% vs previous close", pct)
#   )
# })
# 
# output$range_card <- renderText({
#   s <- req(stats())
#   req(nrow(s) > 0)
#   pos <- 100 * (s$regular_market_price - s$fifty_two_week_low) /
#     (s$fifty_two_week_high - s$fifty_two_week_low)
#   sprintf(
#     "%s — %s\n(%s of 52-week range)",
#     dollar(s$fifty_two_week_low), dollar(s$fifty_two_week_high),
#     percent(pos / 100, accuracy = 1)
#   )
# })
# 
# output$volume_card <- renderText({
#   s <- req(stats())
#   req(nrow(s) > 0)
#   label_number(scale_cut = scales::cut_short_scale())(s$regular_market_volume)
# })

## ----price-chart--------------------------------------------------------------
# output$price_chart <- renderPlot({
#   df <- req(prices())
#   req(nrow(df) > 0)
#   s <- req(stats())
#   req(nrow(s) > 0)
# 
#   ggplot(df, aes(x = date, y = close)) +
#     geom_line(color = "#1f77b4", linewidth = 0.9) +
#     geom_hline(
#       yintercept = s$fifty_two_week_high,
#       linetype = "dashed", color = "#2e7d32"
#     ) +
#     geom_hline(
#       yintercept = s$fifty_two_week_low,
#       linetype = "dashed", color = "#c62828"
#     ) +
#     scale_y_continuous(labels = dollar_format()) +
#     labs(
#       title    = paste(s$symbol, "Price History"),
#       subtitle = "Dashed lines mark 52-week high and low",
#       x        = NULL,
#       y        = "Price (USD)",
#       caption  = "Source: Yahoo Finance via yahoofinancer"
#     ) +
#     theme_minimal(base_size = 12) +
#     theme(plot.title = element_text(face = "bold"))
# })

## ----full-workflow------------------------------------------------------------
# library(yahoofinancer)
# library(shiny)
# library(dplyr)
# library(ggplot2)
# library(scales)
# 
# ui <- fluidPage(
#   titlePanel("Live Market Dashboard"),
#   sidebarLayout(
#     sidebarPanel(
#       width = 3,
#       selectInput("symbol", "Ticker",
#         choices = c("AAPL", "MSFT", "GOOG", "AMZN", "NVDA", "META")
#       ),
#       selectInput("period", "History length",
#         choices = c(
#           "1 Month" = "1mo", "3 Months" = "3mo",
#           "6 Months" = "6mo", "1 Year" = "1y",
#           "2 Years" = "2y", "5 Years" = "5y"
#         ),
#         selected = "6mo"
#       ),
#       selectInput("interval", "Bar interval",
#         choices = c(
#           "Daily" = "1d", "Weekly" = "1wk",
#           "Monthly" = "1mo"
#         )
#       ),
#       actionButton("refresh", "Refresh data", class = "btn-primary")
#     ),
#     mainPanel(
#       width = 9,
#       fluidRow(
#         column(4, wellPanel(
#           h4(textOutput("price_card")),
#           p(uiOutput("change_card"), style = "margin-bottom: 0;")
#         )),
#         column(4, wellPanel(
#           h4("52-Week Range"),
#           p(textOutput("range_card"), style = "margin-bottom: 0;")
#         )),
#         column(4, wellPanel(
#           h4("Volume"),
#           p(textOutput("volume_card"), style = "margin-bottom: 0;")
#         ))
#       ),
#       plotOutput("price_chart", height = "360px")
#     )
#   )
# )
# 
# server <- function(input, output, session) {
#   stats <- eventReactive(input$refresh, yf_get_market_stats(input$symbol))
#   prices <- eventReactive(input$refresh, {
#     yf_download_prices(input$symbol,
#       period = input$period,
#       interval = input$interval
#     )
#   })
# 
#   output$price_card <- renderText({
#     s <- req(stats())
#     req(nrow(s) > 0)
#     sprintf("%s · %s", s$symbol, dollar(s$regular_market_price))
#   })
#   output$change_card <- renderUI({
#     s <- req(stats())
#     req(nrow(s) > 0)
#     pct <- 100 * (s$regular_market_price - s$previous_close) / s$previous_close
#     color <- if (pct >= 0) "#2e7d32" else "#c62828"
#     tags$span(
#       style = sprintf("color:%s", color),
#       sprintf("%+.2f%% vs previous close", pct)
#     )
#   })
#   output$range_card <- renderText({
#     s <- req(stats())
#     req(nrow(s) > 0)
#     pos <- 100 * (s$regular_market_price - s$fifty_two_week_low) /
#       (s$fifty_two_week_high - s$fifty_two_week_low)
#     sprintf(
#       "%s — %s\n(%s of range)",
#       dollar(s$fifty_two_week_low),
#       dollar(s$fifty_two_week_high),
#       percent(pos / 100, accuracy = 1)
#     )
#   })
#   output$volume_card <- renderText({
#     s <- req(stats())
#     req(nrow(s) > 0)
#     label_number(scale_cut = cut_short_scale())(s$regular_market_volume)
#   })
# 
#   output$price_chart <- renderPlot({
#     df <- req(prices())
#     req(nrow(df) > 0)
#     s <- req(stats())
#     req(nrow(s) > 0)
#     ggplot(df, aes(x = date, y = close)) +
#       geom_line(color = "#1f77b4", linewidth = 0.9) +
#       geom_hline(
#         yintercept = s$fifty_two_week_high,
#         linetype = "dashed", color = "#2e7d32"
#       ) +
#       geom_hline(
#         yintercept = s$fifty_two_week_low,
#         linetype = "dashed", color = "#c62828"
#       ) +
#       scale_y_continuous(labels = dollar_format()) +
#       labs(title = paste(s$symbol, "Price History"), x = NULL, y = "Price (USD)") +
#       theme_minimal(base_size = 12) +
#       theme(plot.title = element_text(face = "bold"))
#   })
# }
# 
# shinyApp(ui, server)

