์ „๊ตญ

columns 1

์ „๊ตญ ์•ˆ์‹ฌ๋ณ‘์›&์ง„๋ฃŒ์†Œ ์œ„์น˜์ง€๋„

columns 2

์‹œ๋„๋ณ„ ์•ˆ์‹ฌ๋ณ‘์› & ์ง„๋ฃŒ์†Œ ๊ทธ๋ž˜ํ”„

๋ฐ์ดํ„ฐ(์ „๊ตญ)

์„œ์šธ

columns 1

์„œ์šธ - ์•ˆ์‹ฌ๋ณ‘์›&์ง„๋ฃŒ์†Œ ์œ„์น˜์ง€๋„

columns 2

์„œ์šธ - ๊ตฌ๋ณ„ ์•ˆ์‹ฌ๋ณ‘์› & ์ง„๋ฃŒ์†Œ ๊ทธ๋ž˜ํ”„

๋ฐ์ดํ„ฐ(์„œ์šธ)

๋Œ€๊ตฌ

columns 1

๋Œ€๊ตฌ - ์•ˆ์‹ฌ๋ณ‘์›&์ง„๋ฃŒ์†Œ ์œ„์น˜์ง€๋„

columns 2

๋Œ€๊ตฌ - ๊ตฌ๋ณ„ ์•ˆ์‹ฌ๋ณ‘์› & ์ง„๋ฃŒ์†Œ ๊ทธ๋ž˜ํ”„

๋ฐ์ดํ„ฐ(๋Œ€๊ตฌ)

์ „๊ตญ

Colunms 1

์ „๊ตญ ๋งˆ์Šคํฌ ์ œ๊ณต ์œ„์น˜์ง€๋„

Colunms 2

์‹œ๋„๋ณ„ ๋งˆ์Šคํฌ ์ œ๊ณต ๊ธฐ๊ด€ ๊ทธ๋ž˜ํ”„

๋ฐ์ดํ„ฐ(์ „๊ตญ)

์„œ์šธ

columns 1

์„œ์šธ ๋งˆ์Šคํฌ ์ œ๊ณต ์œ„์น˜์ง€๋„

columns 2

์„œ์šธ ๊ตฌ๋ณ„ ๋งˆ์Šคํฌ ์ œ๊ณต ๊ธฐ๊ด€ ๊ทธ๋ž˜ํ”„

๋ฐ์ดํ„ฐ(์„œ์šธ)

๋Œ€๊ตฌ

columns 1

๋Œ€๊ตฌ ๋งˆ์Šคํฌ ์ œ๊ณต ์œ„์น˜์ง€๋„

columns 2

๋Œ€๊ตฌ ๊ตฌ๋ณ„ ๋งˆ์Šคํฌ ์ œ๊ณต ๊ธฐ๊ด€ ๊ทธ๋ž˜ํ”„

๋ฐ์ดํ„ฐ(๋Œ€๊ตฌ)

Reference

0. ๋ฐ์ดํ„ฐ

1. Flexdashboard ์ฐธ๊ณ  ์ž๋ฃŒ

1-1. Flexdashboard ์˜ˆ์ œ

2. leaflet ์ฐธ๊ณ ์ž๋ฃŒ

3. ๋Œ€์‹œ๋ณด๋“œ UI ์ฐธ๊ณ ์ž๋ฃŒ

4. ์œ„๊ฒฝ๋„๋กœ ํ–‰์ •๊ตฌ์—ญ ์ถ”์ถœ ์ฐธ๊ณ ์ž๋ฃŒ

5. ์•„์ด์ฝ˜ List

---
title: "Corona19 DashBoard"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    social: menu
    theme: cosmo
    storyboard: true
    thumbnails: true
    source: embed
    logo: Gareth_Frank_Bale_circle.png
    favicon:  Gareth_Frank_Bale_circle.png
    navbar:
      - { icon: "fa-facebook-f", href: "https://www.facebook.com/shjj08", align: right }
      - { icon: "fa-github", href: "https://github.com/Unfinishedgod", align: right }   
---







        



```{r setup, include=FALSE}
library(flexdashboard)
library(readr)
library(tidyverse)
library(leaflet)
library(DT)
library(httr)
library(jsonlite)
library(plotly)
library(glue)
```


