Skip to content

Instantly share code, notes, and snippets.

@kazutan
Created August 25, 2015 08:07
Show Gist options
  • Save kazutan/3adf209d09abd3a63d75 to your computer and use it in GitHub Desktop.
Save kazutan/3adf209d09abd3a63d75 to your computer and use it in GitHub Desktop.
# 台風情報
library("dplyr")
library("rvest")
library("stringr")
library("leaflet")
trg <- html("http://www.jma.go.jp/jp/typh/") %>% html_table
tbl1 <- trg[[4]] # テーブルは4番目以降が台風に関するもので今回は台風15号と16号があるので4を指定
res <- data.frame(
timing = tbl1$X1 %>% str_subset("[0-9][0-9]日[0-9][0-9]時"),
lat = tbl1$X2 %>%
str_subset("北緯") %>%
str_extract("[0-9][0-9]\\.[0-9]度") %>%
str_replace("度","") %>%
as.numeric,
lon = tbl1$X2 %>%
str_subset("東経") %>%
str_extract("[0-9][0-9][0-9]\\.[0-9]度") %>%
str_replace("度","") %>%
as.numeric
)
m <- leaflet::leaflet(res) %>%
addTiles() %>%
setView(lng = 139.0000, lat = 35.0000, zoom = 4) %>%
addPolylines(~lon,~lat,color="red") %>%
addCircleMarkers(
radius = 4,
color = "red", fillOpacity = 0.8,
stroke = FALSE, popup = ~timing
)
m
@kohske
Copy link

kohske commented Aug 25, 2015

予報円に改造してみた。

res <- data.frame(
  timing = tbl1$X1 %>% str_subset("[0-9][0-9]日[0-9][0-9]時"),
  lat = tbl1$X2 %>% 
    str_subset("北緯") %>%
    str_extract("[0-9][0-9]\\.[0-9]度") %>%
    str_replace("","") %>%
    as.numeric,
  lon = tbl1$X2 %>%
    str_subset("東経") %>%
    str_extract("[0-9][0-9][0-9]\\.[0-9]度") %>%
    str_replace("","") %>%
    as.numeric,
  yohouen = tbl1$X2 %>%
    str_subset("^[0-9]+km") %>%
    str_extract("^[0-9]+") %>%
    as.numeric %>%
    `*`(1000) %>%
    {c(0, .)}
)

m <- leaflet::leaflet(res) %>% 
  addTiles() %>% 
  setView(lng = 139.0000, lat = 35.0000, zoom = 4) %>% 
  addPolylines(~lon,~lat,color="red") %>% 
  addCircles(
    radius = ~yohouen,
    color = "red", popup = ~timing
  )
m

久々にleafletみたらめっちゃ進化してた

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment