fix: Improve data fetching reliability using `httr` with timeout and stabilize UI tab switching with direct JavaScript.
Browse files- funs/scraper.R +12 -1
- server.R +5 -4
funs/scraper.R
CHANGED
|
@@ -2,6 +2,7 @@ library(rvest)
|
|
| 2 |
library(dplyr)
|
| 3 |
library(stringr)
|
| 4 |
library(readr)
|
|
|
|
| 5 |
|
| 6 |
#' Scrape JMA Data (Daily, Hourly, 10-Minute, Monthly)
|
| 7 |
#'
|
|
@@ -43,8 +44,18 @@ get_jma_data <- function(block_no, year, month, day = NULL, prec_no, type = "s1"
|
|
| 43 |
|
| 44 |
message(sprintf("Downloading %s data from: %s", resolution, url))
|
| 45 |
|
|
|
|
|
|
|
|
|
|
| 46 |
page <- tryCatch(
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
error = function(e) {
|
| 49 |
warning("Failed to download data: ", e)
|
| 50 |
return(NULL)
|
|
|
|
| 2 |
library(dplyr)
|
| 3 |
library(stringr)
|
| 4 |
library(readr)
|
| 5 |
+
library(httr)
|
| 6 |
|
| 7 |
#' Scrape JMA Data (Daily, Hourly, 10-Minute, Monthly)
|
| 8 |
#'
|
|
|
|
| 44 |
|
| 45 |
message(sprintf("Downloading %s data from: %s", resolution, url))
|
| 46 |
|
| 47 |
+
message(sprintf("Downloading %s data from: %s", resolution, url))
|
| 48 |
+
|
| 49 |
+
# Use httr::GET with timeout to prevent hanging
|
| 50 |
page <- tryCatch(
|
| 51 |
+
{
|
| 52 |
+
resp <- httr::GET(url, httr::timeout(10))
|
| 53 |
+
if (httr::status_code(resp) != 200) {
|
| 54 |
+
warning(sprintf("Failed to download data: HTTP %s", httr::status_code(resp)))
|
| 55 |
+
return(NULL)
|
| 56 |
+
}
|
| 57 |
+
read_html(resp)
|
| 58 |
+
},
|
| 59 |
error = function(e) {
|
| 60 |
warning("Failed to download data: ", e)
|
| 61 |
return(NULL)
|
server.R
CHANGED
|
@@ -121,9 +121,10 @@ server <- function(input, output, session) {
|
|
| 121 |
} else if (view %in% c("dashboard-plots", "dashboard-data")) {
|
| 122 |
session$sendCustomMessage("switchTab", list(tabId = "Dashboard"))
|
| 123 |
if (view == "dashboard-data") {
|
| 124 |
-
shinyjs::delay(
|
| 125 |
-
|
| 126 |
-
|
|
|
|
| 127 |
})
|
| 128 |
}
|
| 129 |
}
|
|
@@ -704,7 +705,7 @@ server <- function(input, output, session) {
|
|
| 704 |
fetch_queue_idx(idx + 1)
|
| 705 |
fetch_stage(2)
|
| 706 |
})
|
| 707 |
-
}, 0.
|
| 708 |
fetch_stage(-1)
|
| 709 |
})
|
| 710 |
|
|
|
|
| 121 |
} else if (view %in% c("dashboard-plots", "dashboard-data")) {
|
| 122 |
session$sendCustomMessage("switchTab", list(tabId = "Dashboard"))
|
| 123 |
if (view == "dashboard-data") {
|
| 124 |
+
shinyjs::delay(800, {
|
| 125 |
+
# Click the tab directly; assumes id="dashboard_subtabs" -> li a[data-value="Data"]
|
| 126 |
+
# Use robust JS to switch tab as updateNavsetCardPill might fail during init
|
| 127 |
+
shinyjs::runjs("$('a[data-value=\"Data\"]').tab('show');")
|
| 128 |
})
|
| 129 |
}
|
| 130 |
}
|
|
|
|
| 705 |
fetch_queue_idx(idx + 1)
|
| 706 |
fetch_stage(2)
|
| 707 |
})
|
| 708 |
+
}, 0.5)
|
| 709 |
fetch_stage(-1)
|
| 710 |
})
|
| 711 |
|