```{r}
corona_haspital <- read_csv("corona_haspital.csv",locale=locale('ko',encoding='euc-kr'))

corona_clinic <- read_csv("corona_clinic.csv",locale=locale('ko',encoding='euc-kr'))


# ์ง€๋ฆฌ์ •๋ณด API๋ฅผ ์‚ฌ์šฉํ•œ ์œ„๊ฒฝ๋„ ์ถ”์ถœ
Lat_lon_fun <- function(addr) {
  data_list <-
    GET(url = 'https://dapi.kakao.com/v2/local/search/address.json',
        query = list(query = addr),
        add_headers(Authorization = paste0("KakaoAK ", "2ecacbafd523802f293b245103346b06"))) %>%
    content(as = 'text') %>%
    fromJSON()


  lon_lat_df <- tibble(์ฃผ์†Œ = addr,
                       long = as.numeric(data_list$documents$x),
                       lat = as.numeric(data_list$documents$y))

  return(lon_lat_df)
}




# ๋ณ‘์› ------------------------

hos_location_list <- corona_haspital$์ฃผ์†Œ


not_hos_list <- c(2,30,39,102,168,227,280)

hos_location_long_lat <- map_dfr(hos_location_list[-not_hos_list], function(x) {
  Lat_lon_fun(x)
})

hos_info <- corona_haspital %>%
  left_join(hos_location_long_lat)  %>%
  # select(์‹œ๋„, ์‹œ๊ตฐ๊ตฌ, ๊ธฐ๊ด€๋ช…, ์ฃผ์†Œ, ์ „ํ™”๋ฒˆํ˜ธ, long, lat) %>%
  rename(์œ ํ˜• = ์‹ ์ฒญ์œ ํ˜•)

# ์ง„๋ฃŒ์†Œ ------------------------
cli_location_list <- corona_clinic$์ฃผ์†Œ

not_cli_list <- c(128,131,158, 321,419,484, 538,540, 612)

cli_location_long_lat <- map_dfr(cli_location_list[-not_cli_list], function(x) {
  Lat_lon_fun(x)
})


cli_info <- corona_clinic %>%
  left_join(cli_location_long_lat) %>%
  # select(์‹œ๋„, ์‹œ๊ตฐ๊ตฌ, ์˜๋ฃŒ๊ธฐ๊ด€๋ช…, ์ฃผ์†Œ, `๋Œ€ํ‘œ ์ „ํ™”๋ฒˆํ˜ธ`, long, lat) %>%
  rename(์ „ํ™”๋ฒˆํ˜ธ = `๋Œ€ํ‘œ ์ „ํ™”๋ฒˆํ˜ธ`, ๊ธฐ๊ด€๋ช… = ์˜๋ฃŒ๊ธฐ๊ด€๋ช…, ์œ ํ˜• = `๊ฒ€์ฒด์ฑ„์ทจ ๊ฐ€๋Šฅ์—ฌ๋ถ€`)

cli_info$์œ ํ˜• <- case_when(
  cli_info$์œ ํ˜• == "Y" ~ "๊ฒ€์ฒด์ฒด์ทจ ๊ฐ€๋Šฅ",
  TRUE ~ "๊ฒ€์ฒด์ฒด์ทจ ๋ถˆ๊ฐ€"
)


hos_cli_info <- hos_info %>%
  mutate(๊ตฌ๋ถ„ = "๊ตญ๋ฏผ์•ˆ์‹ฌ๋ณ‘์›") %>%
  bind_rows((cli_info %>%
               mutate(๊ตฌ๋ถ„ = "์„ ๋ณ„์ง„๋ฃŒ์†Œ"))) %>%
  drop_na(long) %>%
  replace_na(list(์šด์˜๊ฐ€๋Šฅ์ผ์ž = "์šด์˜๊ฐ€๋Šฅ")) %>%
  select(๊ตฌ๋ถ„, ์‹œ๋„, ์‹œ๊ตฐ๊ตฌ, ๊ธฐ๊ด€๋ช…, ์ฃผ์†Œ, ์œ ํ˜•, ์ „ํ™”๋ฒˆํ˜ธ, ์šด์˜๊ฐ€๋Šฅ์ผ์ž, long, lat)

# write.csv(hos_cli_info, file = "hos_cli_info.csv")
# hos_cli_info <- read_csv("hos_cli_info.csv")

# hos_cli_info

hos_cli_info_seoul <- hos_cli_info %>% 
  filter(์‹œ๋„ == "์„œ์šธ")

hos_cli_info_daegu <- hos_cli_info %>% 
  filter(์‹œ๋„ == "๋Œ€๊ตฌ")
```


์ „๊ตญ {data-navmenu="๋ณด๊ฑด๋ณต์ง€๋ถ€ ์•ˆ์‹ฌ๋ณ‘์›๊ณผ ์ง„๋ฃŒ์†Œ"}
=======================================================================

columns 1 {data-width=40%}
-----------------------------------------------------------------------

### ์ „๊ตญ ์•ˆ์‹ฌ๋ณ‘์›&์ง„๋ฃŒ์†Œ ์œ„์น˜์ง€๋„

```{r}
# ๋ณด๊ฑด๋ณต์ง€๋ถ€ ์•ˆ์‹ฌ๋ณ‘์›&์ง„๋ฃŒ์†Œ
hos_cli_leaflet <- hos_cli_info %>% 
  mutate(ratingcol = case_when(
    ๊ตฌ๋ถ„ == "๊ตญ๋ฏผ์•ˆ์‹ฌ๋ณ‘์›" ~ "green",
    TRUE ~ "blue"
  ))

leaflet() %>% 
  addTiles() %>% 
  addCircleMarkers(hos_cli_leaflet$long, 
                   hos_cli_leaflet$lat, 
                   color = hos_cli_leaflet$ratingcol, 
                   radius = 8, 
                   fill = T,
                   fillOpacity = 0.5,
                   opacity = 0.1,
                   popup = paste0(hos_cli_leaflet$๊ธฐ๊ด€๋ช…, " \n(", hos_cli_leaflet$์ „ํ™”๋ฒˆํ˜ธ,")")) %>%
  addLegend("topright", 
            colors = c("green","blue"),
            labels = c("๋ณด๊ฑด๋ณต์ง€๋ถ€ ๊ตญ๋ฏผ์•ˆ์‹ฌ๋ณ‘์›",
                       "๋ณด๊ฑด๋ณต์ง€๋ถ€ ์„ ๋ณ„์ง„๋ฃŒ์†Œ"), 
            opacity = 1)
```

columns 2 { data-width=60%, data-height=200}
-------------------------------------

### ์‹œ๋„๋ณ„ ์•ˆ์‹ฌ๋ณ‘์› & ์ง„๋ฃŒ์†Œ ๊ทธ๋ž˜ํ”„ {data-height=40%}

