Aba 1

Column

Nuvem de palavras

Sales Forecast

Sales by State

Column

Sales by Category

Best Sellers

Novo Chart

Aba 2

Column

Novo Chart

Novo Chart 2

Column

Tabela 1

Aba 3

Column

Heatmap

Aba 4

Column

Gantt

Column

Time Line

---
title: "Mar a Vista"
author: "Weslley Marçal"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    social: menu
    source_code: embed

---



```{r setup, include=FALSE}
library(highcharter)
library(dplyr)
library(viridisLite)
library(forecast)
library(treemap)
library(arules)
library(flexdashboard)
library(shiny)


thm <- 
hc_theme(
  colors = c("#1a6ecc", "#434348", "#90ed7d"),
  chart = list(
    backgroundColor = "transparent",
    style = list(fontFamily = "Source Sans Pro")
  ),
  xAxis = list(
    gridLineWidth = 1
  )
)

```


Aba 1
=======================================================================

Column {data-width=600}
-----------------------------------------------------------------------

### Nuvem de palavras

```{r}
#Sites com graficos
#(OBS) AO INSERIR BOTOES DE CONTROLE DE GRAFICO, PASSA A SER UM APLICATIVO SHINY

#https://r-graph-gallery.com/
#https://shiny.posit.co/r/gallery/
  
#Importação de Banco de Dados
library(odbc)
library(DBI)
library(tibble)
library(flextable)
library(readxl)
library(xlsx)
library(dplyr)
library(stringr)
library(wordcloud)
library(dplyr)

Diretorio<-"W:/WESLLEY/Portico Construtora/Dashboards/Banco.accdb"
setwd("W:\\WESLLEY\\Portico Construtora\\Dashboards")
con<-odbc::dbConnect(odbc(),Driver="Microsoft Access Driver (*.mdb, *.accdb)",Dbq=Diretorio)
#odbc::odbcListDrivers()

#Leitura de dados
Lista_Tabelas<-dbListTables(con)
#View(as.data.frame(as.matrix(Lista_Tabelas)))

Dados<-dbReadTable(con,dbListTables(con)[1])

Lista<-list()
Vazias<-c(2,3,8,9,10)
for(i in 1:length(Lista_Tabelas)){
  if(!(i==2||i==3||i==8||i==9||i==10)){
    Lista[[i]]<-(dbReadTable(con,dbListTables(con)[i]))
    
#    print(i)
#    str(Lista[[i]])

  }else{
    Lista[[i]]<-c(1)
    
  }
#  head(Lista[[i]])
}

#View(as.data.frame(as.matrix(colnames(Lista[[17]]))))

Tab1<-data.frame(Lista[[17]][,c(175,209,149,151)])

#Por os caracteres na ISO latina
Encoding(Tab1$Nome)<-"ISO-8859-1"

#Gerando nuvem de palavras
frequencia<-table(Tab1$Nome)
frequencia<-sort(frequencia, decreasing=TRUE)
Palavras<-names(frequencia)
frequencia<-as.vector(frequencia)

#wordcloud(words = Palavras, freq = frequencia,min.freq = 1, random.order = TRUE,colors=brewer.pal(8,"Dark2"), use.r.layout=TRUE, rot.per= 0.5)

pie(table(Tab1$Trabalho))

```

### Sales Forecast

```{r}
AirPassengers %>% 
  forecast(level = 90) %>% 
  hchart()%>% 
  hc_add_theme(thm)

```


### Sales by State

```{r}
data("USArrests", package = "datasets")
data("usgeojson")

USArrests <- USArrests %>%
  mutate(state = rownames(.))

n <- 4
colstops <- data.frame(
  q = 0:n/n,
  c = substring(viridis(n + 1), 0, 7)) %>%
  list_parse2()

highchart() %>%
  hc_add_series_map(usgeojson, USArrests, name = "Sales",
                    value = "Murder", joinBy = c("woename", "state"),
                    dataLabels = list(enabled = TRUE,
                                      format = '{point.properties.postalcode}')) %>%
  hc_colorAxis(stops = colstops) %>%
  hc_legend(valueDecimals = 0, valueSuffix = "%") %>%
  hc_mapNavigation(enabled = TRUE)%>% 
  hc_add_theme(thm)

```

Column {.tabset data-width=400}
-----------------------------------------------------------------------

### Sales by Category

