Using Shiny to Show Stock Data

Hello I am attempting to create a Shiny Dashboard that can show stock prices over time and update each day.

#Packages:
library(shiny)
library(shinydashboard)
library(plotly)
library(DT)
library(rsconnect)
library(tidyquant)

#Stock Collection API.
Stock_Table <- tq_get(c("AAPL","MSFT","META"),
from = "1900-01-01",
to = Sys.Date(),
get = "stock.prices")

#Creating the UI.
ui<-dashboardPage(skin="red",
dashboardHeader(title = "Tyler's Dashboard"),
dashboardSidebar(
sidebarMenu(
menuItem("Stock", tabName = "Stock", icon = icon("money-bill-alt"))
)
),
dashboardBody(
tabItems(
tabItem("Stock",
box(plotlyOutput("stock_plot"), width=10),
box(
dateInput("start_date", "Start Date:", value = "2012-02-29"),
dateInput("end_date", "End Date:", value = "2012-02-29"),
selectInput("measure", "Measure",
c("open", "high","low","close")),
selectInput("ticker", "Ticker",
c("AAPL","META","MSFT")),
width = 2)
)
),
)
)

#Creating the Server
server<- function(input, output){
output$stock_plot <- renderPlotly({
plot_ly(Stock_Table[which(Stock_Table$symbol==input$ticker),], x = ~Stock_Table$date, y = ~Stock_Table[[input$measure]], type = 'scatter', mode = 'lines')
})
}

shinyApp(ui, server)

Note that I am attempting to use a 'which' statement to restrict the data set within plotly to only the ticker of interest. Currently the 'passback' from the user input does not seem to be working. Also note that the following line of code works properly when run by itself:
plot_ly(Stock_Table[which(Stock_Table$symbol=="MSFT"),], x = ~date, y = ~close, type = 'scatter', mode = 'lines')

Any recommendations on how to get this to work?

Thanks for providing the reprex. I've played around with it and the issue seems to be in what you pass into plot_ly. I'm not an expert on plot_ly, so if I am wrong please someone correct me. It seems to be working now though.

The updated snippet updates the graph with every ticker input change. Note that I just shortened the which clause into 'data', and for the x and y axis I did not refer to the whole of 'Stock_Table' as we're only plotting a subset of it (which is in 'data'). And since we already pass 'data' into plot_ly, it is implicitly referred already.

#Creating the Server
server<- function(input, output){

        output$stock_plot <- renderPlotly({
                data <- Stock_Table[which(Stock_Table$symbol==input$ticker), ]
                plot_ly(data, x = ~date, y = ~data[[input$measure]], type = 'scatter', mode = 'lines')

    })
}

EDIT: I thought your issue was solely for the ticker selection, but for input date ranges, you may do the same type of pre-processing to the Stock_Table into 'data', by filtering out any dates before the start date, and dates after the end date. Feel free to post here if you need assistance with that.

1 Like

It is working. Thank you so much!!! I got the dates working as well. Once the logic for the tickers was resolved I knew the dates could just follow the same logic. Thanks again!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.