library(flexdashboard)
library(tidyquant)
library(dplyr)
library(ggplot2)
library(plotly)
library(DT)

prices <- read.csv("prices.csv") %>%
  mutate(date = as.Date(date))

tabela_periodo <- data.frame(
  periodo = c("1M", "6M", "1A", "10A"),
  dias = c(30, 180, 365, 3650)
)

colours = c(
  "BBAS3" = "black", "BBDC4" = " blue", "ITUB4"= "green", "SANB11" = "red", "BSLI3" = "yellow",
  "BPAC11" = "pink","BPAN4" = "orange","MODL11" = "brown","BNBR3" = "dark blue","ABCB4" = "cyan"
)

Column {.sidebar}

selectInput(
  "acao", label = "Escolha sua ação",
  choices = c(
    "BBAS3", "BBDC4", "ITUB4", "SANB11", "BSLI3", 
    "BPAC11","BPAN4","MODL11","BNBR3","ABCB4"
  )
)
selectInput(
  "periodo", label = "Escolha o periodo",
  choices = c("1M", "6M", "1A", "10A")
)

ROW {data-width=500}

Tendência

renderPlotly({
  periodo <- Sys.Date() - filter(tabela_periodo, periodo == input$periodo)$dias

  prices %>% 
    filter(date >= periodo, symbol == input$acao) %>% 
    ggplot() +
    aes(x = date, y = adjusted, color = symbol) +
    geom_line() +
    scale_color_manual(values=colours) +
    labs(x = "", y = "Cotação",) +
    ggtitle(label = input$acao) +
    theme(
      panel.background = element_blank(),
      plot.title = element_text(size=22),
      legend.position = "none"
    )
})

Todas as Tendências

renderPlotly({
  periodo <- Sys.Date() - filter(tabela_periodo, periodo == input$periodo)$dias

  prices %>% 
    filter(date >= periodo) %>% 
    ggplot() +
    aes(x = date, y = adjusted, color = symbol) +
    scale_color_manual(values = colours) +
    geom_line(size = 1.1) +
    labs(x = "", y = "Cotação", color="Ação") +
    ggtitle(label = input$acao) +
    theme(
      panel.background = element_blank(),
    )
})

Row {data-width=500}

Cotação

renderDataTable({
  periodo <- Sys.Date() - filter(tabela_periodo, periodo == input$periodo)$dias 

  prices %>% 
    filter(date >= periodo, symbol == input$acao) %>%
    mutate(across(where(is.double), round, digits = 2)) %>%  
    arrange(desc(date)) %>%
    rename(
      Ação = symbol,
      Data = date,
      Abertura = open,
      Máximo = high,
      Mínimo = low,
      Fechamento = close,
      Volume = volume,
      Ajustado = adjusted
    ) %>% 
    datatable(rownames = FALSE)
})


LuisLimaJr/DashboardAcoes documentation built on May 16, 2022, 12:22 a.m.