Define UI for application that draws a chart of stock symbol

## Warning: package 'bslib' was built under R version 4.0.3
## This version of bslib is designed to work with shiny version 1.5.0.9007 or higher.
# Define UI for application that draws a chart of stock symbol
shinyUI <- fluidPage(
  tags$head(tags$style(" h2{color: blue;};
                           label{font-size: 10px;}"
              )
  ),
  
  # Application title
  titlePanel("A Glance-At-Stock App"),
  
  
  # Sidebar with a slider input for charts
  sidebarLayout(
    sidebarPanel(
      selectizeInput("chart", "Stock Symbol", c("GOOGL", "TSLA", "FB", 
                                                "AMZN", "AAPL", "MSFT")),
      radioButtons("PriceInput", "Volume Change Across Closing Prices (Line Plot)",
                   choices = c("over $100", "over $300", "over $500"),
                   selected = "over $100")
      
      
    ),
   
        # Show a plot of the generated distribution
    mainPanel(
            column(8, offset=9,
                   tags$h5((tags$i("Designed by Anna Huynh"))),
                   tags$h5((tags$i("Feb 09, 2021"))),
                   tags$h6("Data source: Yahoo Finance")
            )
     )),
  theme = bs_theme(
    bg = "#002B36", fg = "#EEE8D5", primary = "#2AA198", 
    base_font = font_google("Pacifico")
    ),
    tabsetPanel(
      type = "pills",
      tabPanel("Chart Series", plotOutput("distPlot1")),
      tabPanel("Scatter Plot", plotOutput("distplot2")),
      tabPanel("Line Plot", plotOutput("pricePlot"))
    )
)
## Warning: package 'quantmod' was built under R version 4.0.3
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## Warning: package 'tidyverse' was built under R version 4.0.3
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.4     v dplyr   1.0.2
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.1
## Warning: package 'ggplot2' was built under R version 4.0.3
## Warning: package 'tibble' was built under R version 4.0.3
## Warning: package 'stringr' was built under R version 4.0.3
## Warning: package 'forcats' was built under R version 4.0.3
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::first()  masks xts::first()
## x dplyr::lag()    masks stats::lag()
## x dplyr::last()   masks xts::last()
## 
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
## 
##     discard
## The following object is masked from 'package:readr':
## 
##     col_factor
## Warning: package 'Cairo' was built under R version 4.0.3
## 
## Attaching package: 'glue'
## The following object is masked from 'package:dplyr':
## 
##     collapse
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union

Define server logic required to draw a chart

