Skip to content

Commit

Permalink
Merge pull request #31 from panukatan/dev
Browse files Browse the repository at this point in the history
update README
  • Loading branch information
ernestguevarra authored Apr 14, 2024
2 parents b9664d2 + 8e29439 commit 5175d9d
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 70 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Generated by roxygen2: do not edit by hand

S3method(print,bagyo)
export(find_bagyo)
export(get_bagyo)
80 changes: 25 additions & 55 deletions R/find_cyclone.R
Original file line number Diff line number Diff line change
@@ -1,68 +1,38 @@
#'
#' Get a cyclone name
#' Find cyclones information
#'
#' Randomly returns a cyclone name and prints additional information available
#' from the package regarding the cyclone.
#' @param .year An integer value for a year or a vector of years of cyclones
#' data to retrieve. Default to NULL to retrieve all years.
#' @param .category A character value or a vector of category code/s or category
#' name/s to retrieve. Default to NULL to retrieve all categories
#'
#' @returns A list of information regarding a randomly selected cyclone from
#' the `bagyo` dataset.
#' @returns A data.frame of cyclones information based on specified `year` and
#' `category`
#'
#' @examples
#' get_bagyo()
#' find_bagyo()
#' find_bagyo(.year = 2017)
#' find_bagyo(.category = "TD")
#' find_bagyo(.year = 2017, .category = "TD")
#'
#' @rdname get_bagyo
#' @export
#'

