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

Question - Using as.period within a package #664

Closed
rsangole opened this Issue Apr 10, 2018 · 5 comments

Comments

Projects
None yet
2 participants
@rsangole

rsangole commented Apr 10, 2018

Hello,

Situation

I don't wish to write require(lubridate) in my package. However, not doing so results in an error.

Error & Reproducible Example

  • Doesn't work:

In a brand new R session.

lubridate::as.period(x = 10, unit = 'day')
#> Error in get(as.character(FUN), mode = "function", envir = envir): object 'days' of mode 'function' was not found
  • Works:

In a brand new R session.

library('lubridate')
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date
lubridate::as.period(x = 10, unit = 'day')
#> [1] "10d 0H 0M 0S"

Why is this behavior true?

Background

I'm writing a package which uses the lubridate::as.period function and would like to make a call like:

historic_timeperiod <- lubridate::as.period(30, units='days')

(In the package, lubridate is set as Imports:)

Version Information

R version 3.3.3 (2017-03-06)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.4 LTS

locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] RevoUtilsMath_10.0.0 RevoUtils_10.0.3 RevoMods_11.0.0 MicrosoftML_1.3.0 RevoScaleR_9.1.1
[6] lattice_0.20-34 rpart_4.1-10

loaded via a namespace (and not attached):
[1] codetools_0.2-15 lubridate_1.6.0 CompatibilityAPI_1.1.0 foreach_1.4.3 grid_3.3.3
[6] R6_2.2.0 jsonlite_1.3 magrittr_1.5 stringi_1.1.2 iterators_1.0.8
[11] tools_3.3.3 stringr_1.2.0 mrupdate_1.0.1

@vspinu

This comment has been minimized.

Member

vspinu commented Apr 10, 2018

It should work without attaching the package.

## in  a new session on a similar platform as yours
> lubridate::as.period(x = 10, unit = 'day')
[1] "10d 0H 0M 0S"

Might be an instalation issue. Please try to reinstall lubridate. If you use lubridate as imports you can also specify which functions you would like to use and use them without lubridate:: prefix. Shouldn't matter though.

#' @importFrom lubridate as.period

or in NAMESPACE

importFrom(lubridate,as.period)
@rsangole

This comment has been minimized.

rsangole commented Apr 10, 2018

Odd. It might be an installation issue.

On the + side, lubridate::period() works perfectly and I'm going to run with that for now. I'm OK closing this issue.

@vspinu

This comment has been minimized.

Member

vspinu commented Apr 10, 2018

Actually I can reproduce. It's because of the match.fun within as.period. It looks like that function has a non dynamic lookup which is rather unexpected. Give me a sec.

@vspinu vspinu closed this in 4bca361 Apr 10, 2018

@vspinu

This comment has been minimized.

Member

vspinu commented Apr 10, 2018

Actually it has a dynamic lookup which was the cause of the problem. Thanks for reporting this!

@rsangole

This comment has been minimized.

rsangole commented Apr 10, 2018

Wow, super quick!! Thanks @vspinu !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment