R/dashboard.R

#' Fireguard Dashboard
#'
#' @import magrittr shiny shinydashboard dygraphs
#'
#' @export
fireguardDashboard <- function() {

  tix <- c("VTI", "VXUS", "BND", "VEA", "VEU", "SPX", "DJI")
  if (file.exists("data/index.csv")) {
    data <- data.table::fread("data/index.csv") %>% dplyr::mutate(date = as.Date(date))
  } else {
    if (file.exists(".api-key")) {
      data <- get_data(tix, get_api_key())
      write.csv(data, file = "data/index.csv", row.names = FALSE)
    } else {
      message("Set api key using 'set_api_key', no existing data available.")
    }
  }

  # Sidebar UI --------------------------------------------------------------
  sidebar <- dashboardSidebar(
    width = '80px',
    sidebarMenu(
      id = 'tabs',
      menuItem('', tabName = 'graph_index', icon = icon('home', lib = 'glyphicon', 'fa-3x'))
    )
  )

  # Body UI -----------------------------------------------------------------
  body <- dashboardBody(
    # links to static resources
    tags$link(rel = "stylesheet", type = "text/css", href = "custom.css"),
    tags$script(src="custom.js"),

    tabItems(
      # Index Fund Graph
      tabItem(tabName = "graph_index",
        fluidPage(
          fluidRow(
            box(title = "Choose Tickers",
                selectInput(inputId = "selectTickers",
                            choices = tix,
                            selected = tix[1:3],
                            label = "Pick Index Funds",
                            multiple = TRUE),
                width = NULL)),
          fluidRow(
            box(title = "Index Fund Plots",
                dygraphs::dygraphOutput(outputId = 'indexPlot', height = '600px'),
                width = NULL))))
    )
  )

  ui <- dashboardPage(dashboardHeader(title = "Fireguard Dashboard"), sidebar, body, skin = "black")

  # Server ------------------------------------------------------------------
  server <- function(input, output) {

    output$indexPlot <- dygraphs::renderDygraph({
      plot_index_price(data %>% dplyr::select(date, input$selectTickers))
    })

  }

  shinyApp(ui, server)
}
logan-connolly/fireguard documentation built on May 31, 2019, 10:35 a.m.