```{r}
theme_set(theme_classic())

# hos_cli_info$๊ตฌ๋ถ„ <- as.factor(hos_cli_info$๊ตฌ๋ถ„)
# 
# p <- hos_cli_info %>% 
#   group_by(๊ตฌ๋ถ„,์‹œ๋„) %>% 
#   summarise(๊ฐœ์ˆ˜ = n()) %>% 
#   arrange(๊ตฌ๋ถ„, ์‹œ๋„) %>% 
#   ggplot(aes(x = ์‹œ๋„, y = ๊ฐœ์ˆ˜, fill = ๊ตฌ๋ถ„)) +
#   geom_col(position="dodge") +
#   scale_fill_manual(values = c("green3", "blue3")) +
#   labs(title = "์ง€์—ญ๋ณ„ ๋ณ‘์›, ์ง„๋ฃŒ์†Œ ํŒŒ์•…", x = "", y = "") +
#   theme(legend.position = "bottom")
# 
# 
# ggplotly(p)

df_plotly <- hos_cli_info %>% 
  group_by(๊ตฌ๋ถ„,์‹œ๋„) %>% 
  summarise(๊ฐœ์ˆ˜ = n()) %>% 
  arrange(๊ตฌ๋ถ„, ์‹œ๋„) %>% 
  spread(key = "๊ตฌ๋ถ„", value = "๊ฐœ์ˆ˜") %>% 
  replace_na(list(๊ตญ๋ฏผ์•ˆ์‹ฌ๋ณ‘์› = 0)) 

m <- list(
  l = 50,
  r = 50,
  b = 100,
  t = 100,
  pad = 4
)

df_plotly %>% 
  plot_ly(
    x = ~์‹œ๋„,
    y = ~๊ตญ๋ฏผ์•ˆ์‹ฌ๋ณ‘์›,
    type = "bar",
    name = "๋ณด๊ฑด๋ณต์ง€๋ถ€ ๊ตญ๋ฏผ์•ˆ์‹ฌ๋ณ‘์›",
    textposition = 'auto',
    marker = list(color = 'green')
  ) %>% 
  add_trace(y = ~์„ ๋ณ„์ง„๋ฃŒ์†Œ,
            name = "๋ณด๊ฑด๋ณต์ง€๋ถ€ ์„ ๋ณ„์ง„๋ฃŒ์†Œ",
            textposition = 'auto',
            marker = list(color = 'blue')) %>% 
  layout(legend = list(orientation = "v",   # show entries horizontally
                       xanchor = "center",  # use center of legend as anchor
                       x = 0.9,
                       y = 1.09,
                       autosize = F, width = 200, height = 100, margin = m)) 
```

### ๋ฐ์ดํ„ฐ(์ „๊ตญ) {data-height=60%}

```{r}
hos_cli_info %>% 
  select(๊ตฌ๋ถ„, ์‹œ๋„, ์‹œ๊ตฐ๊ตฌ, ๊ธฐ๊ด€๋ช…, ์ฃผ์†Œ, ์œ ํ˜•, ์ „ํ™”๋ฒˆํ˜ธ, ์šด์˜๊ฐ€๋Šฅ์ผ์ž) %>% 
  datatable(rownames  = FALSE)
```

์„œ์šธ {data-navmenu="๋ณด๊ฑด๋ณต์ง€๋ถ€ ์•ˆ์‹ฌ๋ณ‘์›๊ณผ ์ง„๋ฃŒ์†Œ"}
=======================================================================

columns 1 {data-width=40%}
-----------------------------------------------------------------------

### ์„œ์šธ - ์•ˆ์‹ฌ๋ณ‘์›&์ง„๋ฃŒ์†Œ ์œ„์น˜์ง€๋„

```{r}
# ๋ณด๊ฑด๋ณต์ง€๋ถ€ ์•ˆ์‹ฌ๋ณ‘์›&์ง„๋ฃŒ์†Œ
hos_cli_leaflet <- hos_cli_info_seoul %>% 
  mutate(ratingcol = case_when(
    ๊ตฌ๋ถ„ == "๊ตญ๋ฏผ์•ˆ์‹ฌ๋ณ‘์›" ~ "green",
    TRUE ~ "blue"
  ))

leaflet() %>% 
  addTiles() %>% 
  addCircleMarkers(hos_cli_leaflet$long, 
                   hos_cli_leaflet$lat, 
                   color = hos_cli_leaflet$ratingcol, 
                   radius = 8, 
                   fill = T,
                   fillOpacity = 0.5,
                   opacity = 0.1,
                   popup = paste0(hos_cli_leaflet$๊ธฐ๊ด€๋ช…, " \n(", hos_cli_leaflet$์ „ํ™”๋ฒˆํ˜ธ,")")) %>%
  addLegend("topright", 
            colors = c("green","blue"),
            labels = c("๋ณด๊ฑด๋ณต์ง€๋ถ€ ๊ตญ๋ฏผ์•ˆ์‹ฌ๋ณ‘์›",
                       "๋ณด๊ฑด๋ณต์ง€๋ถ€ ์„ ๋ณ„์ง„๋ฃŒ์†Œ"), 
            opacity = 1)
```

columns 2 { data-width=60%, data-height=200}
-------------------------------------

### ์„œ์šธ - ๊ตฌ๋ณ„ ์•ˆ์‹ฌ๋ณ‘์› & ์ง„๋ฃŒ์†Œ ๊ทธ๋ž˜ํ”„ {data-height=40%}

```{r}
theme_set(theme_classic())

# hos_cli_info$๊ตฌ๋ถ„ <- as.factor(hos_cli_info$๊ตฌ๋ถ„)
# 
# p <- hos_cli_info %>% 
#   group_by(๊ตฌ๋ถ„,์‹œ๋„) %>% 
#   summarise(๊ฐœ์ˆ˜ = n()) %>% 
#   arrange(๊ตฌ๋ถ„, ์‹œ๋„) %>% 
#   ggplot(aes(x = ์‹œ๋„, y = ๊ฐœ์ˆ˜, fill = ๊ตฌ๋ถ„)) +
#   geom_col(position="dodge") +
#   scale_fill_manual(values = c("green3", "blue3")) +
#   labs(title = "์ง€์—ญ๋ณ„ ๋ณ‘์›, ์ง„๋ฃŒ์†Œ ํŒŒ์•…", x = "", y = "") +
#   theme(legend.position = "bottom")
# 
# 
# ggplotly(p)

df_plotly <- hos_cli_info_seoul %>% 
  group_by(๊ตฌ๋ถ„,์‹œ๊ตฐ๊ตฌ) %>% 
  summarise(๊ฐœ์ˆ˜ = n()) %>% 
  arrange(๊ตฌ๋ถ„, ์‹œ๊ตฐ๊ตฌ) %>% 
  spread(key = "๊ตฌ๋ถ„", value = "๊ฐœ์ˆ˜") %>% 
  replace_na(list(๊ตญ๋ฏผ์•ˆ์‹ฌ๋ณ‘์› = 0)) 

m <- list(
  l = 50,
  r = 50,
  b = 100,
  t = 100,
  pad = 4
)

df_plotly %>% 
  plot_ly(
    x = ~์‹œ๊ตฐ๊ตฌ,
    y = ~๊ตญ๋ฏผ์•ˆ์‹ฌ๋ณ‘์›,
    type = "bar",
    name = "๋ณด๊ฑด๋ณต์ง€๋ถ€ ๊ตญ๋ฏผ์•ˆ์‹ฌ๋ณ‘์›",
    textposition = 'auto',
    marker = list(color = 'green')
  ) %>% 
  add_trace(y = ~์„ ๋ณ„์ง„๋ฃŒ์†Œ,
            name = "๋ณด๊ฑด๋ณต์ง€๋ถ€ ์„ ๋ณ„์ง„๋ฃŒ์†Œ",
            textposition = 'auto',
            marker = list(color = 'blue')) %>% 
  layout(legend = list(orientation = "v",   # show entries horizontally
                       xanchor = "center",  # use center of legend as anchor
                       x = 0.9,
                       y = 1.09,
                       autosize = F, width = 200, height = 100, margin = m)) 
```

### ๋ฐ์ดํ„ฐ(์„œ์šธ) {data-height=60%}

```{r}
hos_cli_info_seoul %>% 
  select(๊ตฌ๋ถ„, ์‹œ๊ตฐ๊ตฌ, ๊ธฐ๊ด€๋ช…, ์ฃผ์†Œ, ์œ ํ˜•, ์ „ํ™”๋ฒˆํ˜ธ, ์šด์˜๊ฐ€๋Šฅ์ผ์ž) %>% 
  datatable(rownames  = FALSE)
```



๋Œ€๊ตฌ {data-navmenu="๋ณด๊ฑด๋ณต์ง€๋ถ€ ์•ˆ์‹ฌ๋ณ‘์›๊ณผ ์ง„๋ฃŒ์†Œ"}
=======================================================================

columns 1 {data-width=40%}
-----------------------------------------------------------------------

### ๋Œ€๊ตฌ - ์•ˆ์‹ฌ๋ณ‘์›&์ง„๋ฃŒ์†Œ ์œ„์น˜์ง€๋„

```{r}
# ๋ณด๊ฑด๋ณต์ง€๋ถ€ ์•ˆ์‹ฌ๋ณ‘์›&์ง„๋ฃŒ์†Œ
hos_cli_leaflet <- hos_cli_info_daegu %>% 
  mutate(ratingcol = case_when(
    ๊ตฌ๋ถ„ == "๊ตญ๋ฏผ์•ˆ์‹ฌ๋ณ‘์›" ~ "green",
    TRUE ~ "blue"
  ))

leaflet() %>% 
  addTiles() %>% 
  addCircleMarkers(hos_cli_leaflet$long, 
                   hos_cli_leaflet$lat, 
                   color = hos_cli_leaflet$ratingcol, 
                   radius = 8, 
                   fill = T,
                   fillOpacity = 0.5,
                   opacity = 0.1,
                   popup = paste0(hos_cli_leaflet$๊ธฐ๊ด€๋ช…, " \n(", hos_cli_leaflet$์ „ํ™”๋ฒˆํ˜ธ,")")) %>%
  addLegend("topright", 
            colors = c("green","blue"),
            labels = c("๋ณด๊ฑด๋ณต์ง€๋ถ€ ๊ตญ๋ฏผ์•ˆ์‹ฌ๋ณ‘์›",
                       "๋ณด๊ฑด๋ณต์ง€๋ถ€ ์„ ๋ณ„์ง„๋ฃŒ์†Œ"), 
            opacity = 1)
```

columns 2 { data-width=60%, data-height=200}
-------------------------------------

### ๋Œ€๊ตฌ - ๊ตฌ๋ณ„ ์•ˆ์‹ฌ๋ณ‘์› & ์ง„๋ฃŒ์†Œ ๊ทธ๋ž˜ํ”„ {data-height=40%}

