setwd('~/covid19_repo')
#setwd('~/')
url <- 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/01-22-2020.csv'
fname <- '01-22-2020.csv'
download.file(url = url, destfile = fname)
#install.packages('lubridate')
library(lubridate)
## Warning: package 'lubridate' was built under R version 4.0.2
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
current_date <- today(tzone="Asia/Taipei")
current_date
## [1] "2020-07-16"
current_date - days(1)
## [1] "2020-07-15"
current_date + days(1)
## [1] "2020-07-17"
current_date - weeks(1)
## [1] "2020-07-09"
seq(1,3)
## [1] 1 2 3
seq( (current_date - days(3)) , current_date , by='days')
## [1] "2020-07-13" "2020-07-14" "2020-07-15" "2020-07-16"
for(i in 1:10){
dt <- current_date - days(i)
print(dt)
}
## [1] "2020-07-15"
## [1] "2020-07-14"
## [1] "2020-07-13"
## [1] "2020-07-12"
## [1] "2020-07-11"
## [1] "2020-07-10"
## [1] "2020-07-09"
## [1] "2020-07-08"
## [1] "2020-07-07"
## [1] "2020-07-06"
format(current_date, format="%Y-%m-%d %H:%M:%S")
## [1] "2020-07-16 00:00:00"
format(current_date, format="%m-%d-%Y")
## [1] "07-16-2020"
format(current_date, format="%d-%m-%Y")
## [1] "16-07-2020"
for(i in 1:10){
dt <- current_date - days(i)
dt_str <- format(dt, format="%d-%m-%Y")
print(dt_str)
}
## [1] "15-07-2020"
## [1] "14-07-2020"
## [1] "13-07-2020"
## [1] "12-07-2020"
## [1] "11-07-2020"
## [1] "10-07-2020"
## [1] "09-07-2020"
## [1] "08-07-2020"
## [1] "07-07-2020"
## [1] "06-07-2020"
paste('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/', '01-22-2020', '.csv')
## [1] "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/ 01-22-2020 .csv"
paste('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/', '01-22-2020', '.csv', sep ="")
## [1] "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/01-22-2020.csv"
paste0('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/', '01-22-2020', '.csv')
## [1] "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/01-22-2020.csv"
#sprintf
hello <- 'Hello,%s'
sprintf(hello, 'David')
## [1] "Hello,David"
hello <- 'Hi, %s. Have you eaten %s'
sprintf(hello,'David', 'breakfast')
## [1] "Hi, David. Have you eaten breakfast"
url <- 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/%s.csv'
for(i in 1:10){
dt <- current_date - days(i)
dt_str <- format(dt, format="%d-%m-%Y")
url_str <- sprintf(url, dt_str)
print(url_str)
}
## [1] "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/15-07-2020.csv"
## [1] "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/14-07-2020.csv"
## [1] "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/13-07-2020.csv"
## [1] "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/12-07-2020.csv"
## [1] "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/11-07-2020.csv"
## [1] "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/10-07-2020.csv"
## [1] "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/09-07-2020.csv"
## [1] "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/08-07-2020.csv"
## [1] "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/07-07-2020.csv"
## [1] "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/06-07-2020.csv"
current_date <- today(tz = 'Asia/Taipei')
start_date <- ymd('2020-01-22')
class(start_date)
## [1] "Date"
start_date <- dmy('14-07-2020')
start_date
## [1] "2020-07-14"
date_seq <- seq(start_date, current_date, by = 'days')
#date_seq
url <- 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/%s.csv'
for(dt in date_seq){
#print(class(dt))
#print(as_date(dt))
dt_str <- format(as_date(dt), '%m-%d-%Y')
fname_str <- sprintf('%s.csv', dt_str)
url_str <- sprintf(url, dt_str)
#print(url_str)
#print(dt_str)
#download.file(url_str, fname_str)
}
R 的錯誤處理機制
3/5
## [1] 0.6
3 / 0
## [1] Inf
#3 / 'qoo'
divide <- function(x, y) {
result <- tryCatch({
x / y
}, warning = function(war) {
print(war)
}, error = function(err) {
print("ERROR")
print(err)
}, finally = {
print("executing finally clause")
})
return(result)
}
divide(3,5)
## [1] "executing finally clause"
## [1] 0.6
divide(3,0)
## [1] "executing finally clause"
## [1] Inf
divide(3, "qoo")
## [1] "ERROR"
## <simpleError in x/y: non-numeric argument to binary operator>
## [1] "executing finally clause"
## <simpleError in x/y: non-numeric argument to binary operator>
url <- 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/%s.csv'
setwd('~/covid19_repo')
for(i in 1:3){
dt <- current_date - days(i)
dt_str <- format(dt, format="%m-%d-%Y")
url_str <- sprintf(url, dt_str)
filename <- sprintf('%s.csv', dt_str)
print(url_str)
tryCatch({
download.file(url_str, filename)
}, error = function(err) {
print(paste("ERROR",url_str))
})
}
## [1] "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/07-15-2020.csv"
## Warning in download.file(url_str, filename): cannot open URL 'https://
## raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/
## csse_covid_19_daily_reports/07-15-2020.csv': HTTP status was '404 Not Found'
## [1] "ERROR https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/07-15-2020.csv"
## [1] "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/07-14-2020.csv"
## [1] "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/07-13-2020.csv"
for(i in 1:3){
dt <- current_date - days(i)
dt_str <- format(dt, format="%m-%d-%Y")
url_str <- sprintf(url, dt_str)
filename <- sprintf('%s.csv', dt_str)
if (! file.exists(filename)){
print(url_str)
tryCatch({
download.file(url_str, filename)
}, error = function(err) {
print(url_str)
})
}
}
## [1] "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/07-15-2020.csv"
## Warning in download.file(url_str, filename): cannot open URL 'https://
## raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/
## csse_covid_19_daily_reports/07-15-2020.csv': HTTP status was '404 Not Found'
## [1] "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/07-15-2020.csv"