Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #498 Implement a print function for forecast objects #584

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ Imports:
Metrics,
rlang,
scoringRules,
stats
stats,
stringr
Suggests:
kableExtra,
knitr,
Expand Down
7 changes: 7 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Generated by roxygen2: do not edit by hand

S3method(as_forecast,default)
S3method(print,forecast_binary)
S3method(print,forecast_integer)
S3method(print,forecast_point)
S3method(print,forecast_quantile)
S3method(print,forecast_sample)
S3method(print,scoringutils_check)
S3method(quantile_to_interval,data.frame)
S3method(quantile_to_interval,numeric)
Expand Down Expand Up @@ -57,6 +62,7 @@ export(plot_quantile_coverage)
export(plot_ranges)
export(plot_score_table)
export(plot_wis)
export(print_forecast_info)
export(quantile_score)
export(quantile_to_interval)
export(rules_binary)
Expand Down Expand Up @@ -179,4 +185,5 @@ importFrom(stats,runif)
importFrom(stats,sd)
importFrom(stats,weighted.mean)
importFrom(stats,wilcox.test)
importFrom(stringr,str_pad)
importFrom(utils,combn)
81 changes: 81 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,84 @@
}
return(data)
}

#' @title Print information about a `forecast` object.
#' @description This function prints information about a forecast object,
#' including "Forecast type", "Protected columns", "Metric columns",
#' "Forecast unit".
#' @importFrom stringr str_pad
#' @export
#' @keywords print-forecasts
#' @examples
#' print(example_quantile %>% as_forecast)
print_forecast_info <- function(data) {
# Wrap text and create four spaces in the beginning.
width <- 40
wrap_text <- function(str) {
str <- strwrap(str, width = width - 4, simplify = FALSE)
str <- sapply(str, function(line) paste(" ", line, sep = "")) %>%

Check warning on line 218 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/utils.R,line=218,col=39,[paste_linter] paste0(...) is better than paste(..., sep = "").

Check warning on line 218 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/utils.R,line=218,col=68,[object_usage_linter] no visible global function definition for '%>%'
paste(collapse = "\n ")
return(str)

Check warning on line 220 in R/utils.R

View check run for this annotation

Codecov / codecov/patch

R/utils.R#L215-L220

Added lines #L215 - L220 were not covered by tests
}

eq_str <- rep("=", width) %>% paste(collapse = "") %>% paste("\n")

Check warning on line 223 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/utils.R,line=223,col=30,[object_usage_linter] no visible global function definition for '%>%'

Check warning on line 223 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/utils.R,line=223,col=30,[object_usage_linter] no visible global function definition for '%>%'
s_ <- str_pad("Forecast data information", width = width, side = "both") %>%

Check warning on line 224 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/utils.R,line=224,col=76,[object_usage_linter] no visible global function definition for '%>%'
paste("\n")
s_ <- paste("", eq_str, s_, eq_str)

Check warning on line 226 in R/utils.R

View check run for this annotation

Codecov / codecov/patch

R/utils.R#L223-L226

Added lines #L223 - L226 were not covered by tests

type <- get_forecast_type(data)
col <- "Forecast type:"
s_ <- paste(col, str_pad(type, width = width - nchar(col) - 1), "\n\n") %>%

Check warning on line 230 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/utils.R,line=230,col=75,[object_usage_linter] no visible global function definition for '%>%'
paste(s_, .)

Check warning on line 231 in R/utils.R

View check run for this annotation

Codecov / codecov/patch

R/utils.R#L228-L231

Added lines #L228 - L231 were not covered by tests

# Exclude protected columns.
protect_cols <- get_protected_columns(data)
if (is.null(attr(data, "metric_names"))) {
met_cols <- c()

Check warning on line 236 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/utils.R,line=236,col=17,[unnecessary_concatenation_linter] Unneeded concatenation without arguments. Replace the "c" call by NULL or, whenever possible, vector() seeded with the correct type and/or length.
mets <- "NA"

Check warning on line 237 in R/utils.R

View check run for this annotation

Codecov / codecov/patch

R/utils.R#L234-L237

Added lines #L234 - L237 were not covered by tests
} else {
met_cols <- get_metrics(data)
mets <- met_cols %>% paste(collapse = ", ")

Check warning on line 240 in R/utils.R

View check run for this annotation

Codecov / codecov/patch

R/utils.R#L239-L240

Added lines #L239 - L240 were not covered by tests

Check warning on line 240 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/utils.R,line=240,col=22,[object_usage_linter] no visible global function definition for '%>%'
}

# Protected columns
protect_pri <- protect_cols[!(protect_cols %in% met_cols)] %>%

Check warning on line 244 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/utils.R,line=244,col=62,[object_usage_linter] no visible global function definition for '%>%'
paste(collapse = ", ")
s_ <- paste0("Protected columns:\n ", wrap_text(protect_pri), "\n\n") %>%

Check warning on line 246 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/utils.R,line=246,col=76,[object_usage_linter] no visible global function definition for '%>%'
paste(s_, .)

Check warning on line 247 in R/utils.R

View check run for this annotation

Codecov / codecov/patch

R/utils.R#L244-L247

Added lines #L244 - L247 were not covered by tests

# Print metrics columns if exist.
if (length(met_cols) != 0) {
s_ <- paste0("Metric columns (protected):\n ", wrap_text(mets), "\n\n") %>%
paste(s_, .)

Check warning on line 252 in R/utils.R

View check run for this annotation

Codecov / codecov/patch

R/utils.R#L250-L252

Added lines #L250 - L252 were not covered by tests
}

# Forecast unit
unit <- get_forecast_unit(data) %>% paste(collapse = ", ")
s_ <- paste0("Forecast unit:\n ", wrap_text(unit), "\n") %>%
paste(s_, .)
s_tab1 <- str_pad("data.table print", width = width, side = "both")
s_ <- paste(s_, "\n", eq_str, s_tab1, "\n", eq_str)
cat(s_)
print(data.table(data))

Check warning on line 262 in R/utils.R

View check run for this annotation

Codecov / codecov/patch

R/utils.R#L256-L262

Added lines #L256 - L262 were not covered by tests
}

#' @title Print information about a `forecast_binary` object.
#' @export
print.forecast_binary <- function(data) print_forecast_info(data)

Check warning on line 267 in R/utils.R

View check run for this annotation

Codecov / codecov/patch

R/utils.R#L267

Added line #L267 was not covered by tests

#' @title Print information about a `forecast_quantile` object.
#' @export
print.forecast_quantile <- function(data) print_forecast_info(data)

Check warning on line 271 in R/utils.R

View check run for this annotation

Codecov / codecov/patch

R/utils.R#L271

Added line #L271 was not covered by tests

#' @title Print information about a `forecast_point` object.
#' @export
print.forecast_point <- function(data) print_forecast_info(data)

Check warning on line 275 in R/utils.R

View check run for this annotation

Codecov / codecov/patch

R/utils.R#L275

Added line #L275 was not covered by tests

#' @title Print information about a `forecast_sample` object.
#' @export
print.forecast_sample <- function(data) print_forecast_info(data)

Check warning on line 279 in R/utils.R

View check run for this annotation

Codecov / codecov/patch

R/utils.R#L279

Added line #L279 was not covered by tests

#' @title Print information about a `forecast_integer` object.
#' @export
print.forecast_integer <- function(data) print_forecast_info(data)

Check warning on line 283 in R/utils.R

View check run for this annotation

Codecov / codecov/patch

R/utils.R#L283

Added line #L283 was not covered by tests
11 changes: 11 additions & 0 deletions man/print.forecast_binary.Rd

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

11 changes: 11 additions & 0 deletions man/print.forecast_integer.Rd

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

11 changes: 11 additions & 0 deletions man/print.forecast_point.Rd

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

11 changes: 11 additions & 0 deletions man/print.forecast_quantile.Rd

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

11 changes: 11 additions & 0 deletions man/print.forecast_sample.Rd

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

17 changes: 17 additions & 0 deletions man/print_forecast_info.Rd

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

Loading