79

I need to load to R packages : tseries and chron

Both have a function named is.weekend

I always have in my environment the function from the second package I loaded.

How can I access always the function from, let say, chron ?

Majid
  • 13,853
  • 15
  • 77
  • 113
RockScience
  • 17,932
  • 26
  • 89
  • 125
  • see [how-does-r-handle-overlapping-object-names](https://stats.idre.ucla.edu/r/faq/how-does-r-handle-overlapping-object-names/) – Nick Dong Jun 03 '19 at 13:25

4 Answers4

110

You have probably already noticed that the order of loading the packages makes a difference, i.e. the package that gets loaded last will mask the functions in packages loaded earlier.

To specify the package that you want to use, the syntax is:

chron::is.weekend()
tseries::is.weekend()

In other words, use packagename::functionname()

In addition, if you know that you will always want to use the function in chron, you can define your own function as follows:

is.weekend <- chron::is.weekend    #EDIT
Andrie
  • 176,377
  • 47
  • 447
  • 496
  • 4
    `is.weekend<-chron::is.weekend` is enough. – mbq Apr 06 '11 at 10:25
  • 4
    Not relevant here, so just for future reference in this thread: if the function is *not* exported (i.e. you want a specific S3 method and the method is not exported but the generic is) then the `:::` operator is required. – Gavin Simpson Apr 06 '11 at 10:52
  • 3
    @Gavin That is correct, but I am always very cautious about referring to a function that is not exported. Presumably the package author didn't export it for a reason, and could change the function without warning. To safeguard code that is dependent on this type of function, it might be better to either persuade the package author to export this function, or to get permission to re-use in your own code. – Andrie Apr 06 '11 at 11:04
  • 1
    I agree totally for production code in a package. For personal use I don't see the issues as long as one archives the exact version of the package sources and records details of which versions of packages are used in the data analysis code. Of course, this applies to all use of package code as your are at the whims of the package developers to change things, and all code likely contains some bugs... The key issue is reproducibility in my opinion, the rest we have to accept and live with, but at least one **can** see the code and check it works with R and (most) R packages. – Gavin Simpson Apr 06 '11 at 11:52
  • 1
    How would you go about calling an operator-type function, like %in% which is defined in more than one loaded packages? – LauriK Nov 27 '14 at 08:49
  • 3
    @LauriK Use `base::\`%in%\`` or `\`%in%\` <- base::\`%in%\`` – Andrie Nov 27 '14 at 11:28
  • Thank you @Andrie ! The backticks were the thing I was missing. – LauriK Nov 28 '14 at 11:46
  • Follow-up question to this: it there a way to ask R to use one of the packages by default when both are loaded? In other words, if I only need to use chron's `is.weekend()` once but have to use tseries' `is.weekend()` dozens of times, I would like all of my calls to `is.weekend()` to invoke tseries by default, and prevent my one use of `chron:is.weekend()` from initiating a back-and-forth. Odd example, but I have an analogous Q with `recode()` in dplyr and car where this is a concern – J.Q Apr 01 '18 at 19:23
  • @Andrie why does `base::\`%in%\`` work, but not `base::zeroes`, for example? – Ana Nimbus Aug 02 '19 at 21:50
5
library(chron)
is.weekend.chron <- is.weekend
library(tseries)

then you can call is.weekend for the tseries version or is.weekend.chron for the chron version

jberg
  • 4,758
  • 2
  • 20
  • 15
5

you should turn to the conflicted package from Hadly.

library(conflicted)
library(dplyr)
filter(mtcars, am & cyl == 8)

Then the conflicted package will throw an error and will force you clearly determine which function you prefer:

Error: filter found in 2 packages. You must indicate which one you want with :: * dplyr::filter * stats::filter

To resolve conflicts for your entire session, use <-:

filter <- dplyr::filter
filter(mtcars, am & cyl == 8)
    mpg cyl disp  hp drat   wt qsec vs am gear carb
1 15.8   8  351 264 4.22 3.17 14.5  0  1    5    4
2 15.0   8  301 335 3.54 3.57 14.6  0  1    5    8

You can also turn to the conflict_prefer() function which can determine the winner when a conflict occur. The code example is borrowed from Hadly, pls refer to the package website. https://www.tidyverse.org/blog/2018/06/conflicted/

MDEWITT
  • 2,338
  • 2
  • 12
  • 23
lovelyzlf
  • 107
  • 1
  • 11
0

i had 2 packages who had same function name ts() The 2 pacckages who had the same were :

  1. forecast
  2. List item

I inspected what was going on by typing

?ts


Help on topic 'ts' was found in the following packages:

Time-Series Objects
(in package stats in library C:/Program Files/R/R-3.6.2/library)
Format time stamps
(in package bReeze in library C:/Users/mycomputer/Documents/R/win-library/3.6)

Solution : Then to use the function ts that comes with package forecast i used : because the help showed me that forcast was calling stats

Time-Series Objects (in package stats

stats::ts

because is saw from help that forecasts use a package called stats ;)

forecast::ts

Time-Series Objects
(in package stats

was giving me error, because forecast package was using a sub package ;

so final usage looks like this :

library(bReeze)
library(forecast)

# Subset data
my_time_series <- stats::ts(c(df_sub$y), start=2018, frequency = 12)

# Plot
theme_set(theme_classic())
ggseasonplot(my_time_series) + labs(title="My graph title")
Community
  • 1
  • 1
sylvain
  • 101
  • 1
  • 2