阿好伯
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
1
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
--- disqus: ahb0222 GA : G-CQ4L16KHK4 --- # 使用R(RStudio Cloud)語言擷取觀測資料查詢系統氣象資料 > [color=#40f1ef][name=LHB阿好伯, 2020/02/09][:earth_africa:](https://www.facebook.com/LHB0222/) ###### tags: `R` [TOC] 氣象局有一個[觀測資料查詢系統](https://e-service.cwb.gov.tw/HistoryDataQuery/)可以查詢各個氣象站的資料 剛好最近有學弟需要近兩年大寮測站的降雨資料 若是手動查詢則需要大量時間 所以我就幫忙寫了一段程式幫忙取得兩年間的降雨資料 以下是相關的程式碼 分享給大家 目前遇到一個問題在以google sheet作為資料庫中 DT::datatable中filter只能在前兩欄起作用 mutate新增的第三欄無法使用filter 若是將資料調換過來也是mutate新增的欄位沒有效果 ![](https://i.imgur.com/ewkB55l.png) # 抓取資料並輸出成CSV檔 ```rust= packages <- c("jsonlite", "rvest", "magrittr", "lubridate") installed_packages <- packages %in% rownames(installed.packages()) if (any(installed_packages == FALSE)) { install.packages(packages[!installed_packages]) } # 載入所需套件 lapply(packages, library, character.only = TRUE) %>% invisible() # url <- "https://e-service.cwb.gov.tw/HistoryDataQuery/DayDataController.do?command=viewMain&station=C0V730&stname=%25E5%25A4%25A7%25E5%25AF%25AE&datepicker=2020-07-09#" url_start <- "https://e-service.cwb.gov.tw/HistoryDataQuery/DayDataController.do?command=viewMain&station=C0V730&stname=%25E5%25A4%25A7%25E5%25AF%25AE&datepicker=" start_date <- ymd("2018-07-01") end_date <- ymd("2020-07-31") end_date - start_date data1 <- data.frame() data2 <- data.frame() for(d in c(0:(end_date - start_date))){ url <- paste0(url_start,as.character(start_date + d),"#") #時間資料 time_H <- url %>% read_html() %>% html_nodes(xpath='//*[(@id = "MyTable")]//td[(((count(preceding-sibling::*) + 1) = 1) and parent::*)]') %>% html_text(trim = T) %>% as.numeric() #降雨資料 population <- url %>% read_html() %>% html_nodes(xpath='//td[(((count(preceding-sibling::*) + 1) = 11) and parent::*)]') %>% html_text(trim = T) %>% gsub("X","-9999", .) %>% as.numeric() data1 <- cbind(as.character(start_date + d), time_H, population) data2 <- rbind(data2, data1) write.csv(data1,file = paste0("C:/R/Data/", start_date + d, ".csv")) } write.csv(data2,file = paste0("C:/R/AllData.csv")) ``` # APP版本 ```r= # This is a Shiny web application. You can run the application by clicking # the 'Run App' button above. # # Find out more about building applications with Shiny here: # # http://shiny.rstudio.com/ # #packages <- c("jsonlite", "rvest", "magrittr", "lubridate") #installed_packages <- packages %in% rownames(installed.packages()) #if (any(installed_packages == FALSE)) { # install.packages(packages[!installed_packages]) #} # 載入所需套件 #invisible(lapply(packages, library, character.only = TRUE)) library("jsonlite") library("rvest") library("magrittr") library("lubridate") library("shiny") HistoryData <- function(start_date,end_date,Error_replaced){ url <- "https://e-service.cwb.gov.tw/HistoryDataQuery/DayDataController.do?command=viewMain&station=C0V730&stname=%25E5%25A4%25A7%25E5%25AF%25AE&datepicker=2020-07-09#" url_start <- "https://e-service.cwb.gov.tw/HistoryDataQuery/DayDataController.do?command=viewMain&station=C0V730&stname=%25E5%25A4%25A7%25E5%25AF%25AE&datepicker=" end_date - start_date data1 <- data.frame() data2 <- data.frame() for(d in c(0:(end_date - start_date))){ url <- paste0(url_start,as.character(start_date + d),"#") #時間資料 time_H <- url %>% read_html() %>% html_nodes(xpath='//*[(@id = "MyTable")]//td[(((count(preceding-sibling::*) + 1) = 1) and parent::*)]') %>% html_text(trim = T) %>% as.numeric() #降雨資料 population <- url %>% read_html() %>% html_nodes(xpath='//td[(((count(preceding-sibling::*) + 1) = 11) and parent::*)]') %>% html_text(trim = T) %>% gsub("X",Error_replaced, .) %>% as.numeric() #data1 <- cbind(as.character(start_date + d), time_H, population) data3 <- cbind(as.character(start_date + d),sum(population)) data2 <- rbind(data3, data2) #print(paste(as.character(start_date + d),data3)) #write.csv(data3,file = paste0("C:/R/Data/", start_date + d, ".csv")) } data2 } # Define UI for application that draws a histogram ui <- fluidPage( titlePanel("查詢時間"), # Default value is the date in client's time zone # Application title # Sidebar with a slider input for number of bins sidebarLayout( sidebarPanel(dateInput("start_date", "Date:"), numericInput("Error_replaced", "測站錯誤資料:", -9999), sliderInput("bins", "回朔天數:", min = 0, max = 50, value = 1) ), # Show a plot of the generated distribution mainPanel( tableOutput("dataoutput") ) ) ) # Define server logic required to draw a histogram server <- function(input, output) { output$dataoutput <- renderTable(HistoryData(input$start_date,input$start_date - input$bins,input$Error_replaced)) } # Run the application shinyApp(ui = ui, server = server) ``` ## [展示網址](https://p56064055.shinyapps.io/E-serviceHistoryDataQuery/) # 前七天加總資料 ```rust= # # This is a Shiny web application. You can run the application by clicking # the 'Run App' button above. # # Find out more about building applications with Shiny here: # # http://shiny.rstudio.com/ # #packages <- c("jsonlite", "rvest", "magrittr", "lubridate") #installed_packages <- packages %in% rownames(installed.packages()) #if (any(installed_packages == FALSE)) { # install.packages(packages[!installed_packages]) #} # 載入所需套件 #invisible(lapply(packages, library, character.only = TRUE)) library("jsonlite") library("rvest") library("magrittr") library("lubridate") library("shiny") HistoryData <- function(start_date,Error_replaced){ # url <- "https://e-service.cwb.gov.tw/HistoryDataQuery/DayDataController.do?command=viewMain&station=C0V730&stname=%25E5%25A4%25A7%25E5%25AF%25AE&datepicker=2020-07-09#" url_start <- "https://e-service.cwb.gov.tw/HistoryDataQuery/DayDataController.do?command=viewMain&station=C0V730&stname=%25E5%25A4%25A7%25E5%25AF%25AE&datepicker=" url <- paste0(url_start,as.character(start_date),"#") #時間資料 # time_H <- url %>% # read_html() %>% # html_nodes(xpath='//*[(@id = "MyTable")]//td[(((count(preceding-sibling::*) + 1) = 1) and parent::*)]') %>% # html_text(trim = T) %>% # as.numeric() #降雨資料 population <- url %>% read_html() %>% html_nodes(xpath='//td[(((count(preceding-sibling::*) + 1) = 11) and parent::*)]') %>% html_text(trim = T) %>% gsub("X",Error_replaced, .) %>% as.numeric() #data1 <- cbind(as.character(start_date + d), time_H, population) sum(population) #print(paste(as.character(start_date + d),data3)) #write.csv(data3,file = paste0("C:/R/Data/", start_date + d, ".csv")) } DayJudge <- function(startDate,BackDay,Error_replaced){ data4 <- data.frame() data5 <- data.frame() for (e in c(0:BackDay)) { data4 <- cbind(as.character(startDate - e), sum(HistoryData(startDate - e,Error_replaced), HistoryData(startDate - e-1,Error_replaced), HistoryData(startDate - e-2,Error_replaced), HistoryData(startDate - e-3,Error_replaced), HistoryData(startDate - e-4,Error_replaced), HistoryData(startDate - e-5,Error_replaced), HistoryData(startDate - e-6,Error_replaced) ) ) data5 <- rbind(data4, data5) } data5 } # Define UI for application that draws a histogram ui <- fluidPage( titlePanel("查詢前七天降雨加總"), # Default value is the date in client's time zone # Application title # Sidebar with a slider input for number of bins sidebarLayout( sidebarPanel(dateInput("start_date", "Date:"), numericInput("Error_replaced", "測站錯誤資料:", 0), sliderInput("bins", "回朔天數:", min = 0, max = 50, value = 1) ), # Show a plot of the generated distribution mainPanel( tableOutput("dataoutput") ) ) ) # Define server logic required to draw a histogram server <- function(input, output) { output$dataoutput <- renderTable(DayJudge(input$start_date,input$bins,input$Error_replaced)) } # Run the application shinyApp(ui = ui, server = server) ``` ## [展示網站](https://p56064055.shinyapps.io/TotalFromPreviousWeek/) # 以google sheet作為資料庫 ```rust= #以google sheet做為資料庫 #install.packages("lubridate") #install.packages("googlesheets4") #install.packages("dplyr") #install.packages("shiny") #install.packages("DT") #install.packages("magrittr") library(lubridate) library(googlesheets4) library(dplyr) library(shiny) library(DT) library(magrittr) gs4_deauth() #starttime <- 7 #endtime <- 17 google_sheet_URL <- "https://docs.google.com/spreadsheets/d/1cAEG8RqpJoDJukBY83ffK8fvdHSV2pGT3F1L9C8zQlE/edit?usp=sharing" google_sheet_URL2 <- "https://docs.google.com/spreadsheets/d/1ZtHk0VsMcYMSXDOnUyBWcVJvv4qPJtah1Ylst7139D4/edit?usp=sharing" gdata <- read_sheet(google_sheet_URL) #%>% as.data.frame() gdata$`date-D` %<>% ymd() gdata$time_H %<>% as.numeric() head(gdata) gdata2 <- read_sheet(google_sheet_URL2) %>% as.data.frame() gdata2$`date-D` %<>% ymd() gdata2$`population-mm` %<>% as.numeric() gdata2$Last_Week_Total %<>% as.numeric() head(gdata2) ui <- fluidPage( titlePanel("查詢降雨加總"), dateRangeInput("daterange", "Date range:", min = min(gdata$`date-D`), max = max(gdata$`date-D`)), mainPanel( DTOutput("dataoutput") )) server <- function(input, output) { output$dataoutput <- renderDT(gdata[,-2] %>% filter(gdata$`date-D`>= as.Date(input$daterange[1],"%Y-%m-%e") & gdata$`date-D`<= as.Date(input$daterange[2],"%Y-%m-%e")) %>% group_by(`date-D`) %>% summarise_all(sum) %>% mutate(Last_Week_Total= gdata2 %>% filter(gdata2$`date-D`>= as.Date(input$daterange[1],"%Y-%m-%e") & gdata2$`date-D`<= as.Date(input$daterange[2],"%Y-%m-%e")) %>% select(`Last_Week_Total`)) %>% DT::datatable(colnames =c("Date","Total rainfall","Last week total rainfall"),filter = 'top',extensions = 'Buttons', options = list( dom = 'Bfrtip', buttons = c('copy', 'csv', 'excel', 'pdf', 'print'))) %>% formatStyle('population-mm', color = styleInterval(0,c('blak','red'))) %>% formatStyle('Last_Week_Total', color = styleInterval(0,c('blak','red'))) %>% formatRound('Last_Week_Total',1) ) } # Run the application shinyApp(ui = ui, server = server) ``` ## [展示網址](https://p56064055.shinyapps.io/DatabaseQuery/) # 增加三天加總 ```rust= #以google sheet做為資料庫 #install.packages("lubridate") #install.packages("googlesheets4") #install.packages("dplyr") #install.packages("shiny") #install.packages("DT") #install.packages("magrittr") library(lubridate) library(googlesheets4) library(dplyr) library(shiny) library(DT) library(magrittr) gs4_deauth() #starttime <- 7 #endtime <- 17 google_sheet_URL <- "https://docs.google.com/spreadsheets/d/1cAEG8RqpJoDJukBY83ffK8fvdHSV2pGT3F1L9C8zQlE/edit?usp=sharing" google_sheet_URL2 <- "https://docs.google.com/spreadsheets/d/1ZtHk0VsMcYMSXDOnUyBWcVJvv4qPJtah1Ylst7139D4/edit?usp=sharing" google_sheet_URL3 <- "https://docs.google.com/spreadsheets/d/1Hz3HpnzL4jP7BEqFw-NqAk1C53WuMjdVttaQGQAeds8/edit?usp=sharing" gdata <- read_sheet(google_sheet_URL) #%>% as.data.frame() gdata$`date-D` %<>% ymd() gdata$time_H %<>% as.numeric() head(gdata) gdata2 <- read_sheet(google_sheet_URL2) %>% as.data.frame() gdata2$`date-D` %<>% ymd() gdata2$`population-mm` %<>% as.numeric() gdata2$Last_Week_Total %<>% as.numeric() head(gdata2) gdata3 <- read_sheet(google_sheet_URL3) %>% as.data.frame() gdata3$`date-D` %<>% ymd() gdata3$`population-mm` %<>% as.numeric() gdata3$Last_3Day_Total %<>% as.numeric() ui <- fluidPage( titlePanel("查詢降雨加總"), dateRangeInput("daterange", "Date range:", min = min(gdata$`date-D`), max = max(gdata$`date-D`)), mainPanel( DTOutput("dataoutput") )) server <- function(input, output) { output$dataoutput <- renderDT(gdata[,-2] %>% filter(gdata$`date-D`>= as.Date(input$daterange[1],"%Y-%m-%e") & gdata$`date-D`<= as.Date(input$daterange[2],"%Y-%m-%e")) %>% group_by(`date-D`) %>% summarise_all(sum) %>% mutate(Last_Week_Total= gdata2 %>% filter(gdata2$`date-D`>= as.Date(input$daterange[1],"%Y-%m-%e") & gdata2$`date-D`<= as.Date(input$daterange[2],"%Y-%m-%e")) %>% select(`Last_Week_Total`)) %>% mutate(Last_3Day_Total= gdata3 %>% filter(gdata3$`date-D`>= as.Date(input$daterange[1],"%Y-%m-%e") & gdata3$`date-D`<= as.Date(input$daterange[2],"%Y-%m-%e")) %>% select(`Last_3Day_Total`)) %>% DT::datatable(colnames =c("Date","Total rainfall","Last week total rainfall","Last 3 day total rainfall"),filter = 'top',extensions = 'Buttons', options = list( dom = 'Bfrtip', buttons = c('copy', 'csv', 'excel', 'pdf', 'print'))) %>% formatStyle('population-mm', color = styleInterval(0,c('blak','red'))) %>% formatStyle('Last_Week_Total', color = styleInterval(0,c('blak','red'))) %>% formatRound('Last_Week_Total',1) ) } # Run the application shinyApp(ui = ui, server = server) ``` # 參考資料 [輕鬆學習 R 語言:網頁資料擷取](https://medium.com/datainpoint/r-essentials-web-scraping-8d0222c1e8d5) 全文分享至 https://www.facebook.com/LHB0222/ 有疑問想討論的都歡迎於下方留言 喜歡的幫我分享給所有的朋友 \o/ 有所錯誤歡迎指教 # [:page_with_curl: 全部文章列表](https://hackmd.io/@LHB-0222/AllWritings) ![](https://i.imgur.com/47HlvGH.png)

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully