-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from panukatan/dev
update README
- Loading branch information
Showing
11 changed files
with
174 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,3 +50,4 @@ reference: | |
contents: | ||
- get_bagyo | ||
- print.bagyo | ||
- find_bagyo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) | ||
} | ||
) |