Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestguevarra committed Apr 5, 2024
1 parent 8060fcf commit 9198062
Show file tree
Hide file tree
Showing 14 changed files with 223 additions and 14 deletions.
49 changes: 49 additions & 0 deletions R/pagasa_cyclones.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#'
#' Retrieve download links for yearly cyclones reports
#'
#' @param url
#'
#' @returns A vector of URL links to the yearly cyclones reports
#'
#'

cyclones_get_report_links <- function(url = "https://www.pagasa.dost.gov.ph/tropical-cyclone/publications/annual-report") {
httr::config(ssl_verifypeer = 0L) |>
httr::set_config()

url_session <- rvest::session(url)

url_session |>
rvest::html_elements(css = ".panel .panel-body li a") |>
rvest::html_attr(name = "href")
}


#'
#' Download cyclones reports
#'
#' @param url_link
#' @param directory
#'
#' @returns File downloaded to specified path (invisible). A character value for
#' path to downloaded file
#'

cyclones_download_report <- function(url_link, directory) {
## Check if directory exists; if not create ----
if (!dir.exists(directory))
dir.create(directory, showWarnings = FALSE, recursive = TRUE)

## Create file path to download ----
path <- file.path(directory, basename(url_link))

## Download file/s ----
Map(
f = download.file,
url = as.list(url_link),
destfile = as.list(path)
)

## Return path/s ----
path
}
6 changes: 2 additions & 4 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ knitr::opts_chunk$set(
)
```

# A template R workflow for general data analysis
# pagasa_db:

<!-- badges: start -->
<!-- badges: end -->
Expand All @@ -20,14 +20,12 @@ This repository is a template for a [`docker`](https://www.docker.com/get-starte

## About the Project



## Repository Structure

The project repository is structured as follows:

```
sc-policy-review
pagasa_db
|-- .github/
|-- workflows/
|-- data/
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

<!-- README.md is generated from README.Rmd. Please edit that file -->

# A template R workflow for general data analysis
# pagasa_db:

<!-- badges: start -->
<!-- badges: end -->
Expand All @@ -18,7 +18,7 @@ This repository is a template for a

The project repository is structured as follows:

sc-policy-review
pagasa_db
|-- .github/
|-- workflows/
|-- data/
Expand Down
6 changes: 6 additions & 0 deletions _targets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cyclones:
script: _targets_cyclones.R
store: _targets_cyclones
heat:
script: _targets_heat.R
store: _targets_heat
68 changes: 68 additions & 0 deletions _targets_cyclones.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
################################################################################
#
# General Targets Workflow
#
################################################################################

## Load libraries and custom functions -----------------------------------------
suppressPackageStartupMessages(source("packages.R"))
for (f in list.files(here::here("R"), full.names = TRUE)) source (f)

## Create targets --------------------------------------------------------------

### Download targets ----

download_targets <- tar_plan(
tar_target(
name = cyclone_reports_links,
command = cyclones_get_report_links()
),
tar_target(
name = cyclone_reports_download_files,
command = cyclones_download_report(
url_link = cyclone_reports_links,
directory = "data-raw/cyclones"
),
pattern = map(cyclone_reports_links),
format = "file"
)
)

### Data targets
data_targets <- tar_plan(

)


### Processing targets
processing_targets <- tar_plan(

)


### Analysis targets
analysis_targets <- tar_plan(

)


### Output targets
output_targets <- tar_plan(

)


### Reporting targets
report_targets <- tar_plan(

)


### Deploy targets
deploy_targets <- tar_plan(

)


