diff --git a/.github/workflows/r.yml b/.github/workflows/r.yml index 76d78a84..ef6a7852 100644 --- a/.github/workflows/r.yml +++ b/.github/workflows/r.yml @@ -24,18 +24,25 @@ jobs: matrix: r-version: ['3.6.3', '4.1.1'] - steps: + steps: - uses: actions/checkout@v3 - uses: r-lib/actions/setup-pandoc@v2 - name: Set up R ${{ matrix.r-version }} uses: r-lib/actions/setup-r@f57f1301a053485946083d7a45022b278929a78a with: r-version: ${{ matrix.r-version }} - - name: Install dependencies + cran: https://cran-archive.r-project.org + - name: Install dependencies run: | install.packages(c("remotes", "rcmdcheck")) - remotes::install_deps(dependencies = TRUE) + remotes::install_deps(dependencies = TRUE, type = "binary") shell: Rscript {0} + env: + R_LIBS_USER: /Users/runner/work/_temp/Library + TZ: UTC + _R_CHECK_SYSTEM_CLOCK_: FALSE + NOT_CRAN: true + timeout-minutes: 30 - name: Check run: rcmdcheck::rcmdcheck(args = "--no-manual", error_on = "error") shell: Rscript {0} diff --git a/DESCRIPTION b/DESCRIPTION index 4ee272e9..a8182516 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -22,7 +22,9 @@ Imports: stringr, curl, tibble, - rvest + rvest, + withr, + httr Suggests: knitr, rmarkdown, @@ -30,6 +32,8 @@ Suggests: remotes, tidyverse, testthat (>= 3.0.0) +Config/Needs/website: + tidyverse/tidytemplate RoxygenNote: 7.2.3 BugReports: https://github.com/davidycliao/legisTaiwan/issues URL: https://davidycliao.github.io/legisTaiwan/ diff --git a/NAMESPACE b/NAMESPACE index 82c23102..34c4d4c5 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,10 +1,5 @@ # Generated by roxygen2: do not edit by hand -export(.onAttach) -export(api_check) -export(check_date) -export(check_date2) -export(check_internet) export(get_bills) export(get_bills_2) export(get_caucus_meetings) @@ -16,16 +11,15 @@ export(get_parlquestions) export(get_public_debates) export(get_speech_video) export(get_variable_info) -export(review_session_info) -export(transformed_date_bill) -export(transformed_date_meeting) -export(website_availability2) importFrom(attempt,stop_if_all) importFrom(attempt,stop_if_not) importFrom(curl,has_internet) +importFrom(httr,GET) +importFrom(httr,content) importFrom(jsonlite,fromJSON) importFrom(rvest,html_text2) importFrom(rvest,read_html) importFrom(stringr,str_split_1) importFrom(stringr,str_sub) importFrom(tibble,as_tibble) +importFrom(withr,with_options) diff --git a/NEWS.md b/NEWS.md index a1bcad40..90dc5c5a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,6 @@ +# `legisTaiwan` 0.1.6 + + # `legisTaiwan` 0.1.4 (development version) * re-documentation and inserting handlers. diff --git a/R/bill.R b/R/bill.R index 53cc9b30..04113f5d 100644 --- a/R/bill.R +++ b/R/bill.R @@ -1,102 +1,93 @@ -#'The Records of the Bills 法律提案 -#' -#'@author David Liao (davidycliao@@gmail.com) -#' -#'@param start_date numeric Must be formatted in Minguo (Taiwan) calendar, e.g. 1090101. -#' -#'@param end_date numeric Must be formatted in Minguo (Taiwan) calendar, e.g. 1090102. -#' -#'@param proposer The default value is NULL, which means all bill proposed by all legislators -#' are included between the starting date and the ending date. -#' -#'@param verbose logical, indicates whether `get_bills` should print out -#'detailed output when retrieving the data. The default value is TRUE. -#' -#'@return list, which contains: \describe{ -#' \item{`title`}{the meeting records of cross-caucus session} -#' \item{`query_time`}{the query time} -#' \item{`retrieved_number`}{the number of observation} -#' \item{`meeting_unit`}{the meeting unit} -#' \item{`start_date_ad`}{the start date in POSIXct} -#' \item{`end_date_ad`}{the end date in POSIXct} -#' \item{`start_date`}{the start date in ROC Taiwan calendar} -#' \item{`url`}{the retrieved json url} -#' \item{`variable_names`}{the variables of the tibble dataframe} -#' \item{`manual_info`}{the official manual, \url{https://www.ly.gov.tw/Pages/List.aspx?nodeid=153}; or use legisTaiwan::get_variable_info("get_bills")} -#' \item{`data`}{a tibble dataframe, whose variables include: -#' \describe{\item{`term`}{屆別} -#' \item{`sessionPeriod`}{會期} -#' \item{`sessionTimes`}{會次} -#' \item{`meetingTimes`}{提案日期} -#' \item{`billName`}{提案名稱} -#' \item{`billProposer`}{主提案人} -#' \item{`billCosignatory`}{連署提案} -#' \item{`billStatus`}{議案狀態} -#' \item{`date_ad`}{西元年} -#' } -#' } -#' } -#' -#'@importFrom attempt stop_if_all -#'@importFrom jsonlite fromJSON -#' -#'@export -#' -#'@examples -#' ## query bill records by a period of the dates in Taiwan ROC calender format -#' ## 輸入「中華民國民年」下載立法委員提案資料 -#'get_bills(start_date = 1060120, end_date = 1070310, verbose = FALSE) -#' -#' ## query bill records by a period of the dates in Taiwan ROC calender format -#' ## and a specific legislator -#' ## 輸入「中華民國民年」與「指定立法委員」下載立法委員提案資料 -#'get_bills(start_date = 1060120, end_date = 1070310, proposer = "孔文吉") -#' -#' ## query bill records by a period of the dates in Taiwan ROC calender format -#' ## and multiple legislators -#' ## 輸入「中華民國民年」與「指定多個立法委員」下載立法委員提案資料 -#'get_bills(start_date = 1060120, end_date = 1060510, proposer = "孔文吉&鄭天財") -#' -#'@details `get_bills` produces a list, which contains `query_time`, -#'`retrieved_number`, `meeting_unit`, `start_date_ad`, `end_date_ad`, `start_date`, -#'`end_date`, `url`, `variable_names`, `manual_info` and `data`. -#' -#'@note To retrieve the user manual and more information about variable of the data -#' frame, please use `legisTaiwan::get_variable_info("get_bills")` -#' or visit the API manual at \url{https://www.ly.gov.tw/Pages/List.aspx?nodeid=153}. -#' 資料似乎不一致,待確認。委員發言(取得最早時間不詳,待檢查。) -#' -#'@seealso -#'`get_variable_info("get_bills_2")`,`review_session_info()` -#' -#'@seealso -#'Regarding Minguo calendar, please see \url{https://en.wikipedia.org/wiki/Republic_of_China_calendar}. - +#'The Records of the Bills: 法律提案 +#' +#' @param start_date numeric. Must be formatted in the ROC Taiwan calendar, e.g., 1090101. +#' @param end_date numeric. Must be formatted in the ROC Taiwan calendar, e.g., 1090102. +#' @param proposer The default value is NULL, indicating that bills proposed by all legislators +#' are included between the start and end dates. +#' @param verbose logical. Specifies whether `get_bills` should print out +#' detailed output when retrieving the data. The default value is TRUE. +#' +#' @return A list, which contains: +#' \describe{ +#' \item{`title`}{Records of cross-caucus sessions} +#' \item{`query_time`}{Query timestamp} +#' \item{`retrieved_number`}{Number of observations retrieved} +#' \item{`meeting_unit`}{Meeting unit} +#' \item{`start_date_ad`}{Start date in POSIXct format} +#' \item{`end_date_ad`}{End date in POSIXct format} +#' \item{`start_date`}{Start date in the ROC Taiwan calendar} +#' \item{`url`}{URL of the retrieved JSON data} +#' \item{`variable_names`}{Variable names of the tibble dataframe} +#' \item{`manual_info`}{Official manual. See \url{https://www.ly.gov.tw/Pages/List.aspx?nodeid=153} or use get_variable_info("get_bills")} +#' \item{`data`}{A tibble dataframe with the following variables: +#' \describe{\item{`term`}{Session number} +#' \item{`sessionPeriod`}{Session period} +#' \item{`sessionTimes`}{Session count} +#' \item{`meetingTimes`}{Proposal date} +#' \item{`billName`}{Bill name} +#' \item{`billProposer`}{Primary proposer} +#' \item{`billCosignatory`}{Co-signatories of the bill} +#' \item{`billStatus`}{Status of the bill} +#' \item{`date_ad`}{Date in the Gregorian calendar} +#' } +#' } +#' } +#' +#' @importFrom httr GET +#' @importFrom httr content +#' @importFrom jsonlite fromJSON +#' @importFrom tibble as_tibble +#' @importFrom withr with_options +#' +#' @export +#' +#' @examples +#' \dontrun{ +#' ## Query bill records by a date range in the Taiwan ROC calendar format +#' get_bills(start_date = 1060120, end_date = 1070310, verbose = FALSE) +#' +#' ## Query bill records by a date range and a specific legislator +#' get_bills(start_date = 1060120, end_date = 1070310, proposer = "孔文吉") +#' +#' ## Query bill records by a date range and multiple legislators +#' get_bills(start_date = 1060120, end_date = 1060510, proposer = "孔文吉&鄭天財") +#' } +#' +#' @details The `get_bills` function returns a list that contains `query_time`, +#' `retrieved_number`, `meeting_unit`, `start_date_ad`, `end_date_ad`, `start_date`, +#' `end_date`, `url`, `variable_names`, `manual_info`, and `data`. +#' +#' @note To retrieve the user manual and more details about the data frame, use `get_variable_info("get_bills")`. +#' Further checks are required as the user manual seems to be inconsistent with the actual data. +#' +#' @seealso +#' \url{https://www.ly.gov.tw/Pages/List.aspx?nodeid=153} get_bills <- function(start_date = NULL, end_date = NULL, proposer = NULL, verbose = TRUE) { - legisTaiwan::check_internet() - legisTaiwan::api_check(start_date = legisTaiwan::check_date(start_date), end_date = legisTaiwan::check_date(end_date)) + check_internet() + api_check(start_date = check_date(start_date), end_date = check_date(end_date)) + validate_dates_format(start_date, end_date) set_api_url <- paste("https://www.ly.gov.tw/WebAPI/LegislativeBill.aspx?from=", start_date, "&to=", end_date, "&proposer=", proposer, "&mode=json", sep = "") tryCatch( { - json_df <- jsonlite::fromJSON(set_api_url) + with_options(list(timeout = max(1000, getOption("timeout"))),{json_df <- jsonlite::fromJSON(set_api_url)}) df <- tibble::as_tibble(json_df) attempt::stop_if_all(nrow(df) == 0, isTRUE, msg = "The query is unavailable.") - df["date_ad"] <- do.call("c", lapply(df$date, legisTaiwan::transformed_date_bill)) + df["date_ad"] <- do.call("c", lapply(df$date, transformed_date_bill)) if (isTRUE(verbose)) { cat(" Retrieved URL: \n", set_api_url, "\n") cat(" Retrieved Bill Sponsor(s): ", proposer, "\n") - cat(" Retrieved date between:", as.character(legisTaiwan::check_date(start_date)), "and", as.character(legisTaiwan::check_date(end_date)) , "\n") + cat(" Retrieved date between:", as.character(check_date(start_date)), "and", as.character(check_date(end_date)) , "\n") cat(" Retrieved Num:", nrow(df), "\n") } list_data <- list("title" = "the records of bill sponsor and co-sponsor", "query_time" = Sys.time(), "retrieved_number" = nrow(df), "proposer" = proposer, - "start_date_ad" = legisTaiwan::check_date(start_date), - "end_date_ad" = legisTaiwan::check_date(end_date), + "start_date_ad" = check_date(start_date), + "end_date_ad" = check_date(end_date), "start_date" = start_date, "end_date" = end_date, "url" = set_api_url, @@ -112,35 +103,31 @@ get_bills <- function(start_date = NULL, end_date = NULL, proposer = NULL, } -#'The Records of Legislation and the Executives Proposals 委員及政府議案提案資訊 -#' -#'@author David Liao (davidycliao@@gmail.com) -#' -#'@param term numeric or null. The data is only available from 8th term. -#'The default is set to 8. 參數必須為數值。資料從自第8屆起,預設值為8。 -#' -#'@param session_period integer, numeric or NULL. Available -#'options for the session is: 1, 2, 3, 4, 5, 6, 7, and 8. The default is set to NULL. -#'參數必須為數值。 -#'`review_session_info()` generates each session period available option period -#'in Minguo (Taiwan) calendar. -#' -#'@param verbose The default value is TRUE, displaying the description of data -#'retrieved in number, url and computing time. -#' -#'@return list list, which contains: \describe{ -#' \item{`title`}{the meeting records of cross-caucus session} -#' \item{`query_time`}{the query time} -#' \item{`retrieved_number`}{the number of observation} -#' \item{`meeting_unit`}{the meeting unit} -#' \item{`start_date_ad`}{the start date in POSIXct} -#' \item{`end_date_ad`}{the end date in POSIXct} -#' \item{`start_date`}{the start date in ROC Taiwan calendar} -#' \item{`url`}{the retrieved json url} -#' \item{`variable_names`}{the variables of the tibble dataframe} -#' \item{`manual_info`}{the official manual, \url{https://data.ly.gov.tw/getds.action?id=20}; or use legisTaiwan::get_variable_info("get_bills_2")} -#' \item{`data`}{a tibble dataframe, whose variables include: -#' \describe{\item{`term`}{屆別} +#' The Records of Legislation and the Executives Proposals: 委員及政府議案提案資訊 +#' +#' @author David Liao (davidycliao@@gmail.com) +#' +#' @param term A numeric or NULL value. Data is available from the 8th term onwards. +#' Default is set to 8. 參數必須為數值。資料從第8屆開始,預設值為8。 +#' +#' @param session_period An integer, numeric, or NULL. Valid options for the session are: +#' 1, 2, 3, 4, 5, 6, 7, and 8. Default is set to NULL. +#' 參數必須為數值。 +#' `review_session_info()` provides available session periods based on the Minguo (Taiwan) calendar. +#' +#' @param verbose Default value is TRUE. Displays details of the retrieved data, including the number, URL, and computing time. +#' +#' @return A list containing: +#' \item{`title`}{Records of questions answered by the executives} +#' \item{`query_time`}{Query time} +#' \item{`retrieved_number`}{Number of observations} +#' \item{`retrieved_term`}{Retrieved term} +#' \item{`url`}{Retrieved JSON URL} +#' \item{`variable_names`}{Variables of the tibble dataframe} +#' \item{`manual_info`}{Official manual: \url{https://data.ly.gov.tw/getds.action?id=20} or use `get_variable_info("get_bills_2")`} +#' \item{`data`}{A tibble dataframe with variables such as: +#' \describe{ +#' \item{`term`}{屆別} #' \item{`sessionPeriod`}{會期} #' \item{`sessionTimes`}{會次} #' \item{`meetingTimes`}{臨時會會次} @@ -150,79 +137,95 @@ get_bills <- function(start_date = NULL, end_date = NULL, proposer = NULL, #' \item{`billProposer`}{主提案人} #' \item{`billCosignatory`}{連署提案} #' \item{`billStatus`}{議案狀態} -#' \item{`pdfUrl`}{關係文書pdf檔案下載位置} -#' \item{`docUrl`}{關係文書doc檔案下載位置} -#' \item{`selectTerm`}{屆別期別篩選條件} +#' \item{`pdfUrl`}{PDF download link for related documents} +#' \item{`docUrl`}{DOC download link for related documents} +#' \item{`selectTerm`}{Filtering criteria based on term} #' } #' } -#' } #' -#'@importFrom attempt stop_if_all -#'@importFrom jsonlite fromJSON +#' @importFrom attempt stop_if_all +#' @importFrom jsonlite fromJSON +#' @importFrom httr GET +#' @importFrom tibble as_tibble +#' @importFrom withr with_options #' -#'@export +#' @export #' -#'@examples -#' ## query the Executives' answered response by term and the session period. -#' ## 輸入「立委屆期」與「會期」下載「質詢事項 (行政院答復部分)」 -#'get_bills_2(term = 8, session_period = 1) +#' @examples +#' \dontrun{ +#' ## Query the executives' responses by term and session period. +#' ## 輸入「立委屆期」與「會期」以下載「質詢事項 (行政院答復部分)」 +#' get_bills_2(term = 8, session_period = 1) +#' } #' -#'@details `get_bills_2` produces a list, which contains `query_time`, -#'`retrieved_number`, `meeting_unit`, `start_date_ad`, `end_date_ad`, `start_date`, -#'`end_date`, `url`, `variable_names`, `manual_info` and `data`. To retrieve the user -#'manual and more information about the data frame, please use `legisTaiwan::get_variable_info("get_bills_2")`. +#' @details The `get_bills_2` function produces a list, which includes `query_time`, +#' `retrieved_number`, `retrieved_term`, `url`, `variable_names`, `manual_info`, and `data`. +#' For the user manual and more information about the dataframe, use `get_variable_info("get_bills_2")`. #' -#'@note To retrieve the user manual and more information about variable of the data -#' frame, please use `legisTaiwan::get_variable_info("get_bills_2")` +#' @note For more details about the dataframe's variables, use `get_variable_info("get_bills_2")` #' or visit the API manual at \url{https://data.ly.gov.tw/getds.action?id=20}. -#' 議事類: 提供委員及政府之議案提案資訊 (自第8屆第1會期起)。 +#' 議事類: 提供委員及政府之議案提案資訊 (從第8屆第1會期開始)。 #' -#'@seealso -#'`get_variable_info("get_bills_2")`,`review_session_info()` - +#' @seealso +#' `get_variable_info("get_bills_2")`,`review_session_info()` +# Define the function get_bills_2 get_bills_2 <- function(term = 8, session_period = NULL, verbose = TRUE) { - legisTaiwan::check_internet() + + # Check for internet connectivity + check_internet() + + # If the term is not specified if (is.null(term)) { - options(timeout = max(1000, getOption("timeout"))) - set_api_url <- paste("https://data.ly.gov.tw/odw/ID20Action.action?term=", - term, "&sessionPeriod=", - "&sessionTimes=&meetingTimes=&billName=&billOrg=&billProposer=&billCosignatory=&fileType=json", - sep = "") - message(" term is not defined...\n You are now requesting full data from the API. Please make sure your connectivity is stable until its completion.\n") + # Set the base API URL without specifying any term + set_api_url <- "https://data.ly.gov.tw/odw/ID20Action.action?term=&sessionPeriod=&sessionTimes=&meetingTimes=&billName=&billOrg=&billProposer=&billCosignatory=&fileType=json" + # Display a notification message + message("The term is not defined...\nYou are now requesting full data from the API. Please ensure a stable internet connection until completion.\n") } else { - attempt::stop_if_all(term, is.character, msg = "use numeric format only.") + # If the term is in character format, stop execution and display an error message + attempt::stop_if_all(term, is.character, msg = "Please use numeric format only.") + + # If term length is one, format the term to two digits if (length(term) == 1) { - term <- sprintf("%02d", as.numeric(term))} - else if (length(term) > 1) { - # options(timeout = max(1000, getOption("timeout"))) - term <- paste(sprintf("%02d", as.numeric(term)), collapse = "&") - message("The API is unable to query multiple terms and the retrieved data might not be complete.")} + term <- sprintf("%02d", as.numeric(term)) + } else if (length(term) > 1) { # If multiple terms are provided + stop("The API doesn't support querying multiple terms. Consider implementing batch processing. Please refer to the tutorial for guidance.") + } + + # Convert session period to two-digit format, if it's not NULL + session_str <- ifelse(is.null(session_period), "", sprintf("%02d", as.numeric(session_period))) + + # Construct the complete API URL + set_api_url <- paste0("https://data.ly.gov.tw/odw/ID20Action.action?term=", + term, "&sessionPeriod=", session_str, + "&sessionTimes=&meetingTimes=&billName=&billOrg=&billProposer=&billCosignatory=&fileType=json") } - set_api_url <- paste("https://data.ly.gov.tw/odw/ID20Action.action?term=", - term, "&sessionPeriod=", - sprintf("%02d", as.numeric(session_period)), - "&sessionTimes=&meetingTimes=&billName=&billOrg=&billProposer=&billCosignatory=&fileType=json", sep = "") + + # Try to fetch the data and process it tryCatch( { - json_df <- jsonlite::fromJSON(set_api_url) + with_options(list(timeout = max(1000, getOption("timeout"))),{json_df <- jsonlite::fromJSON(set_api_url)}) df <- tibble::as_tibble(json_df$dataList) + # If the returned data is empty, stop execution and display an error message attempt::stop_if_all(nrow(df) == 0, isTRUE, msg = "The query is unavailable.") if (isTRUE(verbose)) { - cat(" Retrieved URL: \n", set_api_url, "\n") - cat(" Retrieved Term: ", term, "\n") - cat(" Retrieved Num: ", nrow(df), "\n") + cat("Retrieved URL: ", set_api_url, "\n") + cat("Retrieved Term: ", term, "\n") + cat("Retrieved Number of Records: ", nrow(df), "\n") } - list_data <- list("title" = "the records of the questions answered by the executives", - "query_time" = Sys.time(), - "retrieved_number" = nrow(df), - "retrieved_term" = term, - "url" = set_api_url, - "variable_names" = colnames(df), - "manual_info" = "https://data.ly.gov.tw/getds.action?id=2", - "data" = df) - return(list_data) + # Construct the result list + list_data <- list( + "title" = "The records of the questions answered by the executives", + "query_time" = Sys.time(), + "retrieved_number" = nrow(df), + "retrieved_term" = term, + "url" = set_api_url, + "variable_names" = colnames(df), + "manual_info" = "https://data.ly.gov.tw/getds.action?id=2", + "data" = df + ) + return(list_data) # Return the result list }, - error = function(error_message) { + error = function(error_message) { # Handle any errors message(error_message) } ) diff --git a/R/info.R b/R/info.R index 27d4ff51..eb91c04f 100644 --- a/R/info.R +++ b/R/info.R @@ -30,72 +30,139 @@ #'@importFrom rvest html_text2 read_html #'@importFrom tibble as_tibble #' -#'@export +#' @export #' -#'@seealso -#'`review_session_info()` +#' @seealso `review_session_info()`. +#' +#' @examples +#' \dontrun{ +#' get_variable_info("get_bills") +#' } +# get_variable_info <- function(param_) { +# check_internet() +# attempt::stop_if_all(website_availability(), isFALSE, msg = "the error from the API.") +# attempt::stop_if_all(param_, is.numeric, msg = "use string format only.") +# attempt::stop_if_all(param_, is.null, msg = "use correct funtion names.") +# attempt::stop_if(param_ , ~ length(.x) >1, msg = "only allowed to query one function.") +# if (param_ == "get_parlquestions") { +# url <- "https://data.ly.gov.tw/getds.action?id=6" +# } +# else if (param_ == "get_legislators") { +# url <- "https://data.ly.gov.tw/getds.action?id=16" +# } +# else if (param_ == "get_committee_record") { +# url <- "https://data.ly.gov.tw/getds.action?id=46" +# } +# else if (param_ == "get_executive_response") { +# url <- "https://data.ly.gov.tw/getds.action?id=2" +# } +# else if (param_ == "get_caucus_meetings") { +# url <- "https://data.ly.gov.tw/getds.action?id=8" +# } +# else if (param_ == "get_speech_video") { +# url <- "https://data.ly.gov.tw/getds.action?id=148" +# } +# else if (param_ == "get_bills_2") { +# url <- "https://data.ly.gov.tw/getds.action?id=20" +# } +# else if (param_ == "get_public_debates") { +# url <- "https://data.ly.gov.tw/getds.action?id=7" +# } +# else if (param_ %in% c("get_bills", "get_meetings")) { +# if (param_ == "get_meetings") { +# url <- "https://www.ly.gov.tw/Pages/List.aspx?nodeid=154" +# } +# else if (param_ == "get_bills") { +# url <- "https://www.ly.gov.tw/Pages/List.aspx?nodeid=153" +# } +# html_info <- rvest::html_text2(rvest::html_nodes(rvest::html_nodes(rvest::read_html(url), "*[id='form_Query']"), "div") ) +# page_info <- list(page_info = strsplit(html_info[14], split = "\n")[[1]], reference_url = url) +# return(page_info) +# } +# else { +# stop("Use correct funtion names below in character format: +# get_bills: the records of the bills +# get_bills_2: the records of legislators and the government proposals +# get_meetings: the spoken meeting records +# get_caucus_meetings: the meeting records of cross-caucus session +# get_speech_video: the full video information of meetings and committees +# get_public_debates: the records of national public debates +# get_parlquestions: the records of parliamentary questions +# get_executive_response: the records of the questions answered by the executives") +# } +# html <- rvest::html_nodes(rvest::read_html(url), "*[id='content']") +# title <- gsub("[[:space:]]", "", rvest::html_text2(rvest::html_nodes(html, "h2"))) +# +# content <- gsub("[[:space:]]", "", rvest::html_text2(rvest::html_nodes(html, "span"))) +# df <- data.frame(content[seq(1, length(content), 2 )], +# content[seq(1, length(content) + 1, 2 ) -1]) +# colnames(df) <- c(title[2], title[1]) +# df <- tibble::as_tibble(df) +# page_info <- list(page_info = df, reference_url = url) +# return(page_info) +# } get_variable_info <- function(param_) { - legisTaiwan::check_internet() - attempt::stop_if_all(website_availability(), isFALSE, msg = "the error from the API.") - attempt::stop_if_all(param_, is.numeric, msg = "use string format only.") - attempt::stop_if_all(param_, is.null, msg = "use correct funtion names.") - attempt::stop_if(param_ , ~ length(.x) >1, msg = "only allowed to query one function.") - if (param_ == "get_parlquestions") { - url <- "https://data.ly.gov.tw/getds.action?id=6" - } - else if (param_ == "get_legislators") { - url <- "https://data.ly.gov.tw/getds.action?id=16" - } - else if (param_ == "get_committee_record") { - url <- "https://data.ly.gov.tw/getds.action?id=46" - } - else if (param_ == "get_executive_response") { - url <- "https://data.ly.gov.tw/getds.action?id=2" - } - else if (param_ == "get_caucus_meetings") { - url <- "https://data.ly.gov.tw/getds.action?id=8" - } - else if (param_ == "get_speech_video") { - url <- "https://data.ly.gov.tw/getds.action?id=148" - } - else if (param_ == "get_bills_2") { - url <- "https://data.ly.gov.tw/getds.action?id=20" + # Ensure internet and website availability + check_internet() + attempt::stop_if_not(website_availability(), msg = "the error from the API.") + + # Parameter checks + attempt::stop_if(param_, is.numeric, msg = "use string format only.") + attempt::stop_if(param_, is.null, msg = "use correct function names.") + attempt::stop_if(param_, ~ length(.x) > 1, msg = "only allowed to query one function.") + + # Dictionary for URL mapping + url_mapping <- list( + get_parlquestions = "https://data.ly.gov.tw/getds.action?id=6", + get_legislators = "https://data.ly.gov.tw/getds.action?id=16", + get_committee_record = "https://data.ly.gov.tw/getds.action?id=46", + get_executive_response = "https://data.ly.gov.tw/getds.action?id=2", + get_caucus_meetings = "https://data.ly.gov.tw/getds.action?id=8", + get_speech_video = "https://data.ly.gov.tw/getds.action?id=148", + get_bills_2 = "https://data.ly.gov.tw/getds.action?id=20", + get_public_debates = "https://data.ly.gov.tw/getds.action?id=7" + ) + if (!(param_ %in% names(url_mapping) || param_ %in% c("get_bills", "get_meetings"))) { + stop("Use correct function names below in character format: + get_bills: the records of the bills + get_bills_2: the records of legislators and the government proposals + get_meetings: the spoken meeting records + get_caucus_meetings: the meeting records of cross-caucus session + get_speech_video: the full video information of meetings and committees + get_public_debates: the records of national public debates + get_parlquestions: the records of parliamentary questions + get_executive_response: the records of the questions answered by the executives") } - else if (param_ == "get_public_debates") { - url <- "https://data.ly.gov.tw/getds.action?id=7" + + + # Fetch URL from dictionary or process special cases + if (param_ %in% names(url_mapping)) { + url <- url_mapping[[param_]] + } else if (param_ == "get_meetings") { + url <- "https://www.ly.gov.tw/Pages/List.aspx?nodeid=154" + } else if (param_ == "get_bills") { + url <- "https://www.ly.gov.tw/Pages/List.aspx?nodeid=153" + } else { + stop("Use correct function names below in character format.") } - else if (param_ %in% c("get_bills", "get_meetings")) { - if (param_ == "get_meetings") { - url <- "https://www.ly.gov.tw/Pages/List.aspx?nodeid=154" - } - else if (param_ == "get_bills") { - url <- "https://www.ly.gov.tw/Pages/List.aspx?nodeid=153" - } - html_info <- rvest::html_text2(rvest::html_nodes(rvest::html_nodes(rvest::read_html(url), "*[id='form_Query']"), "div") ) + + if (param_ %in% c("get_bills", "get_meetings")) { + html_info <- rvest::html_text2(rvest::html_nodes(rvest::html_nodes(rvest::read_html(url), "*[id='form_Query']"), "div")) page_info <- list(page_info = strsplit(html_info[14], split = "\n")[[1]], reference_url = url) return(page_info) } - else { - stop("Use correct funtion names below in character format: - get_bills: the records of the bills - get_bills_2: the records of legislators and the government proposals - get_meetings: the spoken meeting records - get_caucus_meetings: the meeting records of cross-caucus session - get_speech_video: the full video information of meetings and committees - get_public_debates: the records of national public debates - get_parlquestions: the records of parliamentary questions - get_executive_response: the records of the questions answered by the executives") - } - html <- rvest::html_nodes(rvest::read_html(url), "*[id='content']") - title <- gsub("[[:space:]]", "", rvest::html_text2(rvest::html_nodes(html, "h2"))) - - content <- gsub("[[:space:]]", "", rvest::html_text2(rvest::html_nodes(html, "span"))) - df <- data.frame(content[seq(1, length(content), 2 )], - content[seq(1, length(content) + 1, 2 ) -1]) - colnames(df) <- c(title[2], title[1]) - df <- tibble::as_tibble(df) - page_info <- list(page_info = df, reference_url = url) + + html <- rvest::html_nodes(rvest::read_html(url), "*[id='content']") + title <- gsub("[[:space:]]", "", rvest::html_text2(rvest::html_nodes(html, "h2"))) + + content <- gsub("[[:space:]]", "", rvest::html_text2(rvest::html_nodes(html, "span"))) + df <- data.frame(content[seq(1, length(content), 2)], + content[seq(1, length(content) + 1, 2) - 1]) + colnames(df) <- c(title[2], title[1]) + df <- tibble::as_tibble(df) + page_info <- list(page_info = df, reference_url = url) + return(page_info) } @@ -115,28 +182,55 @@ get_variable_info <- function(param_) { #'@importFrom rvest html_text2 read_html #'@importFrom tibble as_tibble #' -#'@export +#' @seealso +#' Regarding Minguo calendar, please see \url{https://en.wikipedia.org/wiki/Republic_of_China_calendar}. #' -#'@seealso -#'Regarding Minguo calendar, please see \url{https://en.wikipedia.org/wiki/Republic_of_China_calendar}. +#' @examples +#' \dontrun{ +#' review_session_info(7) +#' } + +# review_session_info <- function(term){ +# attempt::stop_if_all(website_availability2(), isFALSE, msg = "the error from the API.") +# attempt::stop_if_all(term, is.null, msg = "use correct `term`") +# attempt::stop_if_all(term %in% 1:11, isFALSE, msg = "use correct `term`") +# url <- paste("https://npl.ly.gov.tw/do/www/appDate?status=0&expire=", +# sprintf("%02d", as.numeric(term)), +# "&startYear=0", sep ="") +# html_ <- rvest::html_nodes(rvest::read_html(url), "*[class='section_wrapper']") +# title <- stringr::str_split_1(rvest::html_text2(rvest::html_nodes(html_, "[class='tt_titlebar2']")), "\t\r")[1:2] +# o <- rvest::html_text2(rvest::html_nodes(html_, "[class='tt_listrow_odd']")) +# e <- rvest::html_text2(rvest::html_nodes(html_, "[class='tt_listrow_even']")) +# s <- lapply(lapply(c(o, e),function(.){stringr::str_split_1(., "\r\r" )}), +# function(.){gsub("[[:space:]]", "", .)}) +# df <- do.call(rbind, s) +# colnames(df) <- title +# df <- tibble::as_tibble(df) +# return(df) +# } +# +# + + + +review_session_info <- function(term) { + attempt::stop_if_not(website_availability2(), msg = "the error from the API.") + attempt::stop_if(term, is.null, msg = "use correct `term`.") + attempt::stop_if_not(term %in% 1:11, msg = "use correct `term`.") -review_session_info <- function(term){ - attempt::stop_if_all(legisTaiwan::website_availability2(), isFALSE, msg = "the error from the API.") - attempt::stop_if_all(term, is.null, msg = "use correct `term`") - attempt::stop_if_all(term %in% 1:11, isFALSE, msg = "use correct `term`") url <- paste("https://npl.ly.gov.tw/do/www/appDate?status=0&expire=", sprintf("%02d", as.numeric(term)), "&startYear=0", sep ="") + html_ <- rvest::html_nodes(rvest::read_html(url), "*[class='section_wrapper']") title <- stringr::str_split_1(rvest::html_text2(rvest::html_nodes(html_, "[class='tt_titlebar2']")), "\t\r")[1:2] o <- rvest::html_text2(rvest::html_nodes(html_, "[class='tt_listrow_odd']")) e <- rvest::html_text2(rvest::html_nodes(html_, "[class='tt_listrow_even']")) - s <- lapply(lapply(c(o, e),function(.){stringr::str_split_1(., "\r\r" )}), - function(.){gsub("[[:space:]]", "", .)}) + s <- lapply(lapply(c(o, e), function(.) {stringr::str_split_1(., "\r\r")}), + function(.) {gsub("[[:space:]]", "", .)}) df <- do.call(rbind, s) colnames(df) <- title df <- tibble::as_tibble(df) - return(df) - } - + return(df) +} diff --git a/R/legislators.R b/R/legislators.R index 4a7fa2b7..30b31e46 100644 --- a/R/legislators.R +++ b/R/legislators.R @@ -33,6 +33,7 @@ #' #'@importFrom attempt stop_if_all #'@importFrom jsonlite fromJSON +#'@importFrom withr with_options #' #'@export #' @@ -45,7 +46,7 @@ #'`queried_term`, `url`, `variable_names`, `manual_info` and `data`. #' #'@note To retrieve the user manual and more information about variable of the data -#' frame, please use `legisTaiwan::get_variable_info("get_legislators")` +#' frame, please use `get_variable_info("get_legislators")` #' or visit the API manual at \url{https://data.ly.gov.tw/getds.action?id=16}. #' 提供委員基本資料,最早資料可追溯至第2屆。 #' @@ -53,7 +54,7 @@ #'`get_variable_info("get_legislators")`, `review_session_info()` get_legislators <- function(term = NULL, verbose = TRUE) { - legisTaiwan::check_internet() + check_internet() if (is.null(term)) { set_api_url <- paste("https://data.ly.gov.tw/odw/ID16Action.action?name=&sex=&party=&partyGroup=&areaName=&term=", term, "=&fileType=json", sep = "") @@ -71,7 +72,7 @@ get_legislators <- function(term = NULL, verbose = TRUE) { } tryCatch( { - json_df <- jsonlite::fromJSON(set_api_url) + with_options(list(timeout = max(1000, getOption("timeout"))),{json_df <- jsonlite::fromJSON(set_api_url)}) df <- tibble::as_tibble(json_df$dataList) attempt::stop_if_all(nrow(df) == 0, isTRUE, msg = "The query is unavailable.") term <- paste(sort(as.numeric(unique(df$term))), collapse = " ", sep = ",") diff --git a/R/meeting.R b/R/meeting.R index 44f38933..aeed4d71 100644 --- a/R/meeting.R +++ b/R/meeting.R @@ -22,7 +22,7 @@ #' \item{`start_date`}{the start date in ROC Taiwan calendar} #' \item{`url`}{the retrieved json url} #' \item{`variable_names`}{the variables of the tibble dataframe} -#' \item{`manual_info`}{the offical manual, \url{https://www.ly.gov.tw/Pages/List.aspx?nodeid=154}; or use legisTaiwan::get_variable_info("get_meetings")} +#' \item{`manual_info`}{the offical manual, \url{https://www.ly.gov.tw/Pages/List.aspx?nodeid=154}; or use get_variable_info("get_meetings")} #' \item{`data`}{a tibble dataframe, whose variables include: #' \describe{\item{`smeeting_date`}{會議日期} #' \item{`meeting_status`}{會議狀態} @@ -37,6 +37,7 @@ #' #'@importFrom attempt stop_if_all #'@importFrom jsonlite fromJSON +#'@importFrom withr with_options #' #'@export #' @@ -55,7 +56,7 @@ #'`end_date`, `url`, `variable_names`, `manual_info` and `data`. #' #'@note To retrieve the user manual and more information about variable of the data -#'frame, please use `legisTaiwan::get_variable_info("get_meetings")` or visit +#'frame, please use `get_variable_info("get_meetings")` or visit #'the API manual at \url{https://www.ly.gov.tw/Pages/List.aspx?nodeid=154}. #'資料似乎不一致,待確認。委員發言(取得最早時間不詳,待檢查。) #' @@ -67,28 +68,28 @@ get_meetings <- function(start_date = NULL, end_date = NULL, meeting_unit = NULL, verbose = TRUE) { - legisTaiwan::check_internet() - legisTaiwan::api_check(start_date = legisTaiwan::check_date(start_date), end_date = legisTaiwan::check_date(end_date)) + check_internet() + api_check(start_date = check_date(start_date), end_date = check_date(end_date)) set_api_url <- paste("https://www.ly.gov.tw/WebAPI/LegislativeSpeech.aspx?from=", start_date, "&to=", end_date, "&meeting_unit=", meeting_unit, "&mode=json", sep = "") tryCatch( { - json_df <- jsonlite::fromJSON(set_api_url) + with_options(list(timeout = max(1000, getOption("timeout"))),{json_df <- jsonlite::fromJSON(set_api_url)}) df <- tibble::as_tibble(json_df) attempt::stop_if_all(nrow(df) == 0, isTRUE, msg = "The query is unavailable.") - df["date_ad"] <- do.call("c", lapply(df$smeeting_date, legisTaiwan::transformed_date_meeting)) + df["date_ad"] <- do.call("c", lapply(df$smeeting_date, transformed_date_meeting)) if (isTRUE(verbose)) { cat(" Retrieved URL: \n", set_api_url, "\n") cat(" Retrieved via :", meeting_unit, "\n") - cat(" Retrieved date between:", as.character(legisTaiwan::check_date(start_date)), "and", as.character(legisTaiwan::check_date(end_date)), "\n") + cat(" Retrieved date between:", as.character(check_date(start_date)), "and", as.character(check_date(end_date)), "\n") cat(" Retrieved number:", nrow(df), "\n") } list_data <- list("title" = "the spoken meeting records", "query_time" = Sys.time(), "retrieved_number" = nrow(df), "meeting_unit" = meeting_unit, - "start_date_ad" = legisTaiwan::check_date(start_date), - "end_date_ad" = legisTaiwan::check_date(end_date), + "start_date_ad" = check_date(start_date), + "end_date_ad" = check_date(end_date), "start_date" = start_date, "end_date" = end_date, "url" = set_api_url, @@ -127,7 +128,7 @@ get_meetings <- function(start_date = NULL, end_date = NULL, meeting_unit = NULL #' \item{`start_date`}{the start date in ROC Taiwan calendar} #' \item{`url`}{the retrieved json url} #' \item{`variable_names`}{the variables of the tibble dataframe} -#' \item{`manual_info`}{the official manual, \url{https://data.ly.gov.tw/getds.action?id=8}; or use legisTaiwan::get_variable_info("get_caucus_meetings")} +#' \item{`manual_info`}{the official manual, \url{https://data.ly.gov.tw/getds.action?id=8}; or use get_variable_info("get_caucus_meetings")} #' \item{`data`}{a tibble dataframe, whose variables include: #' \describe{\item{`comYear`}{卷} #' \item{`comVolume`}{期} @@ -149,6 +150,7 @@ get_meetings <- function(start_date = NULL, end_date = NULL, meeting_unit = NULL #' #'@importFrom attempt stop_if_all #'@importFrom jsonlite fromJSON +#'@importFrom withr with_options #' #'@export #' @@ -165,7 +167,7 @@ get_meetings <- function(start_date = NULL, end_date = NULL, meeting_unit = NULL #'\\ifelse{html}{\\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\\strong{[Experimental]}} #' #'@note To retrieve the user manual and more information about variable of the data -#' frame, please use `legisTaiwan::get_variable_info("get_caucus_meetings")` +#' frame, please use `get_variable_info("get_caucus_meetings")` #' or visit the API manual at \url{https://data.ly.gov.tw/getds.action?id=8}. #' 議事類:提供公報之黨團協商資訊 (自第8屆第1會期起) #' @@ -177,26 +179,26 @@ get_meetings <- function(start_date = NULL, end_date = NULL, meeting_unit = NULL # get_caucus_meetings <- function(start_date = NULL, end_date = NULL, verbose = TRUE) { - legisTaiwan::check_internet() - legisTaiwan::api_check(start_date = legisTaiwan::transformed_date_meeting(start_date), - end_date = legisTaiwan::transformed_date_meeting(end_date)) + check_internet() + api_check(start_date = transformed_date_meeting(start_date), + end_date = transformed_date_meeting(end_date)) set_api_url <- paste("https://data.ly.gov.tw/odw/ID8Action.action?comYear=&comVolume=&comBookId=&term=&sessionPeriod=&sessionTimes=&meetingTimes=&meetingDateS=", start_date, "&meetingDateE=", end_date, "&fileType=json", sep = "") tryCatch( { - json_df <- jsonlite::fromJSON(set_api_url) + with_options(list(timeout = max(1000, getOption("timeout"))),{json_df <- jsonlite::fromJSON(set_api_url)}) df <- tibble::as_tibble(json_df$dataList) attempt::stop_if_all(nrow(df) == 0, isTRUE, msg = "The query is unavailable.") if (isTRUE(verbose)) { cat(" Retrieved URL: \n", set_api_url, "\n") - cat(" Retrieved date between:", as.character(legisTaiwan::transformed_date_meeting(start_date)), "and", as.character(legisTaiwan::transformed_date_meeting(end_date)), "\n") + cat(" Retrieved date between:", as.character(transformed_date_meeting(start_date)), "and", as.character(transformed_date_meeting(end_date)), "\n") cat(" Retrieved number:", nrow(df), "\n") } list_data <- list("title" = "the meeting records of cross-caucus session", "query_time" = Sys.time(), "retrieved_number" = nrow(df), - "start_date_ad" = legisTaiwan::transformed_date_meeting(start_date), - "end_date_ad" = legisTaiwan::transformed_date_meeting(end_date), + "start_date_ad" = transformed_date_meeting(start_date), + "end_date_ad" = transformed_date_meeting(end_date), "start_date" = start_date, "end_date" = end_date, "url" = set_api_url, @@ -233,7 +235,7 @@ get_caucus_meetings <- function(start_date = NULL, end_date = NULL, #' \item{`start_date`}{the start date in ROC Taiwan calendar} #' \item{`url`}{the retrieved json url} #' \item{`variable_names`}{the variables of the tibble dataframe} -#' \item{`manual_info`}{the official manual, \url{https://data.ly.gov.tw/getds.action?id=148}; or use legisTaiwan::get_variable_info("get_speech_video")} +#' \item{`manual_info`}{the official manual, \url{https://data.ly.gov.tw/getds.action?id=148}; or use get_variable_info("get_speech_video")} #' \item{`data`}{a tibble dataframe, whose variables include: #' \describe{\item{`term`}{屆別} #' \item{`sessionPeriod`}{會期} @@ -256,6 +258,7 @@ get_caucus_meetings <- function(start_date = NULL, end_date = NULL, #' #'@importFrom attempt stop_if_all #'@importFrom jsonlite fromJSON +#'@importFrom withr with_options #' #'@export #' @@ -269,10 +272,10 @@ get_caucus_meetings <- function(start_date = NULL, end_date = NULL, #'@details `get_speech_video` produces a list, which contains `title`, `query_time`, #'`retrieved_number`, `meeting_unit`, `start_date_ad`, `end_date_ad`, `start_date`, #'`end_date`, `url`, `variable_names`, `manual_info` and `data.` To retrieve the user -#'manual and more information about the data frame, please use `legisTaiwan::get_variable_info("get_speech_video")`. +#'manual and more information about the data frame, please use `get_variable_info("get_speech_video")`. #' #'@note To retrieve the user manual and more information about variable of the data -#' frame, please use `legisTaiwan::get_variable_info("get_speech_video")` +#' frame, please use `get_variable_info("get_speech_video")` #' or visit the API manual at \url{https://data.ly.gov.tw/getds.action?id=148}. #' 會議類:提供立法院院會及委員會之委員發言片段相關影片資訊 (自第9屆第1會期起)。 #' @@ -280,10 +283,10 @@ get_caucus_meetings <- function(start_date = NULL, end_date = NULL, #'`get_variable_info("get_speech_video")` get_speech_video <- function(start_date = NULL, end_date = NULL, verbose = TRUE) { - legisTaiwan::check_internet() - legisTaiwan::api_check(start_date = legisTaiwan::transformed_date_meeting(start_date), end_date = legisTaiwan::transformed_date_meeting(end_date)) + check_internet() + api_check(start_date = transformed_date_meeting(start_date), end_date = transformed_date_meeting(end_date)) # # 自第9屆第1會期起 2016 民國 105 - # queried_year <- format(legisTaiwan::transformed_date_meeting(start_date), format = "%Y") + # queried_year <- format(transformed_date_meeting(start_date), format = "%Y") # attempt::warn_if(queried_year < 2016, # isTRUE, # msg = paste("The query retrieved from", queried_year, "may not be complete.", "The data is only available from the 6th session of the 8th legislative term in 2015/104 in ROC.")) @@ -294,19 +297,19 @@ get_speech_video <- function(start_date = NULL, end_date = NULL, verbose = TRUE) "&meetingTime=&legislatorName=&fileType=json" , sep = "") tryCatch( { - json_df <- jsonlite::fromJSON(set_api_url) + with_options(list(timeout = max(1000, getOption("timeout"))),{json_df <- jsonlite::fromJSON(set_api_url)}) df <- tibble::as_tibble(json_df$dataList) attempt::stop_if_all(nrow(df) == 0, isTRUE, msg = "The query is unavailable.") if (isTRUE(verbose)) { cat(" Retrieved URL: \n", set_api_url, "\n") - cat(" Retrieved date between:", as.character(legisTaiwan::transformed_date_meeting(start_date)), "and", as.character(legisTaiwan::transformed_date_meeting(end_date)), "\n") + cat(" Retrieved date between:", as.character(transformed_date_meeting(start_date)), "and", as.character(transformed_date_meeting(end_date)), "\n") cat(" Retrieved number:", nrow(df), "\n") } list_data <- list("title" = "the meeting records of cross-caucus session", "query_time" = Sys.time(), "retrieved_number" = nrow(df), - "start_date_ad" = legisTaiwan::transformed_date_meeting(start_date), - "end_date_ad" = legisTaiwan::transformed_date_meeting(end_date), + "start_date_ad" = transformed_date_meeting(start_date), + "end_date_ad" = transformed_date_meeting(end_date), "start_date" = start_date, "end_date" = end_date, "url" = set_api_url, @@ -346,7 +349,7 @@ get_speech_video <- function(start_date = NULL, end_date = NULL, verbose = TRUE) #' \item{`start_date`}{the start date in ROC Taiwan calendar} #' \item{`url`}{the retrieved json url} #' \item{`variable_names`}{the variables of the tibble dataframe} -#' \item{`manual_info`}{the official manual, \url{https://data.ly.gov.tw/getds.action?id=7}; or use legisTaiwan::get_variable_info("get_public_debates")} +#' \item{`manual_info`}{the official manual, \url{https://data.ly.gov.tw/getds.action?id=7}; or use get_variable_info("get_public_debates")} #' \item{`data`}{a tibble dataframe, whose variables include: #' \describe{\item{`term`}{屆別} #' \item{`sessionPeriod`}{會期} @@ -365,6 +368,7 @@ get_speech_video <- function(start_date = NULL, end_date = NULL, verbose = TRUE) #' #'@importFrom attempt stop_if_all #'@importFrom jsonlite fromJSON +#'@importFrom withr with_options #' #'@export #' @@ -379,7 +383,7 @@ get_speech_video <- function(start_date = NULL, end_date = NULL, verbose = TRUE) #' #'@note #' To retrieve the user manual and more information about variable of the data -#' frame, please use `legisTaiwan::get_variable_info("get_public_debates")` +#' frame, please use `get_variable_info("get_public_debates")` #' or visit the API manual at \url{https://data.ly.gov.tw/getds.action?id=7}. #' 議事類: 提供公報之國是論壇資訊,並包含書面意見。自第8屆第1會期起,但實測資料從第10屆。 #' @@ -390,7 +394,7 @@ get_speech_video <- function(start_date = NULL, end_date = NULL, verbose = TRUE) #' Regarding Minguo calendar, please see \url{https://en.wikipedia.org/wiki/Republic_of_China_calendar}. get_public_debates <- function(term = NULL, session_period = NULL, verbose = TRUE) { - legisTaiwan::check_internet() + check_internet() if (is.null(term)) { set_api_url <- paste("https://data.ly.gov.tw/odw/ID7Action.action?term=", term, "&sessionPeriod=", @@ -412,7 +416,7 @@ get_public_debates <- function(term = NULL, session_period = NULL, verbose = TRU sep = "") tryCatch( { - json_df <- jsonlite::fromJSON(set_api_url) + with_options(list(timeout = max(1000, getOption("timeout"))),{json_df <- jsonlite::fromJSON(set_api_url)}) df <- tibble::as_tibble(json_df$dataList) attempt::stop_if_all(nrow(df) == 0, isTRUE, msg = "The query is unavailable.") if (isTRUE(verbose)) { @@ -441,60 +445,115 @@ get_public_debates <- function(term = NULL, session_period = NULL, verbose = TRU #' #'@author David Yen-Chieh Liao #' -#'@param term numeric or null. The data is only available from 8th term. -#'The default is set to 8. 參數必須為數值。資料從自第8屆起,預設值為8。 +#'@param term numeric or null. Data is available only from the 8th term. +#'The default is set to 10. 參數必須為數值。提供委員會會議審查之議案項目。(自第10屆第1會期起) +#' +#'@param session_period integer, numeric or NULL. +#'`review_session_info()` provides each session period's available options based on the +#' Minguo (Taiwan) calendar. +#' +#'@param verbose logical. This indicates whether `get_executive_response` should +#'print a detailed output during data retrieval. Default is TRUE. +#' +#'@return A list containing: +#' \item{`title`}{Records of questions answered by executives} +#' \item{`query_time`}{Time of query} +#' \item{`retrieved_number`}{Total number of observations} +#' \item{`retrieved_term`}{Queried term} +#' \item{`url`}{Retrieved JSON URL} +#' \item{`variable_names`}{Variables of the tibble dataframe} +#' \item{`manual_info`}{Official manual, \url{https://data.ly.gov.tw/getds.action?id=46}; or use get_variable_info("get_committee_record")} +#' \item{`data`}{A tibble dataframe with variables: +#' \describe{ +#' \item{`term`}{Term number} +#' \item{`sessionPeriod`}{Session} +#' \item{`meetingNo`}{Meeting number} +#' \item{`billNo`}{Bill number} +#' \item{`selectTerm`}{Term selection filter} +#' } +#' } #' -#'@param session_period integer, numeric or NULL. Available -#'options for the session is: 1, 2, 3, 4, 5, 6, 7, and 8. The default is set to NULL. 參數必須為數值。 -#'`review_session_info()` generates each session period available option period -#' in Minguo (Taiwan) calendar. +#'@importFrom attempt stop_if_all +#'@importFrom jsonlite fromJSON +#'@importFrom withr with_options #' -#'@param verbose logical, indicates whether `get_executive_response` should -#'print out detailed output when retrieving the data. The default is TRUE -#' -#'@return list contains: \describe{ -#' \item{`title`}{the records of the questions answered by the executives} -#' \item{`query_time`}{the queried time} -#' \item{`retrieved_number`}{the total number of observations} -#' \item{`retrieved_term`}{the queried term} -#' \item{`url`}{the retrieved json url} -#' \item{`variable_names`}{the variables of the tibble dataframe} -#' \item{`manual_info`}{the official manual, \url{https://data.ly.gov.tw/getds.action?id=46}; or use legisTaiwan::get_variable_info("get_committee_record")} -#' \item{`data`}{a tibble dataframe, whose variables include: -#' \describe{\item{`term`}{屆別} -#' \item{`sessionPeriod`}{會期} -#' \item{`meetingNo`}{會議編號} -#' \item{`billNo`}{議案編號} -#' \item{`selectTerm`}{屆別期別篩選條件} +#'@export +#' +#'@examples +#' ## Query the committee record by term and session period. +#' ## 輸入「立委屆期」與「會期」下載「委員會審議之議案」 +#'get_committee_record(term = 10, session_period = 1) +#' +#'@details `get_committee_record` provides a list which includes `title`, +#'`query_time`, `retrieved_number`, `retrieved_term`, `url`, `variable_names`, +#' `manual_info`, and `data`. +#' +#'@note +#' To access the user manual and more information about the data frame's variables, +#' please refer to `get_variable_info("get_committee_record")` or check the API manual at +#' \url{https://data.ly.gov.tw/getds.action?id=46}. +#' This provides agenda items reviewed in committee meetings (from the 10th term, 1st session onwards). +#' +#'@seealso +#'`get_variable_info("get_committee_record")`, `review_session_info()` +#' The Records of Reviewed Items in the Committees 委員會會議審查之議案項目 +#' +#'@author David Yen-Chieh Liao +#' +#'@param term numeric or null. Data is available only from the 8th term. +#'The default is set to 10. 參數必須為數值。提供委員會會議審查之議案項目。(自第10屆第1會期起) +#' +#'@param session_period integer, numeric or NULL. +#'`review_session_info()` provides each session period's available options based on the +#' Minguo (Taiwan) calendar. +#' +#'@param verbose logical. This indicates whether `get_executive_response` should +#'print a detailed output during data retrieval. Default is TRUE. +#' +#'@return A list containing: +#' \item{`title`}{Records of questions answered by executives} +#' \item{`query_time`}{Time of query} +#' \item{`retrieved_number`}{Total number of observations} +#' \item{`retrieved_term`}{Queried term} +#' \item{`url`}{Retrieved JSON URL} +#' \item{`variable_names`}{Variables of the tibble dataframe} +#' \item{`manual_info`}{Official manual, \url{https://data.ly.gov.tw/getds.action?id=46}; or use get_variable_info("get_committee_record")} +#' \item{`data`}{A tibble dataframe with variables: +#' \describe{ +#' \item{`term`}{Term number} +#' \item{`sessionPeriod`}{Session} +#' \item{`meetingNo`}{Meeting number} +#' \item{`billNo`}{Bill number} +#' \item{`selectTerm`}{Term selection filter} #' } #' } -#' } #' #'@importFrom attempt stop_if_all #'@importFrom jsonlite fromJSON +#'@importFrom withr with_options #' #'@export #' #'@examples -#' ## query the committee record by term and the session period. +#' ## Query the committee record by term and session period. #' ## 輸入「立委屆期」與「會期」下載「委員會審議之議案」 -#'get_committee_record(term = 8, session_period = 1) +#'get_committee_record(term = 10, session_period = 1) #' -#'@details `get_committee_record` produces a list, which contains `title`, +#'@details `get_committee_record` provides a list which includes `title`, #'`query_time`, `retrieved_number`, `retrieved_term`, `url`, `variable_names`, -#' `manual_info` and `data`. +#' `manual_info`, and `data`. #' #'@note -#' To retrieve the user manual and more information -#' about variable of the data frame, please use `legisTaiwan::get_variable_info("get_committee_record")` -#' or visit the API manual at \url{https://data.ly.gov.tw/getds.action?id=46}. -#' 議事類: 提供委員會會議審查之議案項目 (自第8屆第1會期起)。 +#' To access the user manual and more information about the data frame's variables, +#' please refer to `get_variable_info("get_committee_record")` or check the API manual at +#' \url{https://data.ly.gov.tw/getds.action?id=46}. +#' This provides agenda items reviewed in committee meetings (from the 10th term, 1st session onwards). #' #'@seealso #'`get_variable_info("get_committee_record")`, `review_session_info()` -get_committee_record <- function(term = 8, session_period = NULL, verbose = TRUE) { - legisTaiwan::check_internet() +get_committee_record <- function(term = 10, session_period = NULL, verbose = TRUE) { + check_internet() if (is.null(term)) { set_api_url <- paste("https://data.ly.gov.tw/odw/ID46Action.action?term=", term, "&sessionPeriod=", @@ -514,7 +573,7 @@ get_committee_record <- function(term = 8, session_period = NULL, verbose = TRUE "&sessionTimes=01&meetingTimes=&fileType=json", sep = "") tryCatch( { - json_df <- jsonlite::fromJSON(set_api_url) + with_options(list(timeout = max(1000, getOption("timeout"))),{json_df <- jsonlite::fromJSON(set_api_url)}) df <- tibble::as_tibble(json_df$dataList) attempt::stop_if_all(nrow(df) == 0, isTRUE, msg = "The query is unavailable.") if (isTRUE(verbose)) { diff --git a/R/parlquestions.R b/R/parlquestions.R index d7da8909..1f390be0 100644 --- a/R/parlquestions.R +++ b/R/parlquestions.R @@ -1,82 +1,78 @@ -#'The Records of Parliamentary Questions 委員質詢事項資訊 +#' The Records of Parliamentary Questions 委員質詢事項資訊 #' -#'@author David Liao (davidycliao@@gmail.com) +#' @author David Liao (davidycliao@@gmail.com) #' -#'@param term numeric or NULL. The data is only available from 8th term. -#'The default is set to 8. 參數必須為數值。資料從自第8屆起,預設值為8。 +#' @param term numeric or NULL. The data is only available from 8th term. +#' The default is set to 8. 參數必須為數值。資料從自第8屆起,預設值為8。 #' -#'@param session_period integer, numeric or NULL. Available -#'options for the session is: 1, 2, 3, 4, 5, 6, 7, and 8. The default is set to 8 參數必須為數值。 -#'`review_session_info()` generates each session period available option period +#' @param session_period integer, numeric or NULL. Available +#' options for the session is: 1, 2, 3, 4, 5, 6, 7, and 8. The default is set to 8 參數必須為數值。 +#' `review_session_info()` generates each session period available option period #' in Minguo (Taiwan) calendar. #' -#'@param verbose logical, indicates whether `get_parlquestions` should print out -#'detailed output when retrieving the data. The default is TRUE -#' -#'@return list contains: \describe{ -#'\item{`title`}{the records of parliamentary questions} -#'\item{`query_time`}{the queried time} -#'\item{`retrieved_number`}{the total number of observations} -#'\item{`retrieved_term`}{the queried term} -#'\item{`url`}{the retrieved json url} -#'\item{`variable_names`}{the variables of the tibble dataframe} -#'\item{`manual_info`}{the offical manual from \url{https://data.ly.gov.tw/getds.action?id=6}, or use legisTaiwan::get_variable_info("get_parlquestions")} -#'\item{`data`}{a tibble dataframe, whose variables include: -#' \describe{\item{`term`}{屆別} -#' \item{`sessionPeriod`}{會期} -#' \item{`sessionTimes`}{會次} -#' \item{`item`}{項目} -#' \item{`selectTerm`}{屆別期別篩選條件} -#' } -#' } -#' } -#' -#'@importFrom attempt stop_if_all -#'@importFrom jsonlite fromJSON -#' -#'@export -#' -#'@examples -#' ## query parliamentary questions by term. +#' @param verbose logical, indicates whether `get_parlquestions` should print out +#' detailed output when retrieving the data. The default is TRUE. +#' +#' @return A list containing: +#' \describe{ +#' \item{`title`}{the records of parliamentary questions} +#' \item{`query_time`}{the queried time} +#' \item{`retrieved_number`}{the total number of observations} +#' \item{`retrieved_term`}{the queried term} +#' \item{`url`}{the retrieved json url} +#' \item{`variable_names`}{the variables of the tibble dataframe} +#' \item{`manual_info`}{the offical manual from \url{https://data.ly.gov.tw/getds.action?id=6}, or use get_variable_info("get_parlquestions")} +#' \item{`data`}{a tibble dataframe, whose variables include: +#' \describe{ +#' \item{`term`}{屆別} +#' \item{`sessionPeriod`}{會期} +#' \item{`sessionTimes`}{會次} +#' \item{`item`}{項目} +#' \item{`selectTerm`}{屆別期別篩選條件} +#' } +#' } +#' } +#' +#' @importFrom attempt stop_if_all +#' @importFrom jsonlite fromJSON +#' @importFrom withr with_options +#' @export +#' +#' @examples +#' ## Query parliamentary questions by term. #' ## 輸入「立委會期」下載立委質詢資料 -#'get_parlquestions(term = 8) +#' get_parlquestions(term = 8) #' -#' ## query parliamentary questions by term. +#' ## Query parliamentary questions by term and session period. #' ## 輸入「立委屆期」與「會期」下載立委質詢資料 -#'get_parlquestions(term = 8, session_period = 2) -#' -#' ## query parliamentary questions by term. -#' ## 輸入「空白」下載立委全部質詢資料 -#'get_parlquestions(term = 8, session_period = 2) +#' get_parlquestions(term = 8, session_period = 2) #' -#'@details get_parlquestions` produces a list, which contains `title`, -#'`query_time`, `retrieved_number`, `retrieved_term`, `url`, `variable_names`, -#' `manual_info` and `data`. +#' @details `get_parlquestions` produces a list, which contains `title`, +#' `query_time`, `retrieved_number`, `retrieved_term`, `url`, `variable_names`, +#' `manual_info`, and `data`. #' -#'@note To retrieve the user manual and more information about variable of the data -#' frame, please use `legisTaiwan::get_variable_info("get_parlquestions")` +#' @note To retrieve the user manual and more information about variable of the data +#' frame, please use `get_variable_info("get_parlquestions")` #' or visit the API manual at \url{https://data.ly.gov.tw/getds.action?id=6}. #' 質詢類: 提供議事日程本院委員之質詢事項資訊(自第8屆第1會期起)。 #' -#'@seealso -#'`get_variable_info("get_parlquestions")` - +#' @seealso `get_variable_info("get_parlquestions")` get_parlquestions <- function(term = 8, session_period = NULL, verbose = TRUE) { - legisTaiwan::check_internet() + check_internet() if (is.null(term)) { - options(timeout = max(1000, getOption("timeout"))) - set_api_url <- paste("https://data.ly.gov.tw/odw/ID6Action.action?term=", term, - "&sessionPeriod=", - "&sessionTimes=&item=&fileType=json", sep = "") - message(" term is not defined...\n You are now requesting full data from the API. Please make sure your connectivity is stable until its completion.\n") + set_api_url <- paste("https://data.ly.gov.tw/odw/ID6Action.action?term=", term, + "&sessionPeriod=", + "&sessionTimes=&item=&fileType=json", sep = "") + message(" term is not defined...\n You are now requesting full data from the API. Please make sure your connectivity is stable until its completion.\n") } else { attempt::stop_if_all(term, is.character, msg = "use numeric format only.") if (length(term) == 1) { term <- sprintf("%02d", as.numeric(term))} else if (length(term) > 1) { - options(timeout = max(1000, getOption("timeout"))) - term <- paste(sprintf("%02d", as.numeric(term)), collapse = "&") - message("The API is unable to query multiple terms and the retrieved data might not be complete.")} + options(timeout = max(1000, getOption("timeout"))) + term <- paste(sprintf("%02d", as.numeric(term)), collapse = "&") + stop("The API is unable to query multiple terms.") + } } set_api_url <- paste("https://data.ly.gov.tw/odw/ID6Action.action?term=", term, "&sessionPeriod=", @@ -84,7 +80,7 @@ get_parlquestions <- function(term = 8, session_period = NULL, verbose = TRUE) { "&sessionTimes=&item=&fileType=json", sep = "") tryCatch( { - json_df <- jsonlite::fromJSON(set_api_url) + with_options(list(timeout = max(1000, getOption("timeout"))),{json_df <- jsonlite::fromJSON(set_api_url)}) df <- tibble::as_tibble(json_df$dataList) attempt::stop_if_all(nrow(df) == 0, isTRUE, msg = "The query is unavailable.") if (isTRUE(verbose)) { @@ -151,7 +147,7 @@ get_parlquestions <- function(term = 8, session_period = NULL, verbose = TRUE) { #' #'@importFrom attempt stop_if_all #'@importFrom jsonlite fromJSON -#' +#'@importFrom withr with_options #'@export #' #'@examples @@ -162,11 +158,11 @@ get_parlquestions <- function(term = 8, session_period = NULL, verbose = TRUE) { #'@details **`get_executive_response`** produces a list, which contains `title`, #'`query_time`, `retrieved_number`, `retrieved_term`, `url`, `variable_names`, #' `manual_info` and `data`. To retrieve the user manual and more information, please -#' use `legisTaiwan::get_variable_info("get_executive_response")`. +#' use `get_variable_info("get_executive_response")`. #' #' #'#'@note To retrieve the user manual and more information about variable of the data -#' frame, please use `legisTaiwan::get_variable_info("get_executive_response")` +#' frame, please use `get_variable_info("get_executive_response")` #' or visit the API manual at \url{https://data.ly.gov.tw/getds.action?id=2}. #' 質詢類: 提供公報質詢事項行政院答復資訊 (自第8屆第1會期起)。 #' @@ -175,21 +171,20 @@ get_parlquestions <- function(term = 8, session_period = NULL, verbose = TRUE) { get_executive_response <- function(term = NULL, session_period = NULL, verbose = TRUE) { - legisTaiwan::check_internet() + check_internet() if (is.null(term)) { - options(timeout = max(1000, getOption("timeout"))) - set_api_url <- paste("https://data.ly.gov.tw/odw/ID2Action.action?term=", - term, "&sessionPeriod=", - "&sessionTimes=&item=&fileType=json", sep = "") - message(" term is not defined...\n You are now requesting full data from the API. Please make sure your connectivity is stable until its completion.\n") + set_api_url <- paste("https://data.ly.gov.tw/odw/ID2Action.action?term=", + term, "&sessionPeriod=", + "&sessionTimes=&item=&fileType=json", sep = "") + message(" term is not defined...\n You are now requesting full data from the API. Please make sure your connectivity is stable until its completion.\n") } else { attempt::stop_if_all(term, is.character, msg = "use numeric format only.") if (length(term) == 1) { term <- sprintf("%02d", as.numeric(term))} else if (length(term) > 1) { - options(timeout = max(1000, getOption("timeout"))) term <- paste(sprintf("%02d", as.numeric(term)), collapse = "&") - message("The API is unable to query multiple terms and the retrieved data might not be complete.")} + stop("The API is unable to query multiple terms.") + } } set_api_url <- paste("https://data.ly.gov.tw/odw/ID2Action.action?term=", term, "&sessionPeriod=", @@ -197,7 +192,7 @@ get_executive_response <- function(term = NULL, session_period = NULL, verbose = "&sessionTimes=&item=&fileType=json", sep = "") tryCatch( { - json_df <- jsonlite::fromJSON(set_api_url) + with_options(list(timeout = max(1000, getOption("timeout"))),{json_df <- jsonlite::fromJSON(set_api_url)}) df <- tibble::as_tibble(json_df$dataList) attempt::stop_if_all(nrow(df) == 0, isTRUE, msg = "The query is unavailable.") if (isTRUE(verbose)) { diff --git a/R/utils.R b/R/utils.R index ea44f72f..b499a690 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,19 +1,38 @@ -### Package Utility Functions - -#' @export .onAttach -.onAttach <- function(...) { - packageStartupMessage("## legisTaiwan ###") - packageStartupMessage("## An R package connecting to the Taiwan Legislative API. ###") - +#' Validate Date Formats +#' +#' This function checks if the provided start and end dates are in the correct numeric format. +#' +#' @param start_date A string representing the start date. Expected format is numeric, e.g., "1090101". +#' @param end_date A string representing the end date. Expected format is numeric, e.g., "1090101". +#' @return NULL. If the dates are not in the expected format, an error is thrown. +#' @examples +#' # This should throw an error: +#' \dontrun{ +#' validate_dates_format("10901", "1100101") +#' } +#' @keywords internal +validate_dates_format <- function(start_date, end_date) { + valid_date_format <- function(date) { + return(grepl("^\\d{7}$", date)) + } + + if (!valid_date_format(start_date) || !valid_date_format(end_date)) { + stop("Dates should be in numeric format. E.g., 1090101.") + } } -#'A Check for the Website Availability I + +#' Check for the Website Availability I #' -#'@param site https://data.ly.gov.tw/index.action +#' This function checks the availability of a specified website by trying to read +#' the first line of the site's content. #' -#'@seealso -#' `check_internet()`, - +#' @param site A website URL to check. Default is "https://data.ly.gov.tw/index.action". +#' +#' @seealso +#' `check_internet()` +#' +#' @keywords internal website_availability <- function(site = "https://data.ly.gov.tw/index.action") { tryCatch({ readLines(site, n = 1) @@ -23,15 +42,16 @@ website_availability <- function(site = "https://data.ly.gov.tw/index.action") { error = function(e) FALSE) } -#'A Check for the Website Availability II +#' Check for the Website Availability II #' -#'@param site https://npl.ly.gov.tw/do/www/appDate?status=0&expire=02&startYear=0 +#' This function checks the availability of a specified website by trying to read +#' the first line of the site's content. #' -#'@seealso -#' `check_internet()`, `website_availability()` -#'@export - -website_availability2 <- function(site = "https://npl.ly.gov.tw/do/www/appDate?status=0&expire=02&startYear=0n") { +#' @param site A website URL to check. Default is "https://npl.ly.gov.tw/do/www/appDate?status=0&expire=02&startYear=0". +#' +#' @keywords internal +#' @seealso `check_internet()` and `website_availability()`. +website_availability2 <- function(site = "https://npl.ly.gov.tw/do/www/appDate?status=0&expire=02&startYear=0") { tryCatch({ readLines(site, n = 1) TRUE @@ -51,11 +71,10 @@ website_availability2 <- function(site = "https://npl.ly.gov.tw/do/www/appDate?s #' #'@importFrom curl has_internet #' -#'@export - +#'@keywords internal check_internet <- function(x = curl::has_internet()) { attempt::stop_if_not(.x = x, - msg = "Please check the internet connetion") + msg = "Please check the internet connection") } #' A General Check for Taiwan Legislative Yuan API @@ -65,46 +84,61 @@ check_internet <- function(x = curl::has_internet()) { #'@param end_date end_date is inherited from global env. #' #'@importFrom attempt stop_if_not -#' -#'@export - +#'@keywords internal +# api_check <- function(start_date = start_date, end_date = end_date) { +# attempt::stop_if_all(start_date > as.Date(Sys.time()), +# isTRUE, msg = "The start date should not be after the system time") +# attempt::stop_if_all(end_date > as.Date(Sys.time()), +# isTRUE, msg = "The end date should not be after the system time") +# attempt::stop_if_all(start_date, is.character, msg = "use numeric format") +# attempt::stop_if_all(end_date, is.character, msg = "use numeric format") +# attempt::stop_if_all(start_date, is.null, msg = "start_date is missing") +# attempt::stop_if_all(end_date, is.null, msg = "end_date is missing") +# attempt::stop_if_all(end_date > start_date, isFALSE, +# msg = paste("The start date, ", start_date, ",", " should not be later than the end date, ", +# end_date, ".", sep = "")) +# } api_check <- function(start_date = start_date, end_date = end_date) { + attempt::stop_if_all(start_date > as.Date(Sys.time()), - isTRUE, msg = "The start date should not be after the system time") + isTRUE, msg = "The start date should not be after the current system time.") attempt::stop_if_all(end_date > as.Date(Sys.time()), - isTRUE, msg = "The end date should not be after the system time") - attempt::stop_if_all(start_date, is.character, msg = "use numeric format") - attempt::stop_if_all(end_date, is.character, msg = "use numeric format") - attempt::stop_if_all(start_date, is.null, msg = "start_date is missing") - attempt::stop_if_all(end_date, is.null, msg = "end_date is missing") + isTRUE, msg = "The end date should not be after the current system time.") + attempt::stop_if_all(start_date, is.character, + msg = "Use numeric format for start_date.") + attempt::stop_if_all(end_date, is.character, + msg = "Use numeric format for end_date.") + attempt::stop_if_all(start_date, is.null, + msg = "The parameter 'start_date' is missing.") + attempt::stop_if_all(end_date, is.null, + msg = "The parameter 'end_date' is missing.") attempt::stop_if_all(end_date > start_date, isFALSE, - msg = paste("The start date,", start_date, ",", "should not be later than the end date,", - end_date, ".", sep = " ")) + msg = paste("The start date, ", start_date, ",", " should not be later than the end date, ", + end_date, ".", sep = "")) } #' Transforming Minguo (Taiwan) Calendar to A.D. Calendar I #' -#'@description `transformed_date_meeting()` transforms Minguo (Taiwan) Calendar -#'to A.D. calendar in POSIXct for `get_meetings()`, `get_caucus_meetings()`, -#'and `get_speech_video()`, +#' @description `transformed_date_meeting()` transforms Minguo (Taiwan) Calendar +#' to A.D. calendar in POSIXct for `get_meetings()`, `get_caucus_meetings()`, +#' and `get_speech_video()`, #' +#' @param roc_date Date format in Minguo (Taiwan) calendar (e.g., "105/05/31") as a +#' string vector #' -#'@param roc_date Date format in Minguo (Taiwan) calendar (e.g., "105/05/31") as a -#'string vector +#' @return date in POSIXct #' -#'@return date in POSIXct -#' -#'@importFrom stringr str_split_1 +#' @importFrom stringr str_split_1 #' -#'@export +#' @keywords internal #' -#'@examples -#'transformed_date_meeting("105/05/31") +#' @examples +#' \dontrun{ +#' transformed_date_meeting("105/05/31") +#' } #' -#'@details `check_date` transforms ROC date to a date in POSIXct, e.g. "105/05/31" to "2016-05-31". - - +#' @details `check_date` transforms ROC date to a date in POSIXct, e.g. "105/05/31" to "2016-05-31". transformed_date_meeting <- function(roc_date) { roc_date <- stringr::str_split_1(roc_date, "/") date_ad <- as.Date(as.POSIXct(paste(as.numeric(roc_date[1]) + 1911, @@ -117,17 +151,21 @@ transformed_date_meeting <- function(roc_date) { #' Transforming Minguo (Taiwan) Calendar to A.D. Calendar II #' -#'@description `transformed_date_meeting()` transforms Minguo (Taiwan) Calendar -#'to A.D. format in POSIXct for `get_bill()`, e.g. "1050531" to "2016-05-31". +#' @description `transformed_date_meeting()` transforms Minguo (Taiwan) Calendar +#' to A.D. format in POSIXct for `get_bill()`, e.g. "1050531" to "2016-05-31". #' -#'@param roc_date date format in Taiwan ROC calendar (e.g., "1050531") in a character vector +#' @param roc_date date format in Taiwan ROC calendar (e.g., "1050531") in a character vector #' -#'@return date in POSIXct +#' @return date in POSIXct #' -#'@importFrom stringr str_sub +#' @importFrom stringr str_sub #' -#'@export - +#' @keywords internal +#' +#' @examples +#' \dontrun{ +#' transformed_date_bill("1050531") +#' } transformed_date_bill <- function(roc_date) { day <- stringr::str_sub(roc_date, -2, -1) month <- stringr::str_sub(roc_date, -4, -3) @@ -151,8 +189,7 @@ transformed_date_bill <- function(roc_date) { #' #'@importFrom stringr str_sub #' -#'@export - +#'@keywords internal check_date <- function(roc_date) { day <- stringr::str_sub(roc_date, -2, -1) month <- stringr::str_sub(roc_date, -4, -3) @@ -177,13 +214,14 @@ check_date <- function(roc_date) { #' #'@importFrom stringr str_split_1 #' -#'@export +#'@keywords internal #' -#'@examples -#'check_date2("105/05/31") +#' @examples +#' \dontrun{ +#' check_date2("105/05/31") +#' } #' #'@details `check_date` transforms ROC date to a date in POSIXct, e.g. "105/05/31" to "2016-05-31". - check_date2 <- function(roc_date) { roc_date <- stringr::str_split_1(roc_date, "/") date_ad <- as.Date(as.POSIXct(paste(as.numeric(roc_date[1]) + 1911, diff --git a/R/zzz.R b/R/zzz.R new file mode 100644 index 00000000..31493d64 --- /dev/null +++ b/R/zzz.R @@ -0,0 +1,8 @@ +### Package Utility Functions +#' On package attach, display a startup message to the user. +#' +#' @keywords internal +.onAttach <- function(...) { + packageStartupMessage("## legisTaiwan ##") + packageStartupMessage("## An R package connecting to the Taiwan Legislative API. ##") +} diff --git a/README.md b/README.md index aea4ad94..81755306 100644 --- a/README.md +++ b/README.md @@ -20,9 +20,8 @@ coverage](https://github.com/davidycliao/legisTaiwan/actions/workflows/test-cove `legisTaiwan` is designed to download the real-time archives of Taiwan legislative data easily. This package includes many streamlined functions to access [Taiwan Legislative Yuan -API](https://data.ly.gov.tw/index.action) and efficiently perform -analysis and natural language processing tasks in R without any hassle -or runarounds. +API](https://data.ly.gov.tw/index.action) in R without any hassle or +runarounds. @@ -37,21 +36,21 @@ remotes::install_github("davidycliao/legisTaiwan", force = TRUE) ``` r library(legisTaiwan) -#> ## legisTaiwan ### -#> ## An R package connecting to the Taiwan Legislative API. ### +#> ## legisTaiwan ## +#> ## An R package connecting to the Taiwan Legislative API. ## ``` ## Caution: