Skip to content

Commit

Permalink
Reduce instances when dplyr::lag() warning is shown
Browse files Browse the repository at this point in the history
The warning was shown whenever it detected dplyr is installed, even if
the user wasn't actively using dplyr. That caused an excessive amount
of noise when other packages attached xts (e.g. quantmod).

Thanks to Duncan Murdoch for the report and suggested fix!

Fixes #393.
  • Loading branch information
joshuaulrich committed Mar 1, 2023
1 parent 548f313 commit 61c2200
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 32 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: xts
Type: Package
Title: eXtensible Time Series
Version: 0.13.0.1
Version: 0.13.0.2
Authors@R: c(
person(given=c("Jeffrey","A."), family="Ryan", role=c("aut","cph")),
person(given=c("Joshua","M."), family="Ulrich", role=c("cre","aut"), email="josh.m.ulrich@gmail.com"),
Expand Down
46 changes: 15 additions & 31 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,42 +64,26 @@ function(pkg, generic, class, fun = NULL)
}

if (warn_dplyr_lag && dplyr_will_mask_lag) {

dplyr_path <- find.package("dplyr", quiet = TRUE, verbose = FALSE)

is_dplyr_installed <- (length(dplyr_path) > 0)
is_dplyr_attached <- "package:dplyr" %in% search()

if (is_dplyr_installed) {

msg <- "
################################### WARNING ###################################
# We noticed you have dplyr installed. The dplyr lag() function breaks how #
# base R's lag() function is supposed to work, which breaks lag(my_xts). #"

if (is_dplyr_attached) { msg <- paste0(msg, "
ugly_message <- "
######################### Warning from 'xts' package ##########################
# #
# Calls to lag(my_xts) that you enter or source() into this session won't #
# work correctly. #")
} else {
msg <- paste0(msg, "
# The dplyr lag() function breaks how base R's lag() function is supposed to #
# work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or #
# source() into this session won't work correctly. #
# #
# If you call library(dplyr) later in this session, then calls to lag(my_xts) #
# that you enter or source() into this session won't work correctly. #")
}

msg <- paste0(msg, "
# #
# All package code is unaffected because it is protected by the R namespace #
# mechanism. #
# Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
# conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop #
# dplyr from breaking base R's lag() function. #
# #
# Code in packages is not affected. It's protected by R's namespace mechanism #
# Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning. #
# #
# You can use stats::lag() to make sure you're not using dplyr::lag(), or you #
# can add conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop #
# dplyr from breaking base R's lag() function. #
################################### WARNING ###################################")
packageStartupMessage(msg)
###############################################################################"
if ("package:dplyr" %in% search()) {
packageStartupMessage(ugly_message)
} else {
setHook(packageEvent("dplyr", "attach"),
function(...) packageStartupMessage(ugly_message))
}
}
}
Expand Down

0 comments on commit 61c2200

Please sign in to comment.