Summary

Row

confirmed

33,089

last 24h

4,517

recovered

3,355

death

2182

Row

Daily Cumulative Cases by Type

Cases by CCAA

Map

Map

Simulated data

Simulated Visualitzations

Row

Histogram, e.g. age

Interactive

Row

Histogram, e.g. age by sex

Interactive

Row

Pie char. e.g. Gender

Pie char. e.g. Fiebre

Row

Long Data. e.g. Temperature

---
title: "Test html"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    social: menu
    source_code: embed
    vertical_layout: scroll
editor_options: 
  chunk_output_type: console
---

```{r setup, include=FALSE}
#------------------ Packages ------------------
library(flexdashboard)
# library(coronavirus)
library(rvest)

# library(httr)
# library(readxl)
library(dplyr)
# data(coronavirus)

`%>%` <- magrittr::`%>%`
#------------------ Parameters ------------------
# Set colors
# https://www.w3.org/TR/css-color-3/#svg-color
confirmed_color <- "purple"
active_color <- "#1f77b4"
recovered_color <- "forestgreen"
death_color <- "red"
#------------------ Data ------------------
# Spain data
## Page of ISCSIII works wiht java 
# system("/home/josep/Baixades/phantomjs-2.1.1-linux-x86_64/bin/phantomjs  /home/josep/Documents/01_Idisba/24_covid/scraper_PaddyPower.js")
# page<-"covid_java.html"
# # Get page
# tmp <- read_html(page)

# Get table
#tmp_table <- html_table(tmp)[[1]]
# write.csv(tmp_table,"/home/josep/Documents/01_Idisba/24_covid/tmp_table.csv")

tmp_table<-read.csv("./tmp_table.csv")
Cases<-33089
# tmp %>%
#         html_nodes("#casos") %>% 
#         html_text()

Cases_24h<-4517
# tmp %>%
#         html_nodes("#casos24h") %>% 
#         html_text()

Cases_recovered<-3355
  # tmp %>%
  #       html_nodes("#recuperados") %>% 
  #       html_text()

Cases_hosp<-18374
  # tmp %>%
  #       html_nodes("#hospitalizados") %>% 
  #       html_text()
Cases_deaths<-2182
  # tmp %>%
  #       html_nodes("#defunciones") %>% 
  #       html_text()
  # 
df <- tmp_table

```

Summary
=======================================================================
Row
-----------------------------------------------------------------------

### confirmed {.value-box}

```{r}

valueBox(value = paste(format(Cases, big.mark = ","), "", sep = " "), 
         caption = "Total Confirmed Cases", 
         icon = "fas fa-user-md", 
         color = confirmed_color)
```


### last 24h {.value-box}

```{r}
valueBox(value = paste(format(Cases_24h, big.mark = ",")," ", sep = ""), 
         caption = "Last 24h", icon = "fas fa-ambulance", 
         color = active_color)
```

### recovered {.value-box}

```{r}
valueBox(value = paste(format(Cases_recovered, big.mark = ","), sep = ""), 
         caption = "Recovered Cases", icon = "fas fa-heartbeat", 
         color = recovered_color)
```

### death {.value-box}

```{r}

valueBox(value = Cases_deaths,
         caption = "Death Cases", 
         icon = "fas fa-heart-broken", 
         color = death_color)
```



Row {data-width=400}
-----------------------------------------------------------------------


### Daily Cumulative Cases by Type
    
```{r,include=F}
library(httr)
library(readxl)
library(dplyr)
# url <- paste("https://www.ecdc.europa.eu/sites/default/files/documents/COVID-19-geographic-disbtribution-worldwide-",format(Sys.time(), "%Y-%m-%d"), ".xlsx", sep = "")

## If is not avaibable the day selected

url<-"https://www.ecdc.europa.eu/sites/default/files/documents/COVID-19-geographic-disbtribution-worldwide-2020-03-23.xlsx"

GET(url, authenticate(":", ":", type="ntlm"), write_disk(tf <- tempfile(fileext = ".xlsx")))
data <- read_excel(tf)
data_spain<-
data %>% 
  filter(`Countries and territories`=="Spain")
df <- data_spain %>% 
  dplyr::group_by("Countries and territories" ) %>%
  # dplyr::filter(date == max(date)) %>%
  dplyr::mutate(total_cases = sum(Cases), total_deaths=sum(Deaths))

df_daily <- data_spain %>% 
  dplyr::group_by(DateRep) %>%
 
  dplyr::arrange(DateRep) %>%
  dplyr::ungroup() %>%
  dplyr::mutate(death_cum = cumsum(Deaths),
                
                active_cum = cumsum(Cases))
  

df1 <- data_spain %>% dplyr::filter(DateRep == max(DateRep))



```