## List targets ----------------------------------------------------------------
all_targets()
5 changes: 5 additions & 0 deletions _targets_cyclones/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*
!.gitignore
!meta
meta/*
!meta/meta
20 changes: 20 additions & 0 deletions _targets_cyclones/meta/meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name|type|data|command|depend|seed|path|time|size|bytes|format|repository|iteration|parent|children|seconds|warnings|error
.Random.seed|object|1617e1b0d9b19ac3|||||||||||||||
all_targets|function|b6dd8c08c18a0132|||||||||||||||
analysis_targets|object|ef46db3751d8e999|||||||||||||||
cyclone_reports_download_files|pattern|0aba2b50cff34e6a|f6ca15f19248c12f||1643365027||||50315072|file|local|vector||cyclone_reports_download_files_1342b9b0*cyclone_reports_download_files_3de468d2*cyclone_reports_download_files_1f710163*cyclone_reports_download_files_3a21c11c|111.33||
cyclone_reports_download_files_1342b9b0|branch|60729b809d91b79c|f6ca15f19248c12f|06035e6aa816dd5c|1498400784|data-raw/cyclones/PAGASA_ARTC_2017.pdf|t19818.7964824806s|5313c5a66b4edadc|5460545|file|local|vector|cyclone_reports_download_files||31.722||
cyclone_reports_download_files_1f710163|branch|de688a74a6ff77fd|f6ca15f19248c12f|06035e6aa816dd5c|226425042|data-raw/cyclones/PAGASA_ARTC_2019.pdf|t19818.7971196276s|65928e670053fd1b|12015407|file|local|vector|cyclone_reports_download_files||26.517||
cyclone_reports_download_files_3a21c11c|branch|db3ea6470ddc52b2|f6ca15f19248c12f|06035e6aa816dd5c|-825356566|data-raw/cyclones/PAGASA_ARTC_2020.pdf|t19818.7974045428s|a566e253d0d1ff29|15191585|file|local|vector|cyclone_reports_download_files||24.597||
cyclone_reports_download_files_3de468d2|branch|bc6d333cb1096ac5|f6ca15f19248c12f|06035e6aa816dd5c|1448407431|data-raw/cyclones/PAGASA_ARTC_2018.pdf|t19818.7968125359s|dab08093bc4d0be0|17647535|file|local|vector|cyclone_reports_download_files||28.494||
cyclone_reports_links|stem|976139e21c1457e1|50c3fa41d6ec29a2|3c2674030d3a4120|-147333770||t19818.7875375608s|4859fc503e8e9e17|155|rds|local|vector|||4.655||
cyclones_download_report|function|cb072b709121376a|||||||||||||||
cyclones_get_report_links|function|4ae6238e54fa5d98|||||||||||||||
data_targets|object|ef46db3751d8e999|||||||||||||||
deploy_targets|object|ef46db3751d8e999|||||||||||||||
download_targets|object|d135cd3318e807a0|||||||||||||||
f|object|2cca7bbc806ca10a|||||||||||||||
get_cyclone_report_links|function|4ae6238e54fa5d98|||||||||||||||
output_targets|object|ef46db3751d8e999|||||||||||||||
processing_targets|object|ef46db3751d8e999|||||||||||||||
report_targets|object|ef46db3751d8e999|||||||||||||||
50 changes: 50 additions & 0 deletions _targets_heat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
################################################################################
#
# General Targets Workflow
#
################################################################################

## Load libraries and custom functions -----------------------------------------
suppressPackageStartupMessages(source("packages.R"))
for (f in list.files(here::here("R"), full.names = TRUE)) source (f)

## Create targets and list targets objects -------------------------------------

### Data targets
data_targets <- tar_plan(

)


### Processing targets
processing_targets <- tar_plan(

)


### Analysis targets
analysis_targets <- tar_plan(

)


### Output targets
output_targets <- tar_plan(

)


### Reporting targets
report_targets <- tar_plan(

)


### Deploy targets
deploy_targets <- tar_plan(

)


## List targets
all_targets()
Binary file added data-raw/cyclones/PAGASA_ARTC_2017.pdf
Binary file not shown.
Binary file added data-raw/cyclones/PAGASA_ARTC_2018.pdf
Binary file not shown.
Binary file added data-raw/cyclones/PAGASA_ARTC_2019.pdf
Binary file not shown.
Binary file added data-raw/cyclones/PAGASA_ARTC_2020.pdf
Binary file not shown.
File renamed without changes.
29 changes: 21 additions & 8 deletions renv.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"R": {
"Version": "4.3.1",
"Version": "4.3.3",
"Repositories": [
{
"Name": "CRAN",
Expand Down Expand Up @@ -193,13 +193,13 @@
},
"cpp11": {
"Package": "cpp11",
"Version": "0.4.6",
"Version": "0.4.7",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"R"
],
"Hash": "707fae4bbf73697ec8d85f9d7076c061"
"Hash": "5a295d7d963cc5035284dcdbaf334f4e"
},
"curl": {
"Package": "curl",
Expand Down Expand Up @@ -498,7 +498,7 @@
},
"igraph": {
"Package": "igraph",
"Version": "1.5.1",
"Version": "2.0.3",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
Expand All @@ -514,9 +514,10 @@
"pkgconfig",
"rlang",
"stats",
"utils"
"utils",
"vctrs"
],
"Hash": "80401cb5ec513e8ddc56764d03f63669"
"Hash": "c3b7d801d722e26e4cd888e042bf9af5"
},
"isoband": {
"Package": "isoband",
Expand Down Expand Up @@ -944,6 +945,16 @@
],
"Hash": "906cb23d2f1c5680b8ce439b44c6fa63"
},
"secretbase": {
"Package": "secretbase",
"Version": "0.4.0",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"R"
],
"Hash": "e9591ffb6d5adcc9284213e496ed7fe9"
},
"selectr": {
"Package": "selectr",
"Version": "0.4-2",
Expand Down Expand Up @@ -1042,7 +1053,7 @@
},
"targets": {
"Package": "targets",
"Version": "1.3.0",
"Version": "1.6.0",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
Expand All @@ -1056,7 +1067,9 @@
"digest",
"igraph",
"knitr",
"ps",
"rlang",
"secretbase",
"stats",
"tibble",
"tidyselect",
Expand All @@ -1065,7 +1078,7 @@
"vctrs",
"yaml"
],
"Hash": "da40edff48c5c900c369d16564b51af1"
"Hash": "35b83348e5cf547715687c93ce5c2126"
},
"tibble": {
"Package": "tibble",
Expand Down

0 comments on commit 9198062

Please sign in to comment.