Skip to content

hhpfiona/Stock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

stockVis: A Personal Shiny App Project for Visualizing Stock Prices [November 2023]

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!

Project Overview

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.

How to Get Started

  1. Create a new folder named stockVis in your working directory.
  2. Download the following files and place them inside the stockVis folder:
    • app.R
    • helpers.R
  3. Launch the app using runApp("stockVis") in R.

Make sure you have the quantmod package installed:

install.packages("quantmod")

Features

  • 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.

Reactive Programming for Efficiency

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.

Example: Reactive Data Fetching

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.

To-Do List

  • 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.

Dependencies

  • 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)

Running the App

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! 🚀

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages