R을 이용한 leaflet 시각화 및 마크다운 생성

작성자

1 요구사항

  • R을 이용한 leaflet 시각화 및 마크다운 생성
  • 공공데이터포털을 비롯한 다양한 데이터 제공 사이트에서 데이터를 2개 이상 사용하시오.
  • 반드시 출처를 표기하시오.

2 R 프로그래밍을 위한 초기 환경변수 설정

#================================================
# 초기 환경변수 설정
#================================================
env = "local"   # 로컬 : 원도우 환경, 작업환경 (현재 소스 코드 환경 시 .) 설정
# env = "dev"   # 개발 : 원도우 환경, 작업환경 (사용자 환경 시 contextPath) 설정
# env = "oper"  # 운영 : 리눅스 환경, 작업환경 (사용자 환경 시 contextPath) 설정

prjName = "test"
serviceName = "LSH0294"
contextPath = ifelse(env == "local", ".", "E:/04. TalentPlatform/Github/TalentPlatform-R")

if (env == "local") {
  globalVar = list(
    "inpPath" = contextPath
    , "figPath" = contextPath
    , "outPath" = contextPath
    , "tmpPath" = contextPath
    , "logPath" = contextPath
  )
} else {
  source(here::here(file.path(contextPath, "src"), "InitConfig.R"), encoding = "UTF-8")
}

3 비즈니스 로직 수행

3.1 라이브러리 읽기

#================================================
# 비즈니스 로직 수행
#================================================
# 라이브러리 읽기
library(tidyverse)
library(leaflet)
library(jsonlite)
library(RCurl)
library(readr)



3.2 한국 COVID-19 데이터 읽기

  • 한국의 COVID-19 확진자 발생 정보를 지도에 표시할 것
  • 데이터 URL : https://bit.ly/2SoEaYF
  • 데이터 출처 : 코로나 확진자 데이터 (구글 드라이브)
# 데이터 읽기
covid_case <- read.csv("https://bit.ly/2SoEaYF")
head(covid_case)
##   case_id province         city group              infection_case confirmed
## 1 1000001    Seoul   Yongsan-gu  TRUE               Itaewon Clubs       139
## 2 1000002    Seoul    Gwanak-gu  TRUE                     Richway       119
## 3 1000003    Seoul      Guro-gu  TRUE         Guro-gu Call Center        95
## 4 1000004    Seoul Yangcheon-gu  TRUE Yangcheon Table Tennis Club        43
## 5 1000005    Seoul    Dobong-gu  TRUE             Day Care Center        43
## 6 1000006    Seoul      Guro-gu  TRUE       Manmin Central Church        41
##    latitude  longitude
## 1 37.538621 126.992652
## 2  37.48208 126.901384
## 3 37.508163 126.884387
## 4 37.546061 126.874209
## 5 37.679422 127.044374
## 6 37.481059 126.894343
covidData = covid_case %>% 
  dplyr::filter(
    ! latitude == "-"
    , ! latitude == "-"
  ) %>% 
  readr::type_convert()

summary(covidData)
##     case_id          province             city            group        
##  Min.   :1000001   Length:65          Length:65          Mode:logical  
##  1st Qu.:1000024   Class :character   Class :character   TRUE:65       
##  Median :1300001   Mode  :character   Mode  :character                 
##  Mean   :2446162                                                       
##  3rd Qu.:4000001                                                       
##  Max.   :6100007                                                       
##  infection_case       confirmed         latitude       longitude    
##  Length:65          Min.   :   0.0   Min.   :35.08   Min.   :126.3  
##  Class :character   1st Qu.:   6.0   1st Qu.:35.85   1st Qu.:127.0  
##  Mode  :character   Median :  13.0   Median :37.00   Median :127.1  
##                     Mean   : 100.3   Mean   :36.69   Mean   :127.6  
##                     3rd Qu.:  41.0   3rd Qu.:37.52   3rd Qu.:128.5  
##                     Max.   :4511.0   Max.   :37.76   Max.   :129.1
covidVis = leaflet::leaflet(data = covidData) %>% 
  leaflet::addTiles() %>% 
  leaflet::addMarkers(lng = ~longitude, lat = ~latitude, popup = ~city, label = ~city) %>% 
  leaflet::addCircles(lng = ~longitude, lat = ~latitude, weight = 1, radius = ~sqrt(confirmed) * 500, popup = ~city)

covidVis



3.3 경기도 동두천시 부동산중개업 현황

jsonFile = RCurl::getURL("https://api.odcloud.kr/api/3080844/v1/uddi:da6b34a9-aa4e-4138-9476-93fbd9c81f15?page=1&perPage=99999&serviceKey=bf9fH0KLgr65zXKT5D%2FdcgUBIj1znJKnUPrzDVZEe6g4gquylOjmt65R5cjivLPfOKXWcRcAWU0SN7KKXBGDKA%3D%3D")
jsonData = jsonlite::fromJSON(jsonFile)
realEstateData = jsonData$data

realEstateDataL1 = realEstateData %>% 
  readr::type_convert()

realEstateVis = leaflet::leaflet(data = realEstateDataL1) %>% 
  leaflet::addTiles() %>% 
  leaflet::addMarkers(lng = ~경도, lat = ~위도, popup = ~사무소명, label = ~사무소명) %>% 
  leaflet::addCircles(lng = ~경도, lat = ~위도, weight = 1, radius = 50, popup = ~사무소명)

realEstateVis