get_bagyo <- function() {
find_bagyo <- function(.year = NULL, .category = NULL) {
df <- bagyo::bagyo

df <- df[sample(seq_len(nrow(df)), size = 1), ] |>
as.list()
if (is.null(.year)) {
if (is.null(.category))
result <- df
else
result <- df[df$category_name %in% .category | df$category_code %in% .category, ]
} else {
if (is.null(.category))
result <- df[df$year %in% .year, ]
else
result <- df[df$year %in% .year & (df$category_name %in% .category | df$category_code %in% .category), ]
}

attr(df, "class") <- "bagyo"

df
}


#'
#' [print()] helper function for [get_bagyo()] function
#'
#' @param x Object of class `bagyo` produced by [get_bagyo()] function
#' @param ... Additional [print()] arguments
#'
#' @returns Printed output of [get_bagyo()] function
#'
#' @export
#'

print.bagyo <- function(x, ...) {
cat("\n\t*** Featured Bagyo ***\n\n", sep = "")
cat(
"Name:", format(x$name, width = 20, justify = "left"),
"RSMC Name:", format(x$rsmc_name, width = 0, justify = "left"),
"\n"
)
cat(
"Year:", format(as.character(x$year), width = 20, justify = "left"),
"Category:", format(
paste0(x$category_name, " (", x$category_code, ")"),
width = 0, justify = "left"),
"\n"
)
cat(
"Start:", format(x$start, format = "%Y-%m-%d %H:%M:%S", justify = "left"),
"End:", format(x$end, format = "%Y-%m-%d %H:%M:%S", justify = "left"),
"\n"
)
cat(
"Pressure:",
format(as.character(x$pressure), justify = "left", width = 1),
"hPa",
" ",
"Speed:", format(as.character(x$speed), justify = "left", width = 1),
"km/h",
"\n"
)
## Return result ----
result
}
68 changes: 68 additions & 0 deletions R/get_cyclone.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#'
#' Get a cyclone name
#'
#' Randomly returns a cyclone name and prints additional information available
#' from the package regarding the cyclone.
#'
#' @returns A list of information regarding a randomly selected cyclone from
#' the `bagyo` dataset.
#'
#' @examples
#' get_bagyo()
#'
#' @rdname get_bagyo
#' @export
#'

get_bagyo <- function() {
df <- bagyo::bagyo

df <- df[sample(seq_len(nrow(df)), size = 1), ] |>
as.list()

attr(df, "class") <- "bagyo"

df
}


#'
#' [print()] helper function for [get_bagyo()] function
#'
#' @param x Object of class `bagyo` produced by [get_bagyo()] function
#' @param ... Additional [print()] arguments
#'
#' @returns Printed output of [get_bagyo()] function
#'
#' @export
#'

print.bagyo <- function(x, ...) {
cat("\n\t*** Featured Bagyo ***\n\n", sep = "")
cat(
"Name:", format(x$name, width = 20, justify = "left"),
"RSMC Name:", format(x$rsmc_name, width = 0, justify = "left"),
"\n"
)
cat(
"Year:", format(as.character(x$year), width = 20, justify = "left"),
"Category:", format(
paste0(x$category_name, " (", x$category_code, ")"),
width = 0, justify = "left"),
"\n"
)
cat(
"Start:", format(x$start, format = "%Y-%m-%d %H:%M:%S", justify = "left"),
"End:", format(x$end, format = "%Y-%m-%d %H:%M:%S", justify = "left"),
"\n"
)
cat(
"Pressure:",
format(as.character(x$pressure), justify = "left", width = 1),
"hPa",
" ",
"Speed:", format(as.character(x$speed), justify = "left", width = 1),
"km/h",
"\n"
)
}
4 changes: 2 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Oceans and seas significantly impact continental weather, with evaporation from

The Philippines frequently experiences tropical cyclones (called ***bagyo*** - pronounced /baɡˈjo/, [bɐɡˈjo] - in the Filipino language) because of its geographical position. These cyclones typically bring heavy rainfall, leading to widespread flooding, as well as strong winds that cause significant damage to human life, crops, and property. Data on cyclones are collected and curated by the [Philippine Atmospheric, Geophysical, and Astronomical Services Administration (PAGASA)](https://www.pagasa.dost.gov.ph/).

This package contains Philippine tropical cyclone data from 2017 to 2020 in a machine-readable format. It is hoped that this data package provides an interesting and unique dataset for data exploration and visualisation as an adjunct to the traditional [`iris`](https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/iris.html) dataset and to the current [`palmerpenguins`](https://allisonhorst.github.io/palmerpenguins/) dataset.
This package contains Philippine tropical cyclones data from 2017 to 2020 in a machine-readable format. It is hoped that this data package provides an interesting and unique dataset for data exploration and visualisation as an adjunct to the traditional [`iris`](https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/iris.html) dataset and to the current [`palmerpenguins`](https://allisonhorst.github.io/palmerpenguins/) dataset.

## Installation

Expand Down Expand Up @@ -84,7 +84,7 @@ bagyo |>
summarise(mean_duration = mean(duration))
```

### `bagyo` are great to visualise
### `bagyo` is great to visualise

```{r barplot, echo = FALSE, fig.align = "center", fig.height = 4}
library(ggplot2)
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ Data on cyclones are collected and curated by the [Philippine
Atmospheric, Geophysical, and Astronomical Services Administration
(PAGASA)](https://www.pagasa.dost.gov.ph/).

This package contains Philippine tropical cyclone data from 2017 to 2020
in a machine-readable format. It is hoped that this data package
This package contains Philippine tropical cyclones data from 2017 to
2020 in a machine-readable format. It is hoped that this data package
provides an interesting and unique dataset for data exploration and
visualisation as an adjunct to the traditional
[`iris`](https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/iris.html)
Expand Down Expand Up @@ -126,7 +126,7 @@ bagyo |>
#> 5 Super Typhoon 77.50000 hours
```

### `bagyo` are great to visualise
### `bagyo` is great to visualise

<img src="man/figures/README-barplot-1.png" style="display: block; margin: auto;" />

Expand Down
29 changes: 29 additions & 0 deletions man/find_bagyo.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/get_bagyo.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/print.bagyo.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkgdown/_pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ reference:
contents:
- get_bagyo
- print.bagyo
- find_bagyo
33 changes: 25 additions & 8 deletions tests/testthat/test-find_cyclone.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
# Tests for get_bagyo() function -----------------------------------------------
# Tests for find_bagyo() function ----------------------------------------------

test_that(
"output is class tibble",
"output is a tibble",
{
expect_s3_class(
get_bagyo(),
"bagyo"
)
expect_s3_class(find_bagyo(), "tbl")
expect_s3_class(find_bagyo(), "data.frame")
}
)

test_that(
"there is an output",
"output is a tibble",
{
expect_output(print(get_bagyo()))
expect_s3_class(find_bagyo(.year = 2017), "tbl")
expect_s3_class(find_bagyo(.year = 2017), "data.frame")
expect_s3_class(find_bagyo(.year = 2016), "tbl")
expect_s3_class(find_bagyo(.year = 2016), "data.frame")
expect_s3_class(find_bagyo(.category = "TD"), "tbl")
expect_s3_class(find_bagyo(.category = "TD"), "data.frame")
expect_s3_class(find_bagyo(.year = 2017, .category = "TD"), "tbl")
expect_s3_class(find_bagyo(.year = 2017, .category = "TD"), "data.frame")
}
)

test_that(
"output has the expected number of rows",
{
expect_equal(nrow(find_bagyo()), nrow(bagyo))
expect_equal(nrow(find_bagyo(.year = 2017)), 22)
expect_equal(nrow(find_bagyo(.year = 2016)), 0)
}
)



18 changes: 18 additions & 0 deletions tests/testthat/test-get_cyclone.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Tests for get_bagyo() function -----------------------------------------------

test_that(
"output is class tibble",
{
expect_s3_class(
get_bagyo(),
"bagyo"
)
}
)

test_that(
"there is an output",
{
expect_output(print(get_bagyo()))
}
)

0 comments on commit 5175d9d

Please sign in to comment.