```{r}

plotly::plot_ly(data = df_daily)%>%
  plotly::add_trace(x = ~ DateRep,
                    y = ~ Cases,
                    type = "scatter",
                    mode = "lines+markers",
                    name = "Active",
                    line = list(color = active_color),
                    marker = list(color = active_color)) %>% 

  
  plotly::add_trace(x = ~ DateRep,
                    y = ~ Deaths,
                    type = "scatter",
                    mode = 'lines+markers',
                    name = "Death",
                    line = list(color = death_color),
                    marker = list(color = death_color)) %>%
    
  plotly::layout(title = "",
                 yaxis = list(title = "Cumulative Number of Cases"),
                 xaxis = list(title = "Date"),
                 legend = list(x = 0.1, y = 0.9),
                 hovermode = "compare")
  
```


### Cases by CCAA
    
```{r}

df_summary <-tmp_table 
df_summary %>%
  DT::datatable(rownames = FALSE,
            colnames = c("","","CCAA", "Total", "Last 24h", "Inc.14d"),extensions = 'Buttons',
                options = list(pageLength = nrow(df_summary), dom = 'Bfrtip',buttons = c('copy', 'csv', 'excel', 'pdf', 'print')))
    
  
 
```


Map
=======================================================================

**Map**

```{r}
# map tab added by Art Steinmetz
library(leaflet)
library(leafpop)
library(purrr)
library(mapview)
  carto_base_2 <- sf::read_sf("/home/josep/Baixades/CCAA_2/")  #Asumiendo que está en el subdirectorio provincias de getwd()

# Eliminar espai
carto_base_2$NOM_CCAA[1]<-"ANDALUCÍA"
carto_base_2$NOM_CCAA[2]<-"ARAGÓN"
carto_base_2$NOM_CCAA[3]<-"ISLAS BALEARES"
carto_base_2$NOM_CCAA[4]<-"ISLAS CANARIAS"
carto_base_2$NOM_CCAA[7]<-"CASTILLA Y LEÓN"
carto_base_2$NOM_CCAA[8]<-"CATALUÑA"
carto_base_2$NOM_CCAA[13]<-"COMUNIDAD DE MADRID"
carto_base_2$NOM_CCAA[15]<-"REGIÓN DE MURCIA"
carto_base_2$NOM_CCAA[16]<-"COMUNIDAD FORAL NAVARRA"
carto_base_2$NOM_CCAA[17]<-"PAÍS VASCO"
carto_base_2$NOM_CCAA[18]<-"PRINCIPADO DE ASTURIAS"
carto_base_2$NOM_CCAA[19]<-"COMUNIDAD VALENCIANA"
library(stringr)
tmp_table$NOM_CCAA<-str_to_upper(tmp_table$CCAA)
left_join(carto_base_2, tmp_table) -> carto_unida


  
library(ggplot2)
library(tidyverse)
library(sf)

#carto_unida$Total
carto_unida %>% 
    ggplot() +
    geom_sf(aes(fill=NOM_CCAA))+
    scale_fill_grey()+
    
    scale_color_gradient(low="blue", high="red") + theme_void()+
    ggrepel::geom_label_repel(aes(label = Total, geometry = geometry),
                              stat = "sf_coordinates",
                              min.segment.length = 0,
                              colour = "goldenrod2",
                              segment.colour = "goldenrod2"
    )+
    theme(legend.position="none")+
  ggtitle("Total cases 23/03")



```

