I'm building an equity watchlist, with the intention of being able to type in a ticker and add a new plot for that ticker without replacing the existing plot. As of now, there is only one plot that adjusts based on the ticker entered. I would theoretically like to be able to add as many or few tickers as I'd like, and be able to see the plots for those tickers all at once. Is this achievable? Below is my code. Thanks in advance!
library(shiny)
library(quantmod)
library(ggplot2)
library(tibble)
# Define UI ----
ui <- fluidPage(
titlePanel("CyCap Watchlist", "Watchlist"),
sidebarLayout(
sidebarPanel(
helpText("Input a stock symbol.
Data will be collected from Yahoo Finance."),
textInput("symb","Symbol", "SPY"),
dateRangeInput("dates","Date Range", start=Sys.Date()-365, end=Sys.Date()),
br(),
),
mainPanel("",
fluidrow(
row(plotOutput("plot")))
)
)
)
# Define server logic ----
server <- function(input, output) {
dataInput <- reactive({
getSymbols(
Symbols= input$symb,
src="yahoo",
from = input$dates[1],
to = input$dates[2],
auto.assign = FALSE
)
})
badf <- reactive({data.frame(dataInput())})
baclosedf <- reactive({
x <- subset(badf(), select=c(paste0(input$symb, ".Close")))
x <- rownames_to_column(x, "Date")
})
output$plot <- renderCachedPlot({
ggplot(
data=baclosedf(),
mapping=aes(x="", y=baclosedf()[, paste0(input$symb, ".Close")], group=1, size=5, height=1))+
geom_linerange(aes(ymin=min(baclosedf()[, paste0(input$symb, ".Close")]),ymax=tail(baclosedf()[, paste0(input$symb, ".Close")],n=1)),linetype="solid",color="red", size=3)+
geom_linerange(aes(ymin=tail(baclosedf()[, paste0(input$symb, ".Close")], n=1)),ymax=max(baclosedf()[, paste0(input$symb, ".Close")]),linetype="solid",color="forestgreen", size=3)+
geom_point(aes(y=min(baclosedf()[, paste0(input$symb, ".Close")])),size=3,color="black")+
geom_point(aes(y=max(baclosedf()[, paste0(input$symb, ".Close")])),size=3,color="black")+
geom_point(aes(y=tail(baclosedf()[, paste0(input$symb, ".Close")], n=1)),size=3,color="black")+
scale_y_continuous(breaks = c(min(baclosedf()[, paste0(input$symb, ".Close")]), tail(baclosedf()[, paste0(input$symb, ".Close")], n=1), max(baclosedf()[, paste0(input$symb, ".Close")])))+
coord_flip()+
labs(x="", y="", title=input$symb)+
theme_bw()+
theme(axis.ticks = element_blank())
},
cacheKeyExpr = { list(input$symb, input$dates) }
)
}
# Run the app ----
shinyApp(ui = ui, server = server)