---
title: "ANLY 512: Data Visualization: Dashboard Laboratory"
author: "Yujia Liao"
date: "`r Sys.Date()`"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
social: menu
source_code: embed
---
```{r setup, include=FALSE, message=FALSE}
library(flexdashboard)
library(PerformanceAnalytics)
library(quantmod)
library(tidyr)
library(DT)
library(plyr)
library(dygraphs)
library(lubridate)
```
Column {.tabset data-width=450}
-----------------------------------------------------------------------
### Stock Closing Prices
```{r,echo=FALSE, message = FALSE}
ticker <- c("AAPL","CAT","TXN", "AMZN")
getSymbols(ticker, from="2018-01-01", to="2019-01-01")
ClosingPrices <- do.call(merge, lapply(ticker, function(x) Cl(get(x))))
dateperiod<-c("2018-01-01", "2019-01-01")
dygraph(ClosingPrices, main="Closing Price in Dollars", group="Stock") %>%
dyAxis("y", label="Closing Price(USD)") %>%
dyOptions(axisLineWidth = 2.0, colors = RColorBrewer::brewer.pal(5, "Set1")) %>%
dyHighlight(highlightSeriesBackgroundAlpha = 1.0,
highlightSeriesOpts = list(strokeWidth = 3)) %>%
dyRangeSelector(height = 65)
```
### Tabular View
```{r}
library(plyr)
what_metrics <- yahooQF(c("Price/Sales",
"P/E Ratio",
"Price/EPS Estimate Next Year",
"PEG Ratio",
"Dividend Yield",
"Market Capitalization"))
tickers <- c("AAPL", "CAT", "TXN", "AMZN")
# Not all the metrics are returned by Yahoo.
metrics <- getQuote(paste(tickers, sep="", collapse=";"), what=what_metrics)
#Add tickers as the first column and remove the first column which had date stamps
metrics <- data.frame(Symbol=tickers, metrics[,2:length(metrics)])
#Change colnames
colnames(metrics) <- c("Symbol", "P-E Ratio", "Price EPS Estimate Next Year", "Div Yield", "Market Cap")
#Persist this to the csv file
#write.csv(metrics, "FinancialMetrics.csv", row.names=FALSE)
DT::datatable(metrics)
```
Column {.tabset data-width=550}
-----------------------------------------------------------------------
### Apple
```{r}
invisible(getSymbols("AAPL", src = "yahoo", from='2018-01-01'))
AAPL_x <- AAPL
dygraph(AAPL_x[, -5], main = "Apple") %>%
dyCandlestick() %>%
dyAxis("y", label="Closing Price") %>%
dyOptions(colors= RColorBrewer::brewer.pal(5, "Set1")) %>%
dyHighlight(highlightCircleSize = 4,
highlightSeriesOpts = list(strokeWidth = 3),
highlightSeriesBackgroundAlpha = 1) %>%
dyRangeSelector(height = 65)
```
### Caterpillar
```{r}
invisible(getSymbols("CAT", src = "yahoo", from='2018-01-01'))
CAT_x <- CAT
dygraph(CAT_x[, -5], main = "Caterpillar") %>%
dyCandlestick() %>%
dyAxis("y", label="Closing Price") %>%
dyOptions(colors= RColorBrewer::brewer.pal(5, "Set1")) %>%
dyHighlight(highlightCircleSize = 4,
highlightSeriesOpts = list(strokeWidth = 3),
highlightSeriesBackgroundAlpha = 1) %>%
dyRangeSelector(height = 65)
```
###Texas Instruments
```{r}
invisible(getSymbols("TXN", src = "yahoo", from='2018-01-01'))
TI_x <- TXN
dygraph(TI_x[, -5], main = "TI") %>%
dyCandlestick() %>%
dyAxis("y", label="Closing Price") %>%
dyOptions(colors= RColorBrewer::brewer.pal(5, "Set1")) %>%
dyHighlight(highlightCircleSize = 4,
highlightSeriesOpts = list(strokeWidth = 3),
highlightSeriesBackgroundAlpha = 1) %>%
dyRangeSelector(height = 65)
```
### Amazon
```{r}
invisible(getSymbols("AMZN", src = "yahoo", from='2018-01-01'))
AMZN_x <- AMZN
dygraph(AMZN_x[, -5], main = "Amazon") %>%
dyCandlestick() %>%
dyAxis("y", label="Closing Price") %>%
dyOptions(colors= RColorBrewer::brewer.pal(5, "Set1")) %>%
dyHighlight(highlightCircleSize = 4,
highlightSeriesOpts = list(strokeWidth = 3),
highlightSeriesBackgroundAlpha = 1) %>%
dyRangeSelector(height = 65)
```