0

In a script I need to load packages tydiverse and gdistance.

gdistance actually loads several other packages and overall mask some functions of the tydiverse (e.g. select).

I tried to rearrange the script by loading the gdistance only when needed and having the related lines at the end of the script.

Anyway one of the very last line I need still use the function tydiverse::select, which is then not found and it throws an error.

Is there a way to make a copy of the R environment before loading the gdistance package so that I can then restore the environment as it was before loading the pakage that induce the problem?

Filippo
  • 309
  • 1
  • 2
  • 10
  • 2
    You've got the answer in your question -- use `dplyr::select()` (the home of the `select()` 'generic') rather than `select()` in your script. – Martin Morgan Jan 15 '20 at 22:26
  • oh ok... didn't know the :: could do that... I'll try – Filippo Jan 15 '20 at 22:32
  • Great, it works! – Filippo Jan 15 '20 at 22:35
  • `tidyverse` is just a package for loading lots of other functions, so you need `dplyr::select`, not `tidyverse::select`. You'll also have trouble if you're spelling it in your code the way it's misspelled in your question – camille Jan 15 '20 at 22:51

1 Answers1

0

Here are some alternatives:

1) Simply load tidyverse second since the most recently loaded name takes precdence.

library(gdistance)
library(tidyverse)

2) select actually comes from the raster package used by gdistance, not from gdistance itself. Also, in R version 3.6+ library has an exlude= argument to exclude loading specified names. Thus we can write the follwoing to ensure that select will refer to dplyr.

# needs R 3.6+ 
library(tidyverse)
library(raster, exclude = "select")
library(gdistance)

3) Use dplyr::select to force select to be used from dplyr.

4) detach gdistance and raster before using select and load them back in afterwards (if you still need them).

library(tidyverse)
library(gdistance)

# ... select refers to select from raster

detach("package:gdistance")
detach("package:raster")

# ... select refers to select from dplyr

library(gdistance)

# ... select refers to select from raster
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341