```{r}
theme_set(theme_classic())

# hos_cli_info$๊ตฌ๋ถ„ <- as.factor(hos_cli_info$๊ตฌ๋ถ„)
# 
# p <- hos_cli_info %>% 
#   group_by(๊ตฌ๋ถ„,์‹œ๋„) %>% 
#   summarise(๊ฐœ์ˆ˜ = n()) %>% 
#   arrange(๊ตฌ๋ถ„, ์‹œ๋„) %>% 
#   ggplot(aes(x = ์‹œ๋„, y = ๊ฐœ์ˆ˜, fill = ๊ตฌ๋ถ„)) +
#   geom_col(position="dodge") +
#   scale_fill_manual(values = c("green3", "blue3")) +
#   labs(title = "์ง€์—ญ๋ณ„ ๋ณ‘์›, ์ง„๋ฃŒ์†Œ ํŒŒ์•…", x = "", y = "") +
#   theme(legend.position = "bottom")
# 
# 
# ggplotly(p)

df_plotly <- hos_cli_info_daegu %>% 
  group_by(๊ตฌ๋ถ„,์‹œ๊ตฐ๊ตฌ) %>% 
  summarise(๊ฐœ์ˆ˜ = n()) %>% 
  arrange(๊ตฌ๋ถ„, ์‹œ๊ตฐ๊ตฌ) %>% 
  spread(key = "๊ตฌ๋ถ„", value = "๊ฐœ์ˆ˜") %>% 
  replace_na(list(๊ตญ๋ฏผ์•ˆ์‹ฌ๋ณ‘์› = 0)) 

m <- list(
  l = 50,
  r = 50,
  b = 100,
  t = 100,
  pad = 4
)

df_plotly %>% 
  plot_ly(
    x = ~์‹œ๊ตฐ๊ตฌ,
    y = ~๊ตญ๋ฏผ์•ˆ์‹ฌ๋ณ‘์›,
    type = "bar",
    name = "๋ณด๊ฑด๋ณต์ง€๋ถ€ ๊ตญ๋ฏผ์•ˆ์‹ฌ๋ณ‘์›",
    textposition = 'auto',
    marker = list(color = 'green')
  ) %>% 
  add_trace(y = ~์„ ๋ณ„์ง„๋ฃŒ์†Œ,
            name = "๋ณด๊ฑด๋ณต์ง€๋ถ€ ์„ ๋ณ„์ง„๋ฃŒ์†Œ",
            textposition = 'auto',
            marker = list(color = 'blue')) %>% 
  layout(legend = list(orientation = "v",   # show entries horizontally
                       xanchor = "center",  # use center of legend as anchor
                       x = 0.9,
                       y = 1.09,
                       autosize = F, width = 200, height = 100, margin = m)) 
```

### ๋ฐ์ดํ„ฐ(๋Œ€๊ตฌ) {data-height=60%}

```{r}
hos_cli_info_daegu %>% 
  select(๊ตฌ๋ถ„, ์‹œ๊ตฐ๊ตฌ, ๊ธฐ๊ด€๋ช…, ์ฃผ์†Œ, ์œ ํ˜•, ์ „ํ™”๋ฒˆํ˜ธ, ์šด์˜๊ฐ€๋Šฅ์ผ์ž) %>% 
  datatable(rownames  = FALSE)
```



์ „๊ตญ {data-navmenu="๋งˆ์Šคํฌ ์ œ๊ณต ์ •๋ณด"}
=======================================================================

```{r}
# ๋งˆ์Šคํฌ ์ •๋ณด API ------------------------

# 1. ์ฃผ์†Œ/์ขŒํ‘œ ๊ธฐ์ค€ ํŒ๋งค์ฒ˜๋ณ„ ๊ณต์  ๋งˆ์Šคํฌ ํŒ๋งค์ •๋ณด ์ œ๊ณต ์„œ๋น„์Šค
url_1 <- "https://8oi9s0nnth.apigw.ntruss.com/corona19-masks/v1/stores/json?page=1&perPage=5000"
# 2. ํŒ๋งค์ฒ˜๋ณ„ ๊ณต์  ๋งˆ์Šคํฌ ํŒ๋งค์ฒ˜ ์ •๋ณด ์ œ๊ณต ์„œ๋น„์Šค
url_2 <- "https://8oi9s0nnth.apigw.ntruss.com/corona19-masks/v1/sales/json?page=1&perPage=5000"


file_1 <- fromJSON(url_1)
file_2 <- fromJSON(url_2)

# 1๋ฒˆ
df_1 <- file_1$storeInfos %>%
  as_tibble()

# 2๋ฒˆ
df_2 <- file_2$sales %>%
  as_tibble()


asdf <- df_1 %>%
  left_join(df_2) %>%
  # replace_na(list(created_at = "์ •๋ณด ์—†์Œ", remain_stat = "์ •๋ณด์—†์Œ", stock_at = "์ •๋ณด ์—†์Œ")) %>%
  drop_na() %>%
  mutate(remain_stat_kr = case_when(
    remain_stat == "plenty" ~ "100๊ฐœ ์ด์ƒ",
    remain_stat == "some" ~ "30๊ฐœ ์ด์ƒ 100๊ฐœ ๋ฏธ๋งŒ",
    remain_stat == "few" ~ "2๊ฐœ ์ด์ƒ 30๊ฐœ ๋ฏธ๋งŒ",
    remain_stat == "empty" ~ "1๊ฐœ ์ดํ•˜",
    remain_stat == "break" ~ "2๊ฐœ ์ด์ƒ 30๊ฐœ ๋ฏธ๋งŒ",
    TRUE ~ "์ •๋ณด ์—†์Œ"
  ), ๊ตฌ๋ถ„ = case_when(
    type == "01" ~ "์•ฝ๊ตญ",
    type == "02" ~ "์šฐ์ฒด๊ตญ",
    TRUE ~ "๋†ํ˜‘"
  ))



lng_lat_list <- map_chr(1:nrow(asdf), function(x) {
  glue("x={asdf[x,4]}&y={asdf[x,3]}")
})


# ์œ„๊ฒฝ๋„๋ฅผ ์‚ฌ์šฉํ•œ ์ง€๋ฆฌ์ •๋ณด ์ถ”์ถœ
addr_fun <- function(lng_lat) {
  data_list <- GET(url = glue("https://dapi.kakao.com/v2/local/geo/coord2regioncode.json?{lng_lat}"),
                   # query = list(query = addr),
                   add_headers(Authorization = paste0("KakaoAK ", "2ecacbafd523802f293b245103346b06"))) %>%
    content(as = 'text') %>%
    fromJSON()

  lng_processing <- lng_lat %>%
    str_split("&")

  raw_lng <- lng_processing[[1]][1] %>%
    str_sub(3,max(nchar(lng_processing[[1]][1]))) %>%
    as.numeric()

  addr_df <- tibble(
    # lng = data_list$documents$x[1],
    lng = raw_lng,
    ์‹œ๋„ = data_list$documents$region_1depth_name[1],
    ์‹œ๊ตฐ๊ตฌ = data_list$documents$region_2depth_name[1],
    ๋™ = data_list$documents$region_3depth_name[1])

  return(addr_df)
}


addr_info <- map_dfr(lng_lat_list , function(x) {
  addr_fun(x)
})



asdf_2 <- asdf %>%
  left_join(addr_info)
# 
# 
# write.csv(asdf_2, file = "mask_df.csv")

# asdf_2 <- read_csv("mask_df.csv")

asdf <- asdf_2

asdf_seoul <- asdf_2 %>% 
  filter(์‹œ๋„ == "์„œ์šธํŠน๋ณ„์‹œ")

asdf_daegu <- asdf_2 %>% 
  filter(์‹œ๋„ == "๋Œ€๊ตฌ๊ด‘์—ญ์‹œ")
```


Colunms 1  {data-width=40%}
-----------------------------------------------------------------------

### ์ „๊ตญ ๋งˆ์Šคํฌ ์ œ๊ณต ์œ„์น˜์ง€๋„

```{r}
# ๋งˆ์Šคํฌ ํŒ๋งค์ฒ˜ leaflet ------------------------

asdf_leaflet <- asdf %>% 
  mutate(ratingcol = case_when(
    type == "01" & remain_stat != "์ •๋ณด์—†์Œ" ~ "green",
    type == "02" & remain_stat != "์ •๋ณด์—†์Œ"~ "orange",
    type == "03" & remain_stat != "์ •๋ณด์—†์Œ"~ "blue",
    TRUE ~ "black"
  ))


leaflet() %>% 
  addTiles() %>% 
  addCircleMarkers(asdf_leaflet$lng, 
                   asdf_leaflet$lat, 
                   color = asdf_leaflet$ratingcol, 
                   radius = 8, 
                   fill = T,
                   fillOpacity = 0.6,
                   opacity = 0.1,
                   popup = paste0(asdf_leaflet$name," (",asdf_leaflet$addr, "(",asdf_leaflet$remain_stat_kr,"))")) %>%
  addLegend("topright", 
            colors = c("green","orange","blue"),
            labels = c("์•ฝ๊ตญ",
                       "์šฐ์ฒด๊ตญ",
                       "๋†ํ˜‘"), 
            opacity = 1)
```

Colunms 2 { data-width=60%, data-height=200}
-----------------------------------------------------------------------

### ์‹œ๋„๋ณ„ ๋งˆ์Šคํฌ ์ œ๊ณต ๊ธฐ๊ด€ ๊ทธ๋ž˜ํ”„ {data-height=40%}

```{r}
df_plotly <- asdf_2 %>% 
  group_by(๊ตฌ๋ถ„,์‹œ๋„) %>% 
  summarise(๊ฐœ์ˆ˜ = n()) %>% 
  arrange(๊ตฌ๋ถ„, ์‹œ๋„) %>% 
  spread(key = "๊ตฌ๋ถ„", value = "๊ฐœ์ˆ˜") %>% 
  replace_na(list(๋†ํ˜‘ = 0, ์•ฝ๊ตญ = 0, ์šฐ์ฒด๊ตญ=0)) 



df_plotly %>% 
  plot_ly(
    x = ~์‹œ๋„,
    y = ~์•ฝ๊ตญ,
    type = "bar",
    name = "์•ฝ๊ตญ",
    textposition = 'auto',
    marker = list(color = 'green')
  ) %>% 
  add_trace(y = ~์šฐ์ฒด๊ตญ,
            name = "์šฐ์ฒด๊ตญ",
            textposition = 'auto',
            marker = list(color = 'orange')) %>% 
  add_trace(y = ~๋†ํ˜‘,
            name = "๋†ํ˜‘",
            textposition = 'auto',
            marker = list(color = 'blue')) %>% 
  layout(legend = list(orientation = "v",   # show entries horizontally
                       xanchor = "center",  # use center of legend as anchor
                       x = 0.9,
                       y = 1.09)) 
```

### ๋ฐ์ดํ„ฐ(์ „๊ตญ) {data-height=60%}

```{r}
asdf_2 %>% 
  select(๊ตฌ๋ถ„, ์‹œ๋„, ์‹œ๊ตฐ๊ตฌ, name,addr, created_at, stock_at, remain_stat_kr) %>% 
  rename(๊ธฐ๊ด€๋ช… = name, ์ฃผ์†Œ = addr, ์ž…๊ณ ์ผ์‹œ = stock_at, ์ƒ์„ฑ์ผ์‹œ = created_at, ์žฌ๊ณ ์ •๋ณด = remain_stat_kr) %>% 
  datatable(rownames  = FALSE)
```

์„œ์šธ {data-navmenu="๋งˆ์Šคํฌ ์ œ๊ณต ์ •๋ณด"}
=======================================================================

columns 1 {data-width=40%}
-----------------------------------------------------------------------

### ์„œ์šธ ๋งˆ์Šคํฌ ์ œ๊ณต ์œ„์น˜์ง€๋„

```{r}
# ๋งˆ์Šคํฌ ํŒ๋งค์ฒ˜ leaflet ------------------------
asdf_leaflet <- asdf_seoul %>% 
  mutate(ratingcol = case_when(
    type == "01" & remain_stat != "์ •๋ณด์—†์Œ" ~ "green",
    type == "02" & remain_stat != "์ •๋ณด์—†์Œ"~ "orange",
    type == "03" & remain_stat != "์ •๋ณด์—†์Œ"~ "blue",
    TRUE ~ "black"
  ))


leaflet() %>% 
  addTiles() %>% 
  addCircleMarkers(asdf_leaflet$lng, 
                   asdf_leaflet$lat, 
                   color = asdf_leaflet$ratingcol, 
                   radius = 8, 
                   fill = T,
                   fillOpacity = 0.6,
                   opacity = 0.1,
                   popup = paste0(asdf_leaflet$name," (",asdf_leaflet$addr, "(",asdf_leaflet$remain_stat_kr,"))")) %>%
  addLegend("topright", 
            colors = c("green","orange","blue"),
            labels = c("์•ฝ๊ตญ",
                       "์šฐ์ฒด๊ตญ",
                       "๋†ํ˜‘"), 
            opacity = 1)
```

columns 2 { data-width=60%, data-height=200}
-------------------------------------

### ์„œ์šธ ๊ตฌ๋ณ„ ๋งˆ์Šคํฌ ์ œ๊ณต ๊ธฐ๊ด€ ๊ทธ๋ž˜ํ”„ {data-height=40%} 

```{r}
df_plotly <- asdf_seoul %>% 
  group_by(๊ตฌ๋ถ„,์‹œ๊ตฐ๊ตฌ) %>% 
  summarise(๊ฐœ์ˆ˜ = n()) %>% 
  arrange(๊ตฌ๋ถ„, ์‹œ๊ตฐ๊ตฌ) %>% 
  spread(key = "๊ตฌ๋ถ„", value = "๊ฐœ์ˆ˜") %>% 
  replace_na(list(๋†ํ˜‘ = 0, ์•ฝ๊ตญ = 0, ์šฐ์ฒด๊ตญ=0)) 

df_plotly <- df_plotly %>% 
  mutate("๋†ํ˜‘" = 0,
         "์šฐ์ฒด๊ตญ" = 0) 


df_plotly %>% 
  plot_ly(
    x = ~์‹œ๊ตฐ๊ตฌ,
    y = ~์•ฝ๊ตญ,
    type = "bar",
    name = "์•ฝ๊ตญ",
    textposition = 'auto',
    marker = list(color = 'green')
  ) %>% 
  add_trace(y = ~์šฐ์ฒด๊ตญ,
            name = "์šฐ์ฒด๊ตญ",
            textposition = 'auto',
            marker = list(color = 'orange')) %>% 
  add_trace(y = ~๋†ํ˜‘,
            name = "๋†ํ˜‘",
            textposition = 'auto',
            marker = list(color = 'blue')) %>% 
  layout(legend = list(orientation = "v",   # show entries horizontally
                       xanchor = "center",  # use center of legend as anchor
                       x = 0.9,
                       y = 1.09)) 
```

### ๋ฐ์ดํ„ฐ(์„œ์šธ) {data-height=60%}

```{r}
asdf_seoul %>% 
  select(๊ตฌ๋ถ„, ์‹œ๋„, ์‹œ๊ตฐ๊ตฌ, name,addr, created_at, stock_at, remain_stat_kr) %>% 
  rename(๊ธฐ๊ด€๋ช… = name, ์ฃผ์†Œ = addr, ์ž…๊ณ ์ผ์‹œ = stock_at, ์ƒ์„ฑ์ผ์‹œ = created_at, ์žฌ๊ณ ์ •๋ณด = remain_stat_kr) %>% 
  datatable(rownames  = FALSE)
```





๋Œ€๊ตฌ {data-navmenu="๋งˆ์Šคํฌ ์ œ๊ณต ์ •๋ณด"}
=======================================================================

columns 1 {data-width=40%}
-----------------------------------------------------------------------

### ๋Œ€๊ตฌ ๋งˆ์Šคํฌ ์ œ๊ณต ์œ„์น˜์ง€๋„

```{r}
# ๋งˆ์Šคํฌ ํŒ๋งค์ฒ˜ leaflet ------------------------
asdf_leaflet <- asdf_daegu %>% 
  mutate(ratingcol = case_when(
    type == "01" & remain_stat != "์ •๋ณด์—†์Œ" ~ "green",
    type == "02" & remain_stat != "์ •๋ณด์—†์Œ"~ "orange",
    type == "03" & remain_stat != "์ •๋ณด์—†์Œ"~ "blue",
    TRUE ~ "black"
  ))


leaflet() %>% 
  addTiles() %>% 
  addCircleMarkers(asdf_leaflet$lng, 
                   asdf_leaflet$lat, 
                   color = asdf_leaflet$ratingcol, 
                   radius = 8, 
                   fill = T,
                   fillOpacity = 0.6,
                   opacity = 0.1,
                   popup = paste0(asdf_leaflet$name," (",asdf_leaflet$addr, "(",asdf_leaflet$remain_stat_kr,"))")) %>%
  addLegend("topright", 
            colors = c("green","orange","blue"),
            labels = c("์•ฝ๊ตญ",
                       "์šฐ์ฒด๊ตญ",
                       "๋†ํ˜‘"), 
            opacity = 1)
```

