0

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)

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
  • Would using `ggarrange` work by saving your plots in a list and then passing that to the function? [See this answer here](https://stackoverflow.com/questions/53048776/ggarrange-plot-all-plots-in-a-list) for something kind of similar. [Information on use of ggarrange is here](http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/81-ggplot2-easy-way-to-mix-multiple-graphs-on-the-same-page/). – chemdork123 May 05 '20 at 18:22
  • Do you mean you want to add a plot every time you click on the button, instead of updating the current one? That is possible, but requires a bit more work, easiest with `insertUI`. – Remko Duursma May 06 '20 at 12:50

1 Answers1

0

Have you come across this prior post on StackOverflow? Multiple Plots in R Shiny

See if you can modify that towards your needs.

Piranha
  • 116
  • 6