File size: 928 Bytes
57537fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env Rscript

url <- "https://www.data.jma.go.jp/obd/stats/etrn/view/available_table.php?prec_no=44&block_no=47662"

suppressMessages({
  if (!requireNamespace("rvest", quietly = TRUE) ||
      !requireNamespace("xml2", quietly = TRUE)) {
    stop("Missing packages: please install 'rvest' and 'xml2'.")
  }
})

doc <- xml2::read_html(url)
tables <- rvest::html_table(doc, fill = TRUE)

start_years <- c()

for (tbl in tables) {
  if (nrow(tbl) == 0) {
    next
  }

  name_norm <- tolower(gsub("\\s+", " ", names(tbl)))
  start_cols <- which(grepl("start", name_norm))

  if (length(start_cols) == 0) {
    next
  }

  for (idx in start_cols) {
    values <- suppressWarnings(as.numeric(gsub("[^0-9]", "", tbl[[idx]])))
    values <- values[!is.na(values)]
    start_years <- c(start_years, values)
  }
}

if (length(start_years) == 0) {
  stop("No start year values found in tables.")
}

cat(min(start_years), "\n")