Historical Performance

Row

Apple

Microsoft

Twitter

Row

Performance Metrics

Future Outlook

Row

Future Outlook

Row

Earnings Per Share Next Year

Future Earnings Per Share

---
title: "Investment Dashboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: fill
    source_code: embed
---


```{r}
library(flexdashboard)
library(DT)
library(quantmod)
library(dygraphs)
library(ggplot2)
library(plotly)
library(ggthemes)
```

```{r}
# Download and pre-process the data for visualization

tickers <- c("AAPL", "TWTR", "MSFT")

#Get the data from Jan-1-2000 to Today
applURL <- "http://chart.finance.yahoo.com/table.csv?s=AAPL&a=0&b=1&c=2000&d=2&e=6&f=2017&g=d&ignore=.csv"
twtrURL <- "http://chart.finance.yahoo.com/table.csv?s=TWTR&a=0&b=1&c=2000&d=2&e=6&f=2017&g=d&ignore=.csv"
msftURL <- "http://chart.finance.yahoo.com/table.csv?s=MSFT&a=0&b=1&c=2000&d=2&e=6&f=2017&g=d&ignore=.csv"

#function to remove all the columns except date and closing price
removeUnwantedColumns <- function(data){
  temp <- data[,c(1,5)]
  temp$Date <- as.Date(as.character(temp$Date))
  return(temp)
}

#Download the data
applData <- read.table(applURL,header=TRUE,sep=",")
twtrData <- read.table(twtrURL,header=TRUE,sep=",")
msftData <- read.table(msftURL,header=TRUE,sep=",")

#Data for DT Tables using yahooQF

#Vector to hold name of attributes
stockAttributes <- c("Price/Sales", 
                "P/E Ratio",
                "Book Value",
                "Dividend/Share",
                "Earnings/Share",
                "52-week Low",
                "52-week High",
                "PEG Ratio",
                "Dividend Yield", 
                "Market Capitalization",
                "EPS Estimate Current Year",
                "EPS Estimate Next Year",
                "EPS Estimate Next Quarter",
                "EBITDA",
                "1 yr Target Price",
                "Price/EPS Estimate Next Year")

#Create a what metrics of data to be downloaded
whatMetrics <- yahooQF(stockAttributes)

# Get data as per whatMetrics from yahoo
stockData <- getQuote(paste(tickers, sep="", collapse=";"), what=whatMetrics)

#Add tickers as the first column and remove the first column which had date stamps
stockData <- data.frame(stockData[,2:length(stockData)]) 

#Change column names
colnames(stockData) <- stockAttributes

```

Historical Performance
=====================================  

Row {.tabset .tabset-fade data-height=650}
-------------------------------------

### Apple

```{r}

#Process data to create candle stick chart
applData$Date <- as.Date(as.character(applData$Date))
appleMatrix <- data.matrix(as.xts(applData, order.by = applData$Date))
appleMatrix <- appleMatrix[,-1]
appleMatrix <- appleMatrix[,(1:4)]
storage.mode(appleMatrix) <- "numeric"

#Plot candle stick graph
dygraph(appleMatrix, xlab = "Days", ylab="price ($)", main="Candlestick chart") %>%
dyCandlestick() %>%
dyRangeSelector()

```

### Microsoft 

```{r}

#Process data to create candle stick chart
msftData$Date <- as.Date(as.character(msftData$Date))
microsoftMatrix <- data.matrix(as.xts(msftData, order.by = msftData$Date))
microsoftMatrix <- microsoftMatrix[,-1]
microsoftMatrix <- microsoftMatrix[,(1:4)]
storage.mode(microsoftMatrix) <- "numeric"

#Plot candle stick graph
dygraph(microsoftMatrix, xlab = "Days", ylab="price ($)", main="Candlestick chart") %>%
dyCandlestick() %>%
dyRangeSelector()

```

### Twitter

```{r}

#Process data to create candle stick chart
twtrData$Date <- as.Date(as.character(twtrData$Date))
twitterMatrix <- data.matrix(as.xts(twtrData, order.by = twtrData$Date))
twitterMatrix <- twitterMatrix[,-1]
twitterMatrix <- twitterMatrix[,(1:4)]
storage.mode(twitterMatrix) <- "numeric"

#Plot candle stick graph
dygraph(twitterMatrix, xlab = "Days", ylab="price ($)", main="Candlestick chart") %>%
dyCandlestick() %>%
dyRangeSelector()

```

### Comparison Of Trends
    
```{r}
#Modify the original data to remove unwanted columns
applModified  <- removeUnwantedColumns(applData)
twtrModified <- removeUnwantedColumns(twtrData)
msftModified <- removeUnwantedColumns(msftData)

#Modify the stock data
applXts <- xts(applModified$Close,order.by=applModified$Date,frequency=365)
twtrXts <- xts(twtrModified$Close,order.by=twtrModified$Date,frequency=365)
msftXts <- xts(msftModified$Close,order.by=msftModified$Date,frequency=365)

#Bind the data together for plotting
stocks <- cbind(applXts,twtrXts,msftXts)

#Plot the graph
dygraph(stocks,xlab = "Time", ylab="Closing price ($)", 
  main="Stock Price Variation") %>%
  dySeries("..1",label="Apple") %>%
  dySeries("..2",label="Twitter") %>%
  dySeries("..3",label="Microsoft") %>%
  dyOptions(colors = c("blue","green","red")) %>%
  dyRangeSelector()
```



   
Row {data-height=350}
-------------------------------------
    
### Performance Metrics
    
```{r}
#Get subset of data which represents historical performance of stocks
performanceMetrics <- stockData[,(1:10)]
                        
#Visualize as table
DT::datatable(performanceMetrics, class = 'cell-border stripe', options = list(dom = 't'))
```

Future Outlook {data-orientation=rows}
=====================================   

Row {data-height=400}
-------------------------------------

### Future Outlook

```{r}
#Get subset of data which represents future outlook of stocks
outlookMetrics <- stockData[,(11:16)]

# Visualize as table
DT::datatable(outlookMetrics, class = 'cell-border stripe', options = list(dom = 't'))

```

Row {data-height=600}
-------------------------------------
   
### Earnings Per Share Next Year

```{r}
outlookMetrics1 <- outlookMetrics
outlookMetrics2 <- data.frame(outlookMetrics1)

outlookMetrics2$Stocks <- c("Apple","Twitter","Microsoft")
outlookMetrics3 <- outlookMetrics2[,c(1,2,7)]


colnames(outlookMetrics3) <- c("eps_current","EPS_Future", "Stocks")

## Bar Plot for current year earnings per share

p1 <- ggplot(data=outlookMetrics3, aes(x=Stocks, y=eps_current, fill=Stocks)) +
      geom_bar(colour="black", stat="identity") +
      guides(fill=FALSE)+
         ylab("Earnings ($)") + theme_base() + theme(legend.position="none")   
     

p2 <- ggplotly(p1)

p2
```   
    
### Future Earnings Per Share

```{r}

## Bar Plot for current year earnings per share

p3 <- ggplot(data=outlookMetrics3, aes(x=Stocks, y=EPS_Future, fill=Stocks)) +
      geom_bar(colour="black", stat="identity") +
      guides(fill=FALSE)+
         labs(y = "Earnings($)")  + theme_base() + theme(legend.position="none") 
p4 <- ggplotly(p3)

p4
#barplot( EPS_Future1, main="Future Earnings Per Share", ylab = " Earnings($)", # #beside=TRUE, col=colours)
```