Skip to content

Commit

Permalink
Safe way to eval expression
Browse files Browse the repository at this point in the history
  • Loading branch information
xqnwang committed Nov 15, 2024
1 parent 880aa99 commit b16d0ad
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 30 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export(mcp)
export(pid)
export(point_measures)
export(scp)
export(update)
export(width)
export(winkler_score)
import(forecast)
Expand Down
2 changes: 1 addition & 1 deletion R/acp.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#' types 4 to 9 are for continuous quantiles. See the
#' \code{\link[ggdist]{weighted_quantile}} function in the ggdist package.
#' @param update If \code{TRUE}, the function will be compatible with the
#' \code{\link[base]{update}} function, allowing for easy updates of conformal prediction.
#' \code{update}(\link{update.cpforecast}) function, allowing for easy updates of conformal prediction.
#' @param na.rm If \code{TRUE}, corresponding entries in sample values are removed
#' if it is \code{NA} when calculating sample quantile.
#' @param ... Other arguments are passed to the
Expand Down
3 changes: 2 additions & 1 deletion R/conformal.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#' point forecasts and \code{ERROR} for forecast errors on validation set.
#' See the results of a call to \code{\link{cvforecast}}.
#' @param method A character string specifying the conformal method to be applied.
#' Possible options include \code{"scp"} (\link{scp}), \code{"acp"} (\link{acp}), \code{"pid"} (\link{pid}), and \code{"mcp"} (\link{mcp}).
#' Possible options include \code{"scp"} (\link{scp}), \code{"acp"}(\link{acp}),
#' \code{"pid"}(\link{pid}), and \code{"mcp"}(\link{mcp}).
#' @param ... Additional arguments to be passed to the selected conformal method.
#'
#' @examples
Expand Down
2 changes: 1 addition & 1 deletion R/scp.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
#' @param kess If \code{TRUE}, Kish's effective sample size is used for sample
#' quantile computation.
#' @param update If \code{TRUE}, the function will be compatible with the
#' \code{\link[base]{update}} function, allowing for easy updates of conformal prediction.
#' \code{update}(\link{update.cpforecast}) function, allowing for easy updates of conformal prediction.
#' @param na.rm If \code{TRUE}, corresponding entries in sample values and weights
#' are removed if either is \code{NA} when calculating sample quantile.
#' @param ... Other arguments are passed to \code{weightfun}.
Expand Down
19 changes: 2 additions & 17 deletions R/update.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,7 @@
#' summary(scpfc_update)
#'
#' @export
update <- function(object, ...) {
UseMethod("update")
}

#' @rdname update
#' @export
update.cpforecast <- function(object, new_data, forecastfun, new_xreg = NULL, ...) {
# browser()
level <- object$level
alpha <- 1 - level / 100
h <- dim(object$MEAN)[2]
Expand All @@ -70,16 +63,8 @@ update.cpforecast <- function(object, new_data, forecastfun, new_xreg = NULL, ..

# Info required for model fitting
cvcall <- object$model$cvforecast$call
if ("initial" %in% names(cvcall)) {
initial <- eval.parent(parse(text = as.character(cvcall)[names(cvcall) == "initial"]))
} else {
initial <- 1
}
if ("window" %in% names(cvcall)) {
window <- eval.parent(parse(text = as.character(cvcall)[names(cvcall) == "window"]))
} else {
window <- NULL
}
initial <- eval(get_call_arg_with_defaults(cvforecast, cvcall, "initial"), .GlobalEnv)
window <- eval(get_call_arg_with_defaults(cvforecast, cvcall, "window"), .GlobalEnv)

# Model fitting and forecasting
nfirst <- ifelse(forward, length(object$x) + 1L, length(object$x))
Expand Down
8 changes: 8 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
get_call_arg_with_defaults <- function(.f, call, arg) {
defaults <- formals(.f)
if(is.null(call[[arg]])) {
defaults[[arg]]
} else {
call[[arg]]
}
}
2 changes: 1 addition & 1 deletion man/acp.Rd

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

3 changes: 2 additions & 1 deletion man/conformal.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/scp.Rd

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

9 changes: 3 additions & 6 deletions man/update.Rd → man/update.cpforecast.Rd

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

0 comments on commit b16d0ad

Please sign in to comment.