Welcome to stockVis, a Shiny app designed to help you explore and visualize historical stock prices interactively and efficiently. This is one of my personal projects, where I experimented with building a responsive and streamlined app using R’s powerful Shiny framework and the quantmod
package. It was an exciting experience to see how reactive programming principles could make data visualization apps both user-friendly and lightning-fast!
The stockVis app enables users to:
- Select a stock by typing in a ticker symbol (e.g., GOOG, AAPL, GS).
- Specify a date range for the data they want to review.
- Customize the y-axis by plotting either raw prices or the log of prices.
- Adjust prices for inflation using historical Consumer Price Index (CPI) data (feature to be implemented!).
The app relies on the quantmod
package to fetch and visualize stock price data, utilizing the getSymbols()
function to retrieve data from Yahoo Finance and chartSeries()
to create attractive line charts.
- Create a new folder named
stockVis
in your working directory. - Download the following files and place them inside the
stockVis
folder:app.R
helpers.R
- Launch the app using
runApp("stockVis")
in R.
Make sure you have the quantmod
package installed:
install.packages("quantmod")
- Date Range Selector: Pick the specific date range you want to analyze.
- Log Scale Option: Toggle the y-axis to display data on a logarithmic scale.
- Inflation Adjustment: (Coming soon) Adjust historical prices to reflect present-day values using CPI data.
- Default Ticker: The app starts with the SPY ticker, representing the S&P 500 index.
One of the key challenges I faced while developing stockVis was optimizing the app’s performance. Fetching stock data repeatedly can be slow and inefficient, especially when users make minor adjustments like switching to a log scale. To solve this, I used reactive expressions. These expressions cache data and only recompute when necessary, ensuring that the app remains fast and responsive.
dataInput <- reactive({
getSymbols(input$symb, src = "yahoo",
from = input$dates[1],
to = input$dates[2],
auto.assign = FALSE)
})
By using a reactive expression like dataInput()
, the app only fetches new data when the selected stock or date range changes. Adjustments to the chart, such as changing the y-axis scale, do not trigger unnecessary data fetching, making the app efficient and reliable.
- Fix Inflation Adjustment: The “Adjust prices for inflation” checkbox is not fully functional yet. I plan to implement a reactive expression to toggle between adjusted and non-adjusted data.
- Optimize Further: Continue improving the app’s responsiveness by fine-tuning the use of reactive expressions.
- R (version 3.5.0 or higher)
- Shiny (for building the web app)
- quantmod (for fetching financial data)
- xts, zoo (for handling time series data)
To run the app, simply execute:
runApp("stockVis")
Feel free to explore different stocks and play around with the app’s features. This project is an ongoing learning experience for me, and I’d love to hear any feedback or suggestions for improvement.
Enjoy visualizing stock data! 🚀