shinyServer <- function(input, output) {

    output$distPlot1 <- renderPlot({
        
        # Use just the last 100 values for illustration
        if(input$chart == "TSLA") {
            symb <- "TSLA"
            tsla <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 100)
            chartSeries(tsla,
                                  theme = chartTheme("white", 
                                                     bg.col = 'grey96', 
                                                     fg.col = "grey70",
                                                     grid.col = "white", 
                                                     border = "transparent"), 
                                  up.col = 'darkblue', dn.col = 'tomato', 
                                  log.scale = TRUE,
                                  name = "The Tesla stock bubble of 2021 ($TSLA)")
            annotation_lab <- "Highest plausible actual value based on firm's revenue, etc.: $100"
            annotation_col <- "steelblue"
            
            abline(32, 0, col = annotation_col)
            text(100, 40, annotation_lab, col = annotation_col, cex = 0.8)
            

        } else if(input$chart == "GOOGL") {
            symb <- "GOOGL"
            googl <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 100)
            chartSeries(googl,
                                  theme = chartTheme("white", 
                                                     bg.col = 'grey96', 
                                                     fg.col = "grey70",
                                                     grid.col = "white", 
                                                     border = "transparent"), 
                                  up.col = 'darkblue', dn.col = 'tomato', 
                                  log.scale = TRUE,
                                  name = "The Google stock bubble of 2021 ($GOOGL)")
            annotation_lab <- "Highest plausible actual value based on firm's revenue, etc.: $100"
            annotation_col <- "steelblue"
            
            abline(32, 0, col = annotation_col)
            text(100, 40, annotation_lab, col = annotation_col, cex = 0.8)
            

        } else if(input$chart == "FB") {
            symb <- "FB"
            fb <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 100)
            chartSeries(fb,
                                  theme = chartTheme("white", 
                                                     bg.col = 'grey96', 
                                                     fg.col = "grey70",
                                                     grid.col = "white", 
                                                     border = "transparent"), 
                                  up.col = 'darkblue', dn.col = 'tomato', 
                                  log.scale = TRUE,
                                  name = "The Facebook stock bubble of 2021 ($FB)")
            annotation_lab <- "Highest plausible actual value based on firm's revenue, etc.: $100"
            annotation_col <- "steelblue"
            
            abline(32, 0, col = annotation_col)
            text(100, 40, annotation_lab, col = annotation_col, cex = 0.8)
            

        } else if(input$chart == "AMZN") {
            symb <- "AMZN"
            amzn <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 100)
            chartSeries(amzn,
                                  theme = chartTheme("white", 
                                                     bg.col = 'grey96', 
                                                     fg.col = "grey70",
                                                     grid.col = "white", 
                                                     border = "transparent"), 
                                  up.col = 'darkblue', dn.col = 'tomato', 
                                  log.scale = TRUE,
                                  name = "The Amazon stock bubble of 2021 ($AMZN)")
            annotation_lab <- "Highest plausible actual value based on firm's revenue, etc.: $100"
            annotation_col <- "steelblue"
            
            abline(32, 0, col = annotation_col)
            text(100, 40, annotation_lab, col = annotation_col, cex = 0.8)
            

        } else if(input$chart == "AAPL") {
            symb <- "AAPL"
            aapl <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 100)
            chartSeries(aapl,
                                  theme = chartTheme("white", 
                                                     bg.col = 'grey96', 
                                                     fg.col = "grey70",
                                                     grid.col = "white", 
                                                     border = "transparent"), 
                                  up.col = 'darkblue', dn.col = 'tomato', 
                                  log.scale = TRUE,
                                  name = "The Apple stock bubble of 2021 ($AAPL)")
            annotation_lab <- "Highest plausible actual value based on firm's revenue, etc.: $100"
            annotation_col <- "steelblue"
            
            abline(32, 0, col = annotation_col)
            text(100, 40, annotation_lab, col = annotation_col, cex = 0.8)
            

        } else {
            symb <- "MSFT"
            msft <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 100)
            chartSeries(msft,
                                  theme = chartTheme("white", 
                                                     bg.col = 'grey96', 
                                                     fg.col = "grey70",
                                                     grid.col = "white", 
                                                     border = "transparent"), 
                                  up.col = 'darkblue', dn.col = 'tomato', 
                                  log.scale = TRUE,
                                  name = "The Microsoft stock bubble of 2021 ($MSFT)")
            annotation_lab <- "Highest plausible actual value based on firm's revenue, etc.: $100"
            annotation_col <- "steelblue"
            
            abline(32, 0, col = annotation_col)
            text(100, 40, annotation_lab, col = annotation_col, cex = 0.8)
            
        }
   })

   output$distplot2 <- renderPlot({
    
    # Use just the last 100 values for illustration
    if(input$chart == "TSLA") {
        symb <- "TSLA"
        tsla <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 50)
        names(tsla) <- gsub(glue("{symb}\\."), "", names(tsla))

        tsla %>%
            as_tibble() %>%
            mutate(date = time(tsla)) %>%
            group_by(month(date), year(date)) %>%
            mutate(direction = ifelse(Close > Open, "Bullish", "Bearish"),
                   lab = ifelse(day(date) == min(day(date)) | Volume > 100e6 | Close > 300, 
                                format(date, "%e %b %y"), "")) %>%
            ungroup() %>%
            ggplot(aes(x = Volume / 1e6, y = Close, label = lab)) +
            geom_path(colour = "grey") +
            geom_point(aes(colour = direction), alpha = 0.5) +
            geom_text(aes(colour = direction), hjust = 0, size = 3, nudge_x = 0.015) +
            scale_x_log10(label = comma_format(suffix = "m", accuracy = 1),
                          limits = c(1, 300)) +
            scale_y_log10(label = dollar_format(accuracy = 1)) +
            scale_colour_manual(values = c(Bullish = "darkblue", Bearish = "tomato")) +
            labs(x = "Volume (log scale)", 
                 y = "Closing price (log scale)",
                 title = "The Tesla stock bubble of 2021 ($TSLA)",
                 colour = "Direction of sentiment over the day:",
                 subtitle = "Connected scatter plot of latest 50 closing price.")
        
      
    } else if(input$chart == "GOOGL") {
        symb <- "GOOGL"
        googl <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 50)
        names(googl) <- gsub(glue("{symb}\\."), "", names(googl))

        googl %>%
            as_tibble() %>%
            mutate(date = time(googl)) %>%
            group_by(month(date), year(date)) %>%
            mutate(direction = ifelse(Close > Open, "Bullish", "Bearish"),
                   lab = ifelse(day(date) == min(day(date)) | Volume > 100e6 | Close > 1000, 
                                format(date, "%e %b %y"), "")) %>%
            ungroup() %>%
            ggplot(aes(x = Volume / 1e6, y = Close, label = lab)) +
            geom_path(colour = "grey") +
            geom_point(aes(colour = direction), alpha = 0.5) +
            geom_text(aes(colour = direction), hjust = 0, size = 3, nudge_x = 0.015) +
            scale_x_log10(label = comma_format(suffix = "m", accuracy = 1),
                          limits = c(1, 300)) +
            scale_y_log10(label = dollar_format(accuracy = 1)) +
            scale_colour_manual(values = c(Bullish = "darkblue", Bearish = "tomato")) +
            labs(x = "Volume (log scale)", 
                 y = "Closing price (log scale)",
                 title = "The Google stock bubble of 2021 ($GOOGL)",
                 colour = "Direction of sentiment over the day:",
                 subtitle = "Connected scatter plot of latest 50 closing price.") 
        
    
    } else if(input$chart == "FB") {
        symb <- "FB"
        fb <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 50)
        names(fb) <- gsub(glue("{symb}\\."), "", names(fb))
        
        fb %>%
            as_tibble() %>%
            mutate(date = time(fb)) %>%
            group_by(month(date), year(date)) %>%
            mutate(direction = ifelse(Close > Open, "Bullish", "Bearish"),
                   lab = ifelse(day(date) == min(day(date)) | Volume > 100e6 | Close > 200, 
                                format(date, "%e %b %y"), "")) %>%
            ungroup() %>%
            ggplot(aes(x = Volume / 1e6, y = Close, label = lab)) +
            geom_path(colour = "grey") +
            geom_point(aes(colour = direction), alpha = 0.5) +
            geom_text(aes(colour = direction), hjust = 0, size = 3, nudge_x = 0.015) +
            scale_x_log10(label = comma_format(suffix = "m", accuracy = 1),
                          limits = c(1, 300)) +
            scale_y_log10(label = dollar_format(accuracy = 1)) +
            scale_colour_manual(values = c(Bullish = "darkblue", Bearish = "tomato")) +
            labs(x = "Volume (log scale)", 
                 y = "Closing price (log scale)",
                 title = "The Facebook stock bubble of 2021 ($FB)",
                 colour = "Direction of sentiment over the day:",
                 subtitle = "Connected scatter plot of latest 50 closing price.") 
        
        
    } else if(input$chart == "AMZN" ) {
        symb <- "AMZN"
        amzn <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 50)
        names(amzn) <- gsub(glue("{symb}\\."), "", names(amzn))

        amzn %>%
            as_tibble() %>%
            mutate(date = time(amzn)) %>%
            group_by(month(date), year(date)) %>%
            mutate(direction = ifelse(Close > Open, "Bullish", "Bearish"),
                   lab = ifelse(day(date) == min(day(date)) | Volume > 100e6 | Close > 2000, 
                                format(date, "%e %b %y"), "")) %>%
            ungroup() %>%
            ggplot(aes(x = Volume / 1e6, y = Close, label = lab)) +
            geom_path(colour = "grey") +
            geom_point(aes(colour = direction), alpha = 0.5) +
            geom_text(aes(colour = direction), hjust = 0, size = 3, nudge_x = 0.015) +
            scale_x_log10(label = comma_format(suffix = "m", accuracy = 1),
                          limits = c(1, 300)) +
            scale_y_log10(label = dollar_format(accuracy = 1)) +
            scale_colour_manual(values = c(Bullish = "darkblue", Bearish = "tomato")) +
            labs(x = "Volume (log scale)", 
                 y = "Closing price (log scale)",
                 title = "The Amazon stock bubble of 2021 ($AMZN)",
                 colour = "Direction of sentiment over the day:",
                 subtitle = "Connected scatter plot of latest 50 closing price.") 
        
        
    } else if(input$chart == "AAPL" ) {
        symb <- "AAPL"
        aapl <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 50)
        names(aapl) <- gsub(glue("{symb}\\."), "", names(aapl))

        aapl %>%
            as_tibble() %>%
            mutate(date = time(aapl)) %>%
            group_by(month(date), year(date)) %>%
            mutate(direction = ifelse(Close > Open, "Bullish", "Bearish"),
                   lab = ifelse(day(date) == min(day(date)) | Volume > 100e6 | Close > 100, 
                                format(date, "%e %b %y"), "")) %>%
            ungroup() %>%
            ggplot(aes(x = Volume / 1e6, y = Close, label = lab)) +
            geom_path(colour = "grey") +
            geom_point(aes(colour = direction), alpha = 0.5) +
            geom_text(aes(colour = direction), hjust = 0, size = 3, nudge_x = 0.015) +
            scale_x_log10(label = comma_format(suffix = "m", accuracy = 1),
                          limits = c(1, 300)) +
            scale_y_log10(label = dollar_format(accuracy = 1)) +
            scale_colour_manual(values = c(Bullish = "darkblue", Bearish = "tomato")) +
            labs(x = "Volume (log scale)", 
                 y = "Closing price (log scale)",
                 title = "The Apple stock bubble of 2021 ($AAPL)",
                 colour = "Direction of sentiment over the day:",
                 subtitle = "Connected scatter plot of latest 50 closing price.") 
        
        
    } else if(input$chart == "MSFT" ) {
        symb <- "MSFT"
        msft <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 50)
        names(msft) <- gsub(glue("{symb}\\."), "", names(msft))

        msft %>%
            as_tibble() %>%
            mutate(date = time(msft)) %>%
            group_by(month(date), year(date)) %>%
            mutate(direction = ifelse(Close > Open, "Bullish", "Bearish"),
                   lab = ifelse(day(date) == min(day(date)) | Volume > 100e6 | Close > 200, 
                                format(date, "%e %b %y"), "")) %>%
            ungroup() %>%
            ggplot(aes(x = Volume / 1e6, y = Close, label = lab)) +
            geom_path(colour = "grey") +
            geom_point(aes(colour = direction), alpha = 0.5) +
            geom_text(aes(colour = direction), hjust = 0, size = 3, nudge_x = 0.015) +
            scale_x_log10(label = comma_format(suffix = "m", accuracy = 1),
                          limits = c(1, 300)) +
            scale_y_log10(label = dollar_format(accuracy = 1)) +
            scale_colour_manual(values = c(Bullish = "darkblue", Bearish = "tomato")) +
            labs(x = "Volume (log scale)", 
                 y = "Closing price (log scale)",
                 title = "The Microsoft stock bubble of 2021 ($MSFT)",
                 colour = "Direction of sentiment over the day:",
                 subtitle = "Connected scatter plot.") 
        
    }
})

  output$pricePlot <- renderPlot({
       
    if(input$chart == "TSLA" & input$PriceInput == "over $100") {
        symb <- "TSLA"
        tsla <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 100)
        names(tsla) <- gsub(glue("{symb}\\."), "", names(tsla))
        p <- tsla[which(tsla$Close >= 100)]
        
        annotation_lab <- "Highest plausible actual value based on firm's revenue, etc.: $100"
        annotation_col <- "steelblue"

        p %>%
            as_tibble() %>%
            mutate(date = time(p)) %>%
            ggplot(aes(x = date, y = Close)) +
            geom_hline(yintercept = 20, colour = "steelblue") +
            geom_line() +
            geom_point(aes(size = Volume / 1e6), alpha = 0.5) +
            annotate("text", x = Sys.Date() - 50, y = 32,  size = 3, hjust = 1, 
                     colour = annotation_col,
                     label = annotation_lab) +
            scale_y_log10(label = dollar_format(accuracy = 1)) +
            scale_x_date(date_labels = "%b %Y") +
            scale_size_area(label = comma) +
            labs(x = "",
                 y = "Closing price (log scale)",
                 size = "Volume (M)",
                 title = "The Tesla stock bubble of 2021 ($TSLA)",
                 subtitle = "Line plot for price with volume shown as point size.")
        
        } else if(input$chart == "TSLA" & input$PriceInput == "over $300") {
            
            symb <- "TSLA"
            tsla <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 100)
            names(tsla) <- gsub(glue("{symb}\\."), "", names(tsla))
            p <- tsla[which(tsla$Close >= 300)]
            
            annotation_lab <- "Highest plausible actual value based on firm's revenue, etc.: $100"
            annotation_col <- "steelblue"

            p %>%
                as_tibble() %>%
                mutate(date = time(p)) %>%
                ggplot(aes(x = date, y = Close)) +
                geom_hline(yintercept = 20, colour = "steelblue") +
                geom_line() +
                geom_point(aes(size = Volume / 1e6), alpha = 0.5) +
                annotate("text", x = Sys.Date() - 50, y = 32,  size = 3, hjust = 1, 
                         colour = annotation_col,
                         label = annotation_lab) +
                scale_y_log10(label = dollar_format(accuracy = 1)) +
                scale_x_date(date_labels = "%b %Y") +
                scale_size_area(label = comma) +
                labs(x = "",
                     y = "Closing price (log scale)",
                     size = "Volume (M)",
                     title = "The Tesla stock bubble of 2021 ($TSLA)",
                     subtitle = "Line plot for price with volume shown as point size.")
            
        } else if(input$chart == "TSLA" & input$PriceInput == "over $500") {
            symb <- "TSLA"
            tsla <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 100)
            names(tsla) <- gsub(glue("{symb}\\."), "", names(tsla))
            p <- tsla[which(tsla$Close >= 500)]
            
            annotation_lab <- "Highest plausible actual value based on firm's revenue, etc.: $100"
            annotation_col <- "steelblue"

            p %>%
                as_tibble() %>%
                mutate(date = time(p)) %>%
                ggplot(aes(x = date, y = Close)) +
                geom_hline(yintercept = 20, colour = "steelblue") +
                geom_line() +
                geom_point(aes(size = Volume / 1e6), alpha = 0.5) +
                annotate("text", x = Sys.Date() - 50, y = 32,  size = 3, hjust = 1, 
                         colour = annotation_col,
                         label = annotation_lab) +
                scale_y_log10(label = dollar_format(accuracy = 1)) +
                scale_x_date(date_labels = "%b %Y") +
                scale_size_area(label = comma) +
                labs(x = "",
                     y = "Closing price (log scale)",
                     size = "Volume (M)",
                     title = "The Tesla stock bubble of 2021 ($TSLA)",
                     subtitle = "Line plot for price with volume shown as point size.")
        
        } else if(input$chart == "GOOGL" & input$PriceInput == "over $100") {
            symb <- "GOOGL"
            googl <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 100)
            names(googl) <- gsub(glue("{symb}\\."), "", names(googl))
            p <- googl[which(googl$Close >= 100)]
            
            annotation_lab <- "Highest plausible actual value based on firm's revenue, etc.: $100"
            annotation_col <- "steelblue"

            p %>%
                as_tibble() %>%
                mutate(date = time(p)) %>%
                ggplot(aes(x = date, y = Close)) +
                geom_hline(yintercept = 20, colour = "steelblue") +
                geom_line() +
                geom_point(aes(size = Volume / 1e6), alpha = 0.5) +
                annotate("text", x = Sys.Date() - 50, y = 32,  size = 3, hjust = 1, 
                         colour = annotation_col,
                         label = annotation_lab) +
                scale_y_log10(label = dollar_format(accuracy = 1)) +
                scale_x_date(date_labels = "%b %Y") +
                scale_size_area(label = comma) +
                labs(x = "",
                     y = "Closing price (log scale)",
                     size = "Volume (M)",
                     title = "The Google stock bubble of 2021 ($GOOGL)",
                     subtitle = "Line plot for price with volume shown as point size.")
            
        } else if(input$chart == "GOOGL" & input$PriceInput == "over $300") {
            symb <- "GOOGL"
            googl <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 100)
            names(googl) <- gsub(glue("{symb}\\."), "", names(googl))
            p <- googl[which(googl$Close >= 300)]
            
            annotation_lab <- "Highest plausible actual value based on firm's revenue, etc.: $100"
            annotation_col <- "steelblue"

            p %>%
                as_tibble() %>%
                mutate(date = time(p)) %>%
                ggplot(aes(x = date, y = Close)) +
                geom_hline(yintercept = 20, colour = "steelblue") +
                geom_line() +
                geom_point(aes(size = Volume / 1e6), alpha = 0.5) +
                annotate("text", x = Sys.Date() - 50, y = 32,  size = 3, hjust = 1, 
                         colour = annotation_col,
                         label = annotation_lab) +
                scale_y_log10(label = dollar_format(accuracy = 1)) +
                scale_x_date(date_labels = "%b %Y") +
                scale_size_area(label = comma) +
                labs(x = "",
                     y = "Closing price (log scale)",
                     size = "Volume (M)",
                     title = "The Google stock bubble of 2021 ($GOOGL)",
                     subtitle = "Line plot for price with volume shown as point size.")
            
        } else if(input$chart == "GOOGL" & input$PriceInput == "over $500") {
            symb <- "GOOGL"
            googl <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 100)
            names(googl) <- gsub(glue("{symb}\\."), "", names(googl))
            p <- googl[which(googl$Close >= 500)]
            
            annotation_lab <- "Highest plausible actual value based on firm's revenue, etc.: $100"
            annotation_col <- "steelblue"

            p %>%
                as_tibble() %>%
                mutate(date = time(p)) %>%
                ggplot(aes(x = date, y = Close)) +
                geom_hline(yintercept = 20, colour = "steelblue") +
                geom_line() +
                geom_point(aes(size = Volume / 1e6), alpha = 0.5) +
                annotate("text", x = Sys.Date() - 50, y = 32,  size = 3, hjust = 1, 
                         colour = annotation_col,
                         label = annotation_lab) +
                scale_y_log10(label = dollar_format(accuracy = 1)) +
                scale_x_date(date_labels = "%b %Y") +
                scale_size_area(label = comma) +
                labs(x = "",
                     y = "Closing price (log scale)",
                     size = "Volume (M)",
                     title = "The Google stock bubble of 2021 ($GOOGL)",
                     subtitle = "Line plot for price with volume shown as point size.")
            
        } else if(input$chart == "FB" & input$PriceInput == "over $100") {
            symb <- "FB"
            fb <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 100)
            names(fb) <- gsub(glue("{symb}\\."), "", names(fb))
            p <- fb[which(fb$Close >= 100)]
            
            annotation_lab <- "Highest plausible actual value based on firm's revenue, etc.: $100"
            annotation_col <- "steelblue"

            p %>%
                as_tibble() %>%
                mutate(date = time(p)) %>%
                ggplot(aes(x = date, y = Close)) +
                geom_hline(yintercept = 20, colour = "steelblue") +
                geom_line() +
                geom_point(aes(size = Volume / 1e6), alpha = 0.5) +
                annotate("text", x = Sys.Date() - 50, y = 32,  size = 3, hjust = 1, 
                         colour = annotation_col,
                         label = annotation_lab) +
                scale_y_log10(label = dollar_format(accuracy = 1)) +
                scale_x_date(date_labels = "%b %Y") +
                scale_size_area(label = comma) +
                labs(x = "",
                     y = "Closing price (log scale)",
                     size = "Volume (M)",
                     title = "The Facebook stock bubble of 2021 ($FB)",
                     subtitle = "Line plot for price with volume shown as point size.")
            
        } else if(input$chart == "FB" & input$PriceInput == "over $300") {
            symb <- "FB"
            fb <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 100)
            names(fb) <- gsub(glue("{symb}\\."), "", names(fb))
            p <- fb[which(fb$Close >= 300)]
            
            annotation_lab <- "Highest plausible actual value based on firm's revenue, etc.: $100"
            annotation_col <- "steelblue"

            p %>%
                as_tibble() %>%
                mutate(date = time(p)) %>%
                ggplot(aes(x = date, y = Close)) +
                geom_hline(yintercept = 20, colour = "steelblue") +
                geom_line() +
                geom_point(aes(size = Volume / 1e6), alpha = 0.5) +
                annotate("text", x = Sys.Date() - 50, y = 32,  size = 3, hjust = 1, 
                         colour = annotation_col,
                         label = annotation_lab) +
                scale_y_log10(label = dollar_format(accuracy = 1)) +
                scale_x_date(date_labels = "%b %Y") +
                scale_size_area(label = comma) +
                labs(x = "",
                     y = "Closing price (log scale)",
                     size = "Volume (M)",
                     title = "The Facebook stock bubble of 2021 ($FB)",
                     subtitle = "Line plot for price with volume shown as point size.")
            
        } else if(input$chart == "FB" & input$PriceInput == "over $500") {
            symb <- "FB"
            fb <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 100)
            names(fb) <- gsub(glue("{symb}\\."), "", names(fb))
            p <- fb[which(fb$Close >= 500)]
            
            annotation_lab <- "Highest plausible actual value based on firm's revenue, etc.: $100"
            annotation_col <- "steelblue"

            p %>%
                as_tibble() %>%
                mutate(date = time(p)) %>%
                ggplot(aes(x = date, y = Close)) +
                geom_hline(yintercept = 20, colour = "steelblue") +
                geom_line() +
                geom_point(aes(size = Volume / 1e6), alpha = 0.5) +
                annotate("text", x = Sys.Date() - 50, y = 32,  size = 3, hjust = 1, 
                         colour = annotation_col,
                         label = annotation_lab) +
                scale_y_log10(label = dollar_format(accuracy = 1)) +
                scale_x_date(date_labels = "%b %Y") +
                scale_size_area(label = comma) +
                labs(x = "",
                     y = "Closing price (log scale)",
                     size = "Volume (M)",
                     title = "The Facebook stock bubble of 2021 ($FB)",
                     subtitle = "Line plot for price with volume shown as point size.")
            
        } else if(input$chart == "AMZN" & input$PriceInput == "over $100") {
            symb <- "AMZN"
            amzn <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 100)
            names(amzn) <- gsub(glue("{symb}\\."), "", names(amzn))
            p <- amzn[which(amzn$Close >= 100)]
            
            annotation_lab <- "Highest plausible actual value based on firm's revenue, etc.: $100"
            annotation_col <- "steelblue"

            p %>%
                as_tibble() %>%
                mutate(date = time(p)) %>%
                ggplot(aes(x = date, y = Close)) +
                geom_hline(yintercept = 20, colour = "steelblue") +
                geom_line() +
                geom_point(aes(size = Volume / 1e6), alpha = 0.5) +
                annotate("text", x = Sys.Date() - 50, y = 32,  size = 3, hjust = 1, 
                         colour = annotation_col,
                         label = annotation_lab) +
                scale_y_log10(label = dollar_format(accuracy = 1)) +
                scale_x_date(date_labels = "%b %Y") +
                scale_size_area(label = comma) +
                labs(x = "",
                     y = "Closing price (log scale)",
                     size = "Volume (M)",
                     title = "The Amazon stock bubble of 2021 ($AMZN)",
                     subtitle = "Line plot for price with volume shown as point size.")
            
        } else if(input$chart == "AMZN" & input$PriceInput == "over $300") {
            symb <- "AMZN"
            amzn <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 100)
            names(amzn) <- gsub(glue("{symb}\\."), "", names(amzn))
            p <- amzn[which(amzn$Close >= 300)]
            
            annotation_lab <- "Highest plausible actual value based on firm's revenue, etc.: $100"
            annotation_col <- "steelblue"

            p %>%
                as_tibble() %>%
                mutate(date = time(p)) %>%
                ggplot(aes(x = date, y = Close)) +
                geom_hline(yintercept = 20, colour = "steelblue") +
                geom_line() +
                geom_point(aes(size = Volume / 1e6), alpha = 0.5) +
                annotate("text", x = Sys.Date() - 50, y = 32,  size = 3, hjust = 1, 
                         colour = annotation_col,
                         label = annotation_lab) +
                scale_y_log10(label = dollar_format(accuracy = 1)) +
                scale_x_date(date_labels = "%b %Y") +
                scale_size_area(label = comma) +
                labs(x = "",
                     y = "Closing price (log scale)",
                     size = "Volume (M)",
                     title = "The Amazon stock bubble of 2021 ($AMZN)",
                     subtitle = "Line plot for price with volume shown as point size.")
            
        } else if(input$chart == "AMZN" & input$PriceInput == "over $500") {
            symb <- "AMZN"
            amzn <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 100)
            names(amzn) <- gsub(glue("{symb}\\."), "", names(amzn))
            p <- amzn[which(amzn$Close >= 500)]
            
            annotation_lab <- "Highest plausible actual value based on firm's revenue, etc.: $100"
            annotation_col <- "steelblue"

            p %>%
                as_tibble() %>%
                mutate(date = time(p)) %>%
                ggplot(aes(x = date, y = Close)) +
                geom_hline(yintercept = 20, colour = "steelblue") +
                geom_line() +
                geom_point(aes(size = Volume / 1e6), alpha = 0.5) +
                annotate("text", x = Sys.Date() - 50, y = 32,  size = 3, hjust = 1, 
                         colour = annotation_col,
                         label = annotation_lab) +
                scale_y_log10(label = dollar_format(accuracy = 1)) +
                scale_x_date(date_labels = "%b %Y") +
                scale_size_area(label = comma) +
                labs(x = "",
                     y = "Closing price (log scale)",
                     size = "Volume (M)",
                     title = "The Amazon stock bubble of 2021 ($AMZN)",
                     subtitle = "Line plot for price with volume shown as point size.")
            
        } else if(input$chart == "AAPL" & input$PriceInput == "over $100") {
            symb <- "AAPL"
            aapl <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 100)
            names(aapl) <- gsub(glue("{symb}\\."), "", names(aapl))
            p <- aapl[which(aapl$Close >= 100)]
            
            annotation_lab <- "Highest plausible actual value based on firm's revenue, etc.: $100"
            annotation_col <- "steelblue"

            p %>%
                as_tibble() %>%
                mutate(date = time(p)) %>%
                ggplot(aes(x = date, y = Close)) +
                geom_hline(yintercept = 20, colour = "steelblue") +
                geom_line() +
                geom_point(aes(size = Volume / 1e6), alpha = 0.5) +
                annotate("text", x = Sys.Date() - 50, y = 32,  size = 3, hjust = 1, 
                         colour = annotation_col,
                         label = annotation_lab) +
                scale_y_log10(label = dollar_format(accuracy = 1)) +
                scale_x_date(date_labels = "%b %Y") +
                scale_size_area(label = comma) +
                labs(x = "",
                     y = "Closing price (log scale)",
                     size = "Volume (M)",
                     title = "The Apple stock bubble of 2021 ($AAPL)",
                     subtitle = "Line plot for price with volume shown as point size.")
            
        } else if(input$chart == "AAPL" & input$PriceInput == "over $300") {
            symb <- "AAPL"
            aapl <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 100)
            names(aapl) <- gsub(glue("{symb}\\."), "", names(aapl))
            p <- aapl[which(aapl$Close >= 300)]
            
            annotation_lab <- "Highest plausible actual value based on firm's revenue, etc.: $100"
            annotation_col <- "steelblue"

            p %>%
                as_tibble() %>%
                mutate(date = time(p)) %>%
                ggplot(aes(x = date, y = Close)) +
                geom_hline(yintercept = 20, colour = "steelblue") +
                geom_line() +
                geom_point(aes(size = Volume / 1e6), alpha = 0.5) +
                annotate("text", x = Sys.Date() - 50, y = 32,  size = 3, hjust = 1, 
                         colour = annotation_col,
                         label = annotation_lab) +
                scale_y_log10(label = dollar_format(accuracy = 1)) +
                scale_x_date(date_labels = "%b %Y") +
                scale_size_area(label = comma) +
                labs(x = "",
                     y = "Closing price (log scale)",
                     size = "Volume (M)",
                     title = "The Apple stock bubble of 2021 ($AAPL)",
                     subtitle = "Line plot for price with volume shown as point size.")
            
        } else if(input$chart == "AAPL" & input$PriceInput == "over $500") {
            symb <- "AAPL"
            aapl <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 100)
            names(aapl) <- gsub(glue("{symb}\\."), "", names(aapl))
            p <- aapl[which(aapl$Close >= 500)]
            
            annotation_lab <- "Highest plausible actual value based on firm's revenue, etc.: $100"
            annotation_col <- "steelblue"

            p %>%
                as_tibble() %>%
                mutate(date = time(p)) %>%
                ggplot(aes(x = date, y = Close)) +
                geom_hline(yintercept = 20, colour = "steelblue") +
                geom_line() +
                geom_point(aes(size = Volume / 1e6), alpha = 0.5) +
                annotate("text", x = Sys.Date() - 50, y = 32,  size = 3, hjust = 1, 
                         colour = annotation_col,
                         label = annotation_lab) +
                scale_y_log10(label = dollar_format(accuracy = 1)) +
                scale_x_date(date_labels = "%b %Y") +
                scale_size_area(label = comma) +
                labs(x = "",
                     y = "Closing price (log scale)",
                     size = "Volume (M)",
                     title = "The Apple stock bubble of 2021 ($AAPL)",
                     subtitle = "Line plot for price with volume shown as point size.")
            
        } else if(input$chart == "MSFT" & input$PriceInput == "over $100") {
            symb <- "MSFT"
            msft <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 100)
            names(msft) <- gsub(glue("{symb}\\."), "", names(msft))
            p <- msft[which(msft$Close >= 100)]
            
            annotation_lab <- "Highest plausible actual value based on firm's revenue, etc.: $100"
            annotation_col <- "steelblue"

            p %>%
                as_tibble() %>%
                mutate(date = time(p)) %>%
                ggplot(aes(x = date, y = Close)) +
                geom_hline(yintercept = 20, colour = "steelblue") +
                geom_line() +
                geom_point(aes(size = Volume / 1e6), alpha = 0.5) +
                annotate("text", x = Sys.Date() - 50, y = 32,  size = 3, hjust = 1, 
                         colour = annotation_col,
                         label = annotation_lab) +
                scale_y_log10(label = dollar_format(accuracy = 1)) +
                scale_x_date(date_labels = "%b %Y") +
                scale_size_area(label = comma) +
                labs(x = "",
                     y = "Closing price (log scale)",
                     size = "Volume (M)",
                     title = "The Microsoft stock bubble of 2021 ($MSFT)",
                     subtitle = "Line plot for price with volume shown as point size.")
            
        } else if(input$chart == "MSFT" & input$PriceInput == "over $300") {
            symb <- "MSFT"
            msft <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 100)
            names(msft) <- gsub(glue("{symb}\\."), "", names(msft))
            p <- msft[which(msft$Close >= 300)]
            
            annotation_lab <- "Highest plausible actual value based on firm's revenue, etc.: $100"
            annotation_col <- "steelblue"

            p %>%
                as_tibble() %>%
                mutate(date = time(p)) %>%
                ggplot(aes(x = date, y = Close)) +
                geom_hline(yintercept = 20, colour = "steelblue") +
                geom_line() +
                geom_point(aes(size = Volume / 1e6), alpha = 0.5) +
                annotate("text", x = Sys.Date() - 50, y = 32,  size = 3, hjust = 1, 
                         colour = annotation_col,
                         label = annotation_lab) +
                scale_y_log10(label = dollar_format(accuracy = 1)) +
                scale_x_date(date_labels = "%b %Y") +
                scale_size_area(label = comma) +
                labs(x = "",
                     y = "Closing price (log scale)",
                     size = "Volume (M)",
                     title = "The Microsoft stock bubble of 2021 ($MSFT)",
                     subtitle = "Line plot for price with volume shown as point size.")
            
        } else if(input$chart == "MSFT" & input$PriceInput == "over $500") {
            symb <- "MSFT"
            msft <- tail(getSymbols(symb, src = 'yahoo', auto.assign = FALSE), 100)
            names(msft) <- gsub(glue("{symb}\\."), "", names(msft))
            p <- msft[which(msft$Close >= 500)]
            
            annotation_lab <- "Highest plausible actual value based on firm's revenue, etc.: $100"
            annotation_col <- "steelblue"

            p %>%
                as_tibble() %>%
                mutate(date = time(p)) %>%
                ggplot(aes(x = date, y = Close)) +
                geom_hline(yintercept = 20, colour = "steelblue") +
                geom_line() +
                geom_point(aes(size = Volume / 1e6), alpha = 0.5) +
                annotate("text", x = Sys.Date() - 50, y = 32,  size = 3, hjust = 1, 
                         colour = annotation_col,
                         label = annotation_lab) +
                scale_y_log10(label = dollar_format(accuracy = 1)) +
                scale_x_date(date_labels = "%b %Y") +
                scale_size_area(label = comma) +
                labs(x = "",
                     y = "Closing price (log scale)",
                     size = "Volume (M)",
                     title = "The Microsoft stock bubble of 2021 ($MSFT)",
                     subtitle = "Line plot for price with volume shown as point size.")
        }
  })
}