Last active
July 18, 2019 19:41
-
-
Save slopp/bbc9e8a88a6fe1aebafe9ca425c574f3 to your computer and use it in GitHub Desktop.
Transformation from Static to Shiny Dashboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| title: "Stock Analysis" | |
| output: | |
| flexdashboard::flex_dashboard: | |
| orientation: columns | |
| vertical_layout: fill | |
| runtime: shiny | |
| --- | |
| ```{r setup, include=FALSE} | |
| library(flexdashboard) | |
| library(tidyverse) | |
| library(tidyquant) | |
| library(timetk) | |
| library(highcharter) | |
| library(DT) | |
| ``` | |
| Inputs {.sidebar} | |
| ----------------------------------------------------------------------- | |
| ```{r} | |
| textInput('ticker', 'Enter Stock Ticker Symbol', 'GOOG') | |
| price <- reactive({tq_get(input$ticker, from = "2018-01-01") %>% | |
| mutate(change = close - open)}) | |
| ``` | |
| Column {data-width=500} | |
| ----------------------------------------------------------------------- | |
| ### Stock Price Over Time (Chart) | |
| ```{r} | |
| renderHighchart({ | |
| xts_price <- tk_xts(price()) | |
| colnames(xts_price) <- paste0(input$ticker,'.', colnames(xts_price)) | |
| highchart(type = "stock") %>% | |
| hc_add_series(xts_price, type = "candlestick") | |
| }) | |
| ``` | |
| Column {data-width=500} | |
| ----------------------------------------------------------------------- | |
| ### Stock Volatility | |
| ```{r} | |
| renderValueBox({ | |
| volatility <- price() %>% | |
| select(date, adjusted) %>% | |
| mutate(returns = (log(adjusted) - log(lag(adjusted)))) %>% | |
| na.omit() %>% | |
| summarize(volatility = var(returns)) %>% | |
| pull(volatility) | |
| valueBox( | |
| sprintf('%g%%', round(volatility*100,2)), | |
| icon = 'ion-cash' | |
| ) | |
| }) | |
| ``` | |
| ### Stock Price Table | |
| ```{r} | |
| DT::renderDataTable({ | |
| DT::datatable(price(), rownames = FALSE, options = list(order = list(list(0, 'desc')))) %>% | |
| formatCurrency(c('open', 'high', 'low', 'close', 'adjusted', 'change')) %>% | |
| formatDate(c('date')) %>% | |
| formatStyle('volume', | |
| background = styleColorBar(price()$volume, 'steelblue') | |
| ) %>% | |
| formatStyle( | |
| 'change', | |
| color = styleInterval(c(0), c('maroon', 'darkgreen')) | |
| ) | |
| }) | |
| ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment