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

Don't load lubridate on startup + other upkeep #237

Merged
merged 9 commits into from
Oct 3, 2023
Merged
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
12 changes: 7 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ Encoding: UTF-8
LazyData: true
Depends:
R (>= 3.5.0),
lubridate,
PerformanceAnalytics,
quantmod (>= 0.4-13)
Imports:
dplyr (>= 1.0.0),
ggplot2 (>= 3.4.0),
jsonlite,
httr,
curl,
lazyeval,
lubridate,
magrittr,
PerformanceAnalytics,
quantmod (>= 0.4-13),
purrr,
readr,
readxl,
Expand All @@ -41,11 +41,13 @@ Imports:
timeDate,
TTR,
xts,
rlang
rlang,
zoo,
cli
Suggests:
alphavantager (>= 0.1.2),
Quandl,
riingo,
riingo,
tibbletime,
broom,
knitr,
Expand Down
14 changes: 13 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

S3method(print,tidyquant_conflicts)
S3method(tq_mutate_,data.frame)
S3method(tq_mutate_,default)
S3method(tq_mutate_,tbl_df)
Expand Down Expand Up @@ -157,6 +158,7 @@ export(scale_fill_tq)
export(theme_tq)
export(theme_tq_dark)
export(theme_tq_green)
export(tidyquant_conflicts)
export(tiingo_api_key)
export(tq_exchange)
export(tq_exchange_options)
Expand All @@ -182,15 +184,25 @@ export(tq_transmute_)
export(tq_transmute_fun_options)
export(tq_transmute_xy)
export(tq_transmute_xy_)
import(PerformanceAnalytics)
import(TTR)
import(lubridate)
import(quantmod)
import(xts)
import(zoo)
importFrom(TTR,BBands)
importFrom(TTR,MACD)
importFrom(TTR,SMA)
importFrom(TTR,runCor)
importFrom(TTR,runSD)
importFrom(ggplot2,`%+replace%`)
importFrom(magrittr,"%$%")
importFrom(magrittr,"%>%")
importFrom(rlang,":=")
importFrom(rlang,.data)
importFrom(utils,download.file)
importFrom(utils,read.csv)
importFrom(xts,lag.xts)
importFrom(xts,to.monthly)
importFrom(xts,to.period)
importFrom(xts,xts)
importFrom(zoo,rollapply)
34 changes: 23 additions & 11 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
# tidyquant (development version)

- Remove the dependency on tidyverse
- tidyquant no longer loads lubridate as tidyverse 2.0 now loads lubridate
## Breaking changes

- tidyquant no longer loads lubridate. (#237, @olivroy)

If you use tidyquant with tidyverse, there is no change for you.

tidyquant no longer loads many packages.

## Fixes

- tidyquant startup messages mimics the tidyverse messages for clarity. (#163, #116)
- Remove the dependency on tidyverse (#236, @olivroy)
- tidyquant no longer loads lubridate as tidyverse 2.0 now loads lubridate.
- Changed the `size` argument to `linewidth` for ggplot2 3.4.0
- Removed the last tidyr and dplyr deprecated functions
- Add linewidth = to `geom_ma()`
- Move Quandl, riingo, and alphavantager to Suggests. tidyquant will not explictly install those, but you can install them from CRAN.
- Fixed CRAN alias
- Removed the last tidyr and dplyr deprecated functions.
- Add `linewidth` to `geom_ma()`
- Move `Quandl`, `riingo`, and `alphavantager` to Suggests. tidyquant will not explicitly install those, but you can install them from CRAN.
- Fixed CRAN package alias
- FB to META change in `FANG`

# tidyquant 1.0.7
Expand Down Expand Up @@ -96,7 +107,7 @@ Other changes:
- Excel Date Math functions: `NET_WORKDAYS()`, `EOMONTH()`
- __Financial Math Functions__ - `NPV()`, `IRR()`, `FV()`, `PV()`, `PMT()`, `RATE()`

* __NEW Tidyverse Functionality__
* __NEW tidyverse Functionality__
- `summarise_by_time()` - This is a new time-based variant of `summarise()` that allows collapsing the time-series by "day", "week", "month", "quarter", "year", and more.
- Note: I will evaluate the need for `summarise_at_by_time()`, `summarise_all_by_time()`, and `summarise_if_by_time()` after the release of `dplyr` v1.0.0.

Expand All @@ -119,7 +130,7 @@ Other changes:

# tidyquant 0.5.10

* `tq_get()` - Temporarily adjust tests for `tq_get(get = "dividends")` and `tq_get(get = "splits")` until API is stabilizes. Yahoo! Dividends and Splits intermitently returns errors.
* `tq_get()` - Temporarily adjust tests for `tq_get(get = "dividends")` and `tq_get(get = "splits")` until API is stabilizes. Yahoo! Dividends and Splits intermittently returns errors.
* Fix documentation warnings during package build checks. Documentation moved from `tq_stocklist` to `?tq_index`.

# tidyquant 0.5.9
Expand All @@ -144,9 +155,9 @@ _Visualizations & Color Palettes_
* `geom_candlestick` and `geom_barchart` - Issue #112.
* Added color names of `theme_tq` palettes (`palette_light`, `palette_dark`, and `palette_green`) for easier identification.

_Compatability with `tidyr` v1.0.0_
_Compatibility with `tidyr` v1.0.0_

* Improvements to ensure compatability with `tidyr` v1.0.0
* Improvements to ensure compatibility with `tidyr` v1.0.0

_[Potential Breaking Change] Move `tidyverse` to suggests_

Expand Down Expand Up @@ -289,9 +300,10 @@ _[Potential Breaking Change] Move `tidyverse` to suggests_
* Changed `tq_mutate()`, `tq_transform()`, `tq_mutate_xy()` and `tq_transform_xy()` arguments to be more obvious:
* `x_fun` is now `ohlc_fun` for `tq_mutate()` and `tq_transform()`
* `.x` is now `x` and `.y` is now `y` for `tq_mutate_xy()` and `tq_transform_xy()`
* Fixed duplication of column names during `tq_mutate`. Names are now sequentually indexed with duplicate names starting at `.1` suffix.
* Fixed duplication of column names during `tq_mutate`. Names are now sequentially indexed with duplicate names starting at `.1` suffix.


# tidyquant 0.1.0


* Initial release of `tidyquant`, for seamless quantitative financial analysis (`xts`, `quantmod`, `TTR`) package integration with the `tidyverse`.
2 changes: 1 addition & 1 deletion R/api-tiingo.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Set Tiingo API Key
#'
#' Requires the riingo package to be installled.
#' Requires the riingo package to be installed.
#' @param api_key Optionally passed parameter to set Tiingo `api_key`.
#'
#' @return Returns invisibly the currently set `api_key`
Expand Down
210 changes: 210 additions & 0 deletions R/attach.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
# Taken from tidyverse
core <- c("xts", "quantmod", "TTR", "PerformanceAnalytics")

core_unloaded <- function() {
search <- paste0("package:", core)
core[!search %in% search()]
}

# Attach the package from the same package library it was
# loaded from before. https://github.com/tidyverse/tidyverse/issues/171
same_library <- function(pkg) {
loc <- if (pkg %in% loadedNamespaces()) dirname(getNamespaceInfo(pkg, "path"))
library(pkg, lib.loc = loc, character.only = TRUE, warn.conflicts = FALSE)
}

tidyquant_attach <- function() {
to_load <- core_unloaded()

suppressPackageStartupMessages(
lapply(to_load, same_library)
)

invisible(to_load)
}

tidyquant_attach_message <- function(to_load) {
if (length(to_load) == 0) {
return(NULL)
}

header <- cli::rule(
left = cli::style_bold("Attaching core tidyquant packages"),
right = paste0("tidyquant ", package_version_h("tidyquant"))
)

to_load <- sort(to_load)
versions <- vapply(to_load, package_version_h, character(1))

packages <- paste0(
cli::col_green(cli::symbol$tick), " ", cli::col_blue(format(to_load)), " ",
cli::ansi_align(versions, max(cli::ansi_nchar(versions)))
)

if (length(packages) %% 2 == 1) {
packages <- append(packages, "")
}
col1 <- seq_len(length(packages) / 2)
info <- paste0(packages[col1], " ", packages[-col1])

paste0(header, "\n", paste(info, collapse = "\n"))
}

package_version_h <- function(pkg) {
highlight_version(utils::packageVersion(pkg))
}

highlight_version <- function(x) {
x <- as.character(x)

is_dev <- function(x) {
x <- suppressWarnings(as.numeric(x))
!is.na(x) & x >= 9000
}

pieces <- strsplit(x, ".", fixed = TRUE)
pieces <- lapply(pieces, function(x) ifelse(is_dev(x), cli::col_red(x), x))
vapply(pieces, paste, collapse = ".", FUN.VALUE = character(1))
}

#' Conflicts between the tidyquant and other packages
#'
#' This function lists all the conflicts between packages in the tidyverse
#' and other packages that you have loaded.
#'
#' There are four conflicts that are deliberately ignored: \code{intersect},
#' \code{union}, \code{setequal}, and \code{setdiff} from dplyr. These functions
#' make the base equivalents generic, so shouldn't negatively affect any
#' existing code.
#'
#' @export
#' @param only Set this to a character vector to restrict to conflicts only
#' with these packages.
#' @examples
#' tidyquant_conflicts()
tidyquant_conflicts <- function(only = NULL) {
envs <- grep("^package:", search(), value = TRUE)
envs <- purrr::set_names(envs)

if (!is.null(only)) {
only <- union(only, core)
envs <- envs[names(envs) %in% paste0("package:", only)]
}

objs <- invert(lapply(envs, ls_env))

conflicts <- purrr::keep(objs, ~ length(.x) > 1)

tidy_names <- paste0("package:", tidyquant_packages())
conflicts <- purrr::keep(conflicts, ~ any(.x %in% tidy_names))

conflict_funs <- purrr::imap(conflicts, confirm_conflict)
conflict_funs <- purrr::compact(conflict_funs)

structure(conflict_funs, class = "tidyquant_conflicts")
}

tidyquant_conflict_message <- function(x) {
header <- cli::rule(
left = cli::style_bold("Conflicts"),
right = "tidyquant_conflicts()"
)

pkgs <- x %>% purrr::map(~ gsub("^package:", "", .))
others <- pkgs %>% purrr::map(`[`, -1)
other_calls <- purrr::map2_chr(
others, names(others),
~ paste0(cli::col_blue(.x), "::", .y, "()", collapse = ", ")
)

winner <- pkgs %>% purrr::map_chr(1)
funs <- format(paste0(cli::col_blue(winner), "::", cli::col_green(paste0(names(x), "()"))))
bullets <- paste0(
cli::col_red(cli::symbol$cross), " ", funs, " masks ", other_calls,
collapse = "\n"
)

conflicted <- paste0(
cli::col_cyan(cli::symbol$info), " ",
cli::format_inline("Use the {.href [conflicted package](http://conflicted.r-lib.org/)} to force all conflicts to become errors"
))

paste0(
header, "\n",
bullets, "\n",
conflicted
)
}

#' @export
print.tidyquant_conflicts <- function(x, ..., startup = FALSE) {
cli::cat_line(tidyquant_conflict_message(x))
invisible(x)
}

#' @importFrom magrittr %>%
confirm_conflict <- function(packages, name) {
# Only look at functions
objs <- packages %>%
purrr::map(~ get(name, pos = .)) %>%
purrr::keep(is.function)

if (length(objs) <= 1)
return()

# Remove identical functions
objs <- objs[!duplicated(objs)]
packages <- packages[!duplicated(packages)]
if (length(objs) == 1)
return()

packages
}

ls_env <- function(env) {
x <- ls(pos = env)

# intersect, setdiff, setequal, union come from generics
if (env %in% c("package:dplyr", "package:lubridate")) {
x <- setdiff(x, c("intersect", "setdiff", "setequal", "union"))
}

if (env == "package:lubridate") {
x <- setdiff(x, c(
"as.difftime", # lubridate makes into an S4 generic
"date" # matches base behavior
))
}

x
}

inform_startup <- function(msg, ...) {
if (is.null(msg)) {
return()
}
if (isTRUE(getOption("tidyquant.quiet"))) {
return()
}

rlang::inform(msg, ..., class = "packageStartupMessage")
}
tidyquant_packages <- function(include_self = TRUE) {
raw <- utils::packageDescription("tidyquant")$Imports
imports <- strsplit(raw, ",")[[1]]
parsed <- gsub("^\\s+|\\s+$", "", imports)
names <- vapply(strsplit(parsed, "\\s+"), "[[", 1, FUN.VALUE = character(1))

if (include_self) {
names <- c(names, "tidyquant")
}

names
}

invert <- function(x) {
if (length(x) == 0) return()
stacked <- utils::stack(x)
tapply(as.character(stacked$ind), stacked$values, list)
}

2 changes: 1 addition & 1 deletion R/excel-date-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#' (as ordered factors) or numeric values.
#' @param abbr A logical used for [MONTH()] and [WEEKDAY()]. If `label = TRUE`, used to determine if
#' full names (e.g. Wednesday) or abbreviated names (e.g. Wed) should be returned.
#' @param include_year A logicial value used in [QUARTER()]. Determines whether or not to return 2020 Q3 as `3` or `2020.3`.
#' @param include_year A logical value used in [QUARTER()]. Determines whether or not to return 2020 Q3 as `3` or `2020.3`.
#' @param fiscal_start A numeric value used in [QUARTER()]. Determines the fiscal-year starting quarter.
#' @param by Used to determine the gap in Date Sequence calculations and value to round to in Date Collapsing operations.
#' Acceptable values are: A character string, containing one of `"day"`, `"week"`, `"month"`, `"quarter"` or `"year"`.
Expand Down
2 changes: 1 addition & 1 deletion R/excel-financial-math-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#' @param cashflow Cash flow values. When one value is provided, it's assumed constant cash flow.
#' @param rate One or more rate. When one rate is provided it's assumed constant rate.
#' @param nper Number of periods. When `nper`` is provided, the cashflow values and rate are assumed constant.
#' @param pv Present value. Initial investments (cash inflows) are typcially a negative value.
#' @param pv Present value. Initial investments (cash inflows) are typically a negative value.
#' @param fv Future value. Cash outflows are typically a positive value.
#' @param pmt Number of payments per period.
#' @param type Should payments (`pmt`) occur at the beginning (`type = 0`) or
Expand Down
2 changes: 1 addition & 1 deletion R/ggplot-geom_bbands.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
#' @examples
#' library(dplyr)
#' library(ggplot2)
#'
#' library(lubridate)
#'
#' AAPL <- tq_get("AAPL", from = "2013-01-01", to = "2016-12-31")
#'
Expand Down
Loading