Skip to content

Commit

Permalink
explicit argval for keep.data and keep.attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Dec 8, 2023
1 parent e88f6cf commit 3712038
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 37 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ichimoku 1.4.10.9000 (development)

* Arguments 'keep.data' and 'keep.attrs' across the package now have an explicit default value of FALSE (no resultant change in behaviour).

# ichimoku 1.4.10

* Improves handling of OANDA API errors.
Expand Down
28 changes: 14 additions & 14 deletions R/ichimoku.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
#' periods used for the cloud. This parameter shoud not normally be modified
#' as using other values would be invalid in the context of traditional
#' ichimoku analysis.
#' @param keep.data (optional) set to TRUE to retain additional data present
#' in the input object as additional columns and/or attributes.
#' @param keep.data [default FALSE] set to TRUE to retain additional data
#' present in the input object as additional columns and/or attributes.
#' @param ... additional arguments, for instance 'holidays', passed along to
#' \code{\link{tradingDays}} for calculating the future cloud on daily data.

Expand Down Expand Up @@ -110,18 +110,18 @@
#' @rdname ichimoku
#' @export
#'
ichimoku <- function(x, ticker, periods = c(9L, 26L, 52L), keep.data, ...) UseMethod("ichimoku")
ichimoku <- function(x, ticker, periods = c(9L, 26L, 52L), keep.data = FALSE, ...) UseMethod("ichimoku")

#' @rdname ichimoku
#' @method ichimoku ichimoku
#' @export
#'
ichimoku.ichimoku <- function(x, ticker, periods = c(9L, 26L, 52L), keep.data, ...) {
ichimoku.ichimoku <- function(x, ticker, periods = c(9L, 26L, 52L), keep.data = FALSE, ...) {

if (missing(ticker)) ticker <- attr(x, "ticker")
x <- x[!is.na(coredata.ichimoku(x)[, "close"]), ]

if (!missing(keep.data) && isTRUE(keep.data)) {
if (isTRUE(keep.data)) {
x$cd <- x$tenkan <- x$kijun <- x$senkouA <- x$senkouB <- x$chikou <- x$cloudT <- x$cloudB <- NULL
x <- as.data.frame.ichimoku(x, keep.attrs = TRUE)
} else {
Expand All @@ -136,10 +136,10 @@ ichimoku.ichimoku <- function(x, ticker, periods = c(9L, 26L, 52L), keep.data, .
#' @method ichimoku xts
#' @export
#'
ichimoku.xts <- function(x, ticker, periods = c(9L, 26L, 52L), keep.data, ...) {
ichimoku.xts <- function(x, ticker, periods = c(9L, 26L, 52L), keep.data = FALSE, ...) {

if (missing(ticker)) ticker <- deparse(substitute(x))
x <- xts_df(x, keep.attrs = !missing(keep.data) && isTRUE(keep.data))
x <- xts_df(x, keep.attrs = isTRUE(keep.data))

ichimoku.data.frame(x, ticker = ticker, periods = periods, keep.data = keep.data, ...)

Expand All @@ -149,7 +149,7 @@ ichimoku.xts <- function(x, ticker, periods = c(9L, 26L, 52L), keep.data, ...) {
#' @method ichimoku data.frame
#' @export
#'
ichimoku.data.frame <- function(x, ticker, periods = c(9L, 26L, 52L), keep.data, ...) {
ichimoku.data.frame <- function(x, ticker, periods = c(9L, 26L, 52L), keep.data = FALSE, ...) {

if (missing(ticker)) ticker <- deparse(substitute(x), width.cutoff = 500L, backtick = FALSE, control = NULL, nlines = 1L)
xlen <- dim(x)[1L]
Expand Down Expand Up @@ -238,15 +238,15 @@ ichimoku.data.frame <- function(x, ticker, periods = c(9L, 26L, 52L), keep.data,
}
xtsindex <- c(index, future)

if (missing(keep.data) || !isTRUE(keep.data)) {
x <- kmatrix <- NULL
} else {
if (isTRUE(keep.data)) {
used <- unlist(lapply(c("coli", "colo", "colh", "coll", "colc", "colp"),
function(x) get0(x, envir = parent.frame(2L), inherits = FALSE)))
used <- used[!is.na(used)]
keep <- if (!is.null(used)) cnames[-used]
kmatrix <- do.call(cbind, lapply(.subset(x, keep),
function(x) `length<-`(as.numeric(x), clen)))
} else {
x <- kmatrix <- NULL
}

kumo <- cbind(open = `length<-`(open, clen),
Expand All @@ -271,10 +271,10 @@ ichimoku.data.frame <- function(x, ticker, periods = c(9L, 26L, 52L), keep.data,
#' @method ichimoku matrix
#' @export
#'
ichimoku.matrix <- function(x, ticker, periods = c(9L, 26L, 52L), keep.data, ...) {
ichimoku.matrix <- function(x, ticker, periods = c(9L, 26L, 52L), keep.data = FALSE, ...) {

if (missing(ticker)) ticker <- deparse(substitute(x), width.cutoff = 500L, backtick = FALSE, control = NULL, nlines = 1L)
x <- matrix_df(x, keep.attrs = !missing(keep.data) && isTRUE(keep.data))
x <- matrix_df(x, keep.attrs = isTRUE(keep.data))

ichimoku.data.frame(x, ticker = ticker, periods = periods, keep.data = keep.data, ...)

Expand All @@ -284,7 +284,7 @@ ichimoku.matrix <- function(x, ticker, periods = c(9L, 26L, 52L), keep.data, ...
#' @method ichimoku default
#' @export
#'
ichimoku.default <- function(x, ticker, periods = c(9L, 26L, 52L), keep.data, ...) {
ichimoku.default <- function(x, ticker, periods = c(9L, 26L, 52L), keep.data = FALSE, ...) {

is.character(x) || stop("cannot create an ichimoku object from a '", class(x)[1L], "' object", call. = FALSE)
exists(x) || stop("object '", x, "' not found", call. = FALSE)
Expand Down
6 changes: 3 additions & 3 deletions R/methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ summary.ichimoku <- function(object, strat = TRUE, ...) {
#' @param x an object of class 'ichimoku'.
#' @param row.names not used.
#' @param optional not used.
#' @param keep.attrs (optional) if set to TRUE, will preserve any custom
#' @param keep.attrs [default FALSE] if set to TRUE, will preserve any custom
#' attributes set on the original object.
#' @param ... arguments passed to or from other methods.
#'
Expand All @@ -227,8 +227,8 @@ summary.ichimoku <- function(object, strat = TRUE, ...) {
#' @method as.data.frame ichimoku
#' @export
#'
as.data.frame.ichimoku <- function(x, row.names, optional, keep.attrs, ...)
.Call(ichimoku_tbl, x, !missing(keep.attrs) && isTRUE(keep.attrs))
as.data.frame.ichimoku <- function(x, row.names, optional, keep.attrs = FALSE, ...)
.Call(ichimoku_tbl, x, isTRUE(keep.attrs))

#' @name coredata
#' @rdname coredata.ichimoku
Expand Down
12 changes: 6 additions & 6 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ grid_dup <- function(n, omit.id) {
#' An optimised 'xts' to 'data.frame' constructor.
#'
#' @param x an 'xts' object.
#' @param keep.attrs (optional) if set to TRUE, will preserve any custom
#' @param keep.attrs [default FALSE] if set to TRUE, will preserve any custom
#' attributes set on the original object.
#'
#' @return A 'data.frame' object. The 'xts' index is preserved as the first
Expand All @@ -123,7 +123,7 @@ grid_dup <- function(n, omit.id) {
#'
#' @export
#'
xts_df <- function(x, keep.attrs) {
xts_df <- function(x, keep.attrs = FALSE) {
core <- coredata(x)
dn2 <- dimnames(core)[[2L]]
xlen <- dim(core)[1L]
Expand All @@ -139,7 +139,7 @@ xts_df <- function(x, keep.attrs) {
`attributes<-`(df, c(list(names = c("index", dn2),
class = "data.frame",
row.names = .set_row_names(xlen)),
if (!missing(keep.attrs) && isTRUE(keep.attrs))
if (isTRUE(keep.attrs))
.Call(ichimoku_look, x)))
}

Expand All @@ -148,7 +148,7 @@ xts_df <- function(x, keep.attrs) {
#' An optimised 'matrix' to 'data.frame' constructor.
#'
#' @param x a matrix.
#' @param keep.attrs (optional) if set to TRUE, will preserve any custom
#' @param keep.attrs [default FALSE] if set to TRUE, will preserve any custom
#' attributes set on the original object.
#'
#' @return A 'data.frame' object. If the matrix has row names, these are
Expand All @@ -167,8 +167,8 @@ xts_df <- function(x, keep.attrs) {
#'
#' @export
#'
matrix_df <- function(x, keep.attrs) {
lk <- if (!missing(keep.attrs) && isTRUE(keep.attrs)) .Call(ichimoku_look, x)
matrix_df <- function(x, keep.attrs = FALSE) {
lk <- if (isTRUE(keep.attrs)) .Call(ichimoku_look, x)
dn <- dimnames(x)
xlen <- dim(x)[1L]
len <- dim(x)[2L]
Expand Down
4 changes: 2 additions & 2 deletions man/as.data.frame.ichimoku.Rd

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

16 changes: 8 additions & 8 deletions man/ichimoku.Rd

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

4 changes: 2 additions & 2 deletions man/matrix_df.Rd

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

4 changes: 2 additions & 2 deletions man/xts_df.Rd

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

0 comments on commit 3712038

Please sign in to comment.