```{r, fig.keep='none'}
data("Groceries", package = "arules")
dfitems <- tbl_df(Groceries@itemInfo)

set.seed(10)

dfitemsg <- dfitems %>%
  mutate(category = gsub(" ", "-", level1),
         subcategory = gsub(" ", "-", level2)) %>%
  group_by(category, subcategory) %>% 
  summarise(sales = n() ^ 3 ) %>% 
  ungroup() %>% 
  sample_n(31)

tm <- treemap(dfitemsg, index = c("category", "subcategory"),
              vSize = "sales", vColor = "sales",
              type = "value", palette = rev(viridis(6)))

hctreemap(tm, allowDrillToNode = TRUE, layoutAlgorithm = "squarified") %>% 
  hc_add_theme(thm)


```

### Best Sellers

```{r}
set.seed(2)

nprods <- 10

dfitems %>% 
  sample_n(nprods) %>% 
  .$labels %>% 
  rep(times = sort(sample( 1e4:2e4, size = nprods), decreasing = TRUE)) %>% 
  factor(levels = unique(.)) %>% 
  hchart(showInLegend = FALSE, name = "Sales", pointWidth = 10)  %>% 
  hc_chart(type = "bar")%>% 
  hc_add_theme(thm)

  
```


### Novo Chart

```{r}
hist(Tab1$Variação.da.Duração)

```

Aba 2
=======================================================================

Column {data-width=600}
-----------------------------------------------------------------------

### Novo Chart

```{r}
hist(Tab1$Variação.da.Duração)

```

### Novo Chart 2

```{r}
hist(Tab1$Variação.da.Duração)

```

Column {data-width=400}
-----------------------------------------------------------------------

### Tabela 1

```{r}
#knitr::kable(Tab1[1:20,])

DT::datatable(Tab1[1:20,])
```

Aba 3
=======================================================================

Inputs {.sidebar}
-----------------------------------------------------------------------

```{r}
library(biclust)
selectInput("clusterNum", label = h3("Cluster number"), 
    choices = list("1" = 1, "2" = 2, "3" = 3, "4" = 4, "5" = 5), 
    selected = 1)
```

Microarray data matrix for 80 experiments with Saccharomyces Cerevisiae
organism extracted from R's `biclust` package.

Sebastian Kaiser, Rodrigo Santamaria, Tatsiana Khamiakova, Martin Sill, Roberto
  Theron, Luis Quintales, Friedrich Leisch and Ewoud De Troyer. (2015). biclust:
  BiCluster Algorithms. R package version 1.2.0.
  http://CRAN.R-project.org/package=biclust


Column {data-width=600}
-----------------------------------------------------------------------

### Heatmap

```{r}
#write.csv(Tab1,"dados1.csv")
Tab1<-as.data.frame(read.csv("dados1.csv"))

num <- reactive(as.integer(input$clusterNum))

renderPlot({
  plot(Tab1[num(),c(1,2,3,4)])
})

``` 

Aba 4
=======================================================================

Column {data-width=500} 
-----------------------------------------------------------------------

### Gantt

```{r}
library(plotly)

df <- read.csv("https://cdn.rawgit.com/plotly/datasets/master/GanttChart-updated.csv", 
               stringsAsFactors = F)

df$Start  <- as.Date(df$Start, format = "%m/%d/%Y")
client    <- "Sample Client"
cols      <- RColorBrewer::brewer.pal(length(unique(df$Resource)), name = "Set3")
df$color  <- factor(df$Resource, labels = cols)

p <- plot_ly()
for(i in 1:(nrow(df) - 1)){
  p <- add_trace(p,
                 x = c(df$Start[i], df$Start[i] + df$Duration[i]), 
                 y = c(i, i), 
                 mode = "lines",
                 line = list(color = df$color[i], width = 20),
                 showlegend = F,
                 hoverinfo = "text",
                 text = paste("Task: ", df$Task[i], "<br>",
                              "Duration: ", df$Duration[i], "days<br>",
                              "Resource: ", df$Resource[i]),
                 evaluate = T
  )
}

p

```


Column {data-width=500} 
-----------------------------------------------------------------------

### Time Line

```{r}
library(timevis)

data <- data.frame(
  id      = 1:4,
  content = c("Item one"  , "Item two"  ,"Ranged item", "Item four"),
  start   = c("2016-01-10", "2016-01-11", "2016-01-20", "2016-02-14 15:00:00"),
  end     = c(NA          ,           NA, "2016-02-04", NA)
)

timevis(data)

```