columns 2 { data-width=60%, data-height=200}
-------------------------------------

### ๋Œ€๊ตฌ ๊ตฌ๋ณ„ ๋งˆ์Šคํฌ ์ œ๊ณต ๊ธฐ๊ด€ ๊ทธ๋ž˜ํ”„ {data-height=40%}

```{r}
df_plotly <- asdf_daegu %>% 
  group_by(๊ตฌ๋ถ„,์‹œ๊ตฐ๊ตฌ) %>% 
  summarise(๊ฐœ์ˆ˜ = n()) %>% 
  arrange(๊ตฌ๋ถ„, ์‹œ๊ตฐ๊ตฌ) %>% 
  spread(key = "๊ตฌ๋ถ„", value = "๊ฐœ์ˆ˜") %>% 
  replace_na(list(๋†ํ˜‘ = 0, ์•ฝ๊ตญ = 0, ์šฐ์ฒด๊ตญ=0)) 

df_plotly <- df_plotly %>% 
  mutate("๋†ํ˜‘" = 0) 

df_plotly %>% 
  plot_ly(
    x = ~์‹œ๊ตฐ๊ตฌ,
    y = ~์•ฝ๊ตญ,
    type = "bar",
    name = "์•ฝ๊ตญ",
    textposition = 'auto',
    marker = list(color = 'green')
  ) %>% 
  add_trace(y = ~์šฐ์ฒด๊ตญ,
            name = "์šฐ์ฒด๊ตญ",
            textposition = 'auto',
            marker = list(color = 'orange')) %>% 
  add_trace(y = ~๋†ํ˜‘,
            name = "๋†ํ˜‘",
            textposition = 'auto',
            marker = list(color = 'blue')) %>% 
  layout(legend = list(orientation = "v",   # show entries horizontally
                       xanchor = "center",  # use center of legend as anchor
                       x = 0.9,
                       y = 1.09)) 
```

### ๋ฐ์ดํ„ฐ(๋Œ€๊ตฌ) {data-height=60%}

```{r}
asdf_daegu %>% 
  select(๊ตฌ๋ถ„, ์‹œ๋„, ์‹œ๊ตฐ๊ตฌ, name,addr, created_at, stock_at, remain_stat_kr) %>% 
  rename(๊ธฐ๊ด€๋ช… = name, ์ฃผ์†Œ = addr, ์ž…๊ณ ์ผ์‹œ = stock_at, ์ƒ์„ฑ์ผ์‹œ = created_at, ์žฌ๊ณ ์ •๋ณด = remain_stat_kr) %>% 
  datatable(rownames  = FALSE)
```



Reference
=======================================================================
**0. ๋ฐ์ดํ„ฐ **

- ๊ณต๊ณต๋ฐ์ดํ„ฐ ํฌํ„ธ - ์ฝ”๋กœ๋‚˜ : https://www.data.go.kr/issues/3/show.do

**1. Flexdashboard ์ฐธ๊ณ  ์ž๋ฃŒ**

- Flexdashboard ํŠœํ† ๋ฆฌ์–ผ: https://rmarkdown.rstudio.com/flexdashboard/index.html
- ํ…Œ๋งˆ: http://www.sangerw.com/blog/flexdashboardthemes/#IDreadable
- Flexdashboard CRAN: https://cran.r-project.org/web/packages/flexdashboard/flexdashboard.pdf
- bookdown in Flexdashboard: https://bookdown.org/yihui/rmarkdown/dashboards.html
- Making Dashboards in R Markdown: https://arm.rbind.io/slides/flexdashboard.html

**1-1. Flexdashboard ์˜ˆ์ œ**

- ์˜ˆ์ œ 1: https://beta.rstudioconnect.com/juliasilge/policeshooting/policeshooting.html
  - ์˜ˆ์ œ์ฝ”๋“œ: https://gist.github.com/juliasilge/9acbe97c549502bac85404779edceba0
- ์˜ˆ์ œ 2: https://jenthompson.me/examples/progressdash.html
  - ์˜ˆ์ œ์ฝ”๋“œ: https://github.com/jenniferthompson/MOSAICProgress/blob/master/progressdash.Rmd
- ์˜ˆ์ œ 3: https://jenthompson.me/examples/insight_progress.html
  - ์˜ˆ์ œ์ฝ”๋“œ: https://github.com/jenniferthompson/INSIGHTProgress/blob/master/insight_progress.Rmd
- ์˜ˆ์ œ 4: https://tonyelhabr.rbind.io/files/nba-tms.html

**2. leaflet ์ฐธ๊ณ ์ž๋ฃŒ**

- https://rstudio.github.io/leaflet/
- https://leafletjs.com/examples.html
- https://rstudio.github.io/leaflet/

**3. ๋Œ€์‹œ๋ณด๋“œ UI ์ฐธ๊ณ ์ž๋ฃŒ**

- http://ncov.mohw.go.kr/
- http://www.bix5.net/examples

**4. ์œ„๊ฒฝ๋„๋กœ ํ–‰์ •๊ตฌ์—ญ ์ถ”์ถœ ์ฐธ๊ณ ์ž๋ฃŒ**

- https://developers.kakao.com/docs/restapi/local#์ขŒํ‘œ-ํ–‰์ •๊ตฌ์—ญ์ •๋ณด-๋ณ€ํ™˜


**5. ์•„์ด์ฝ˜ List**

- https://fontawesome.com/v4.7.0/icons/