Simulated data
=======================================================================
```{r}
data<-read.csv("/home/josep/Documents/01_Idisba/24_covid/data_sim.csv")
#apply(data,2,is.numeric)
for (i in 1: dim(data)[2]){
  if(is.numeric(data[,i])){
    data[,i]<-round(data[,i],3)
  }
}


DT::datatable(data[,-1], filter =  "top",extensions = 'Buttons',
                options = list(pageLength = nrow(df_summary), dom = 'Bfrtip',buttons = c('copy', 'csv', 'excel', 'pdf', 'print')))


```

Simulated Visualitzations
=======================================================================
Row
-----------
### Histogram,  e.g. age 

*Interactive*

```{r,echo=F, warning=F}
library(plotly)

ggplotly(
data %>% 
  group_by(id,hosp) %>% 
  ggplot(aes(edad))+facet_grid(. ~ factor(hosp))+
  geom_histogram(aes(y=..density..))+
 geom_density(alpha=.2, fill="#FF6666") +
theme_linedraw()
)
 # plot_1<-ggplot(data=data, aes(edad)) + 
#   geom_histogram()

  


```

Row
-----------

### Histogram,  e.g. age by sex 

*Interactive*

```{r,echo=F, warning=F}
library(plotly)

ggplotly(
data %>% 
  group_by(id,hosp) %>% 
  ggplot(aes(edad,color=factor(sexo)))+facet_grid(. ~ factor(hosp))+
  geom_histogram(aes(y=..density..))+
 geom_density(alpha=.2, fill="#FF6666") +
theme_linedraw()
)
 # plot_1<-ggplot(data=data, aes(edad)) + 
#   geom_histogram()

  


```

Row
-----------

### Pie char. e.g. Gender

```{r}
mycols <- c("#0073C2FF", "#EFC000FF", "#868686FF", "#CD534CFF")


data %>%
  group_by(hosp) %>% 
  count(hosp, sexo) %>%
  mutate(prop = prop.table(n)) %>% 
ggplot(aes(x = 2, y = prop, fill = factor(sexo))) +
  geom_bar(stat = "identity", color = "white")+
  coord_polar(theta = "y", start = 0)+
  facet_grid(. ~ factor(hosp))+
  scale_fill_manual(values = mycols) +
  theme_void()+
  xlim(0.5, 2.5)+ggtitle("Hospital ID")

```

### Pie char. e.g. Fiebre

```{r}
mycols <- c("#0073C2FF", "#EFC000FF", "#868686FF", "#CD534CFF")


data %>%
  group_by(hosp) %>% 
  count(hosp, fiebre) %>%
  
  mutate(prop = round(prop.table(n),3)*100) %>% 
   
  mutate(lab.ypos = cumsum(prop) - 0.5*prop) %>% 
ggplot(aes(x = 2, y = prop, fill = factor(fiebre))) +
  geom_bar(stat = "identity", color = "white")+
  coord_polar(theta = "y", start = 0)+
  facet_grid(. ~ factor(hosp))+
   geom_text(aes(y = lab.ypos, label = prop), color = "white")+
  scale_fill_manual(values = mycols) +
  scale_fill_manual(values = mycols) +
  theme_void()+
  xlim(0.5, 2.5)+ggtitle("Hospital ID")

```


Row
-----------
### Long Data. e.g. Temperature

```{r}
library(scales)
library(plotly)
data$fecha<-(as.Date(data$fecha))

dat1<-
data %>% 
  group_by(hosp,id)
dat1$covid19<-factor(dat1$covid19)
levels(dat1$covid19)<-c("Covid-","Covid+")
p <- ggplot(data = dat1, aes(x = fecha, y = temperatura))
p1<-p+ facet_grid(. ~ factor(covid19))+ 
  stat_smooth(aes(colour = factor(hosp))) +
  geom_point(size = 0.02)+ 
  theme(
    axis.text.x = element_text(size=7,angle = 90, hjust = 1))+
  scale_x_date(labels = date_format("%d/%m/%y"),date_breaks = "1 day" )
p1$labels$colour<-"Hospital"

ggplotly(p1)

```