In RStudio when we load packages into our session we may encounter same function names in different packages. Take e.g. the raster and the dplyr-package (latter part of the tidyverse):
library(tidyverse)
## ── Attaching packages ────────────────────────────────── tidyverse 1.3.2.9000 ──
## ✔ ggplot2 3.3.6 ✔ dplyr 1.0.10
## ✔ tibble 3.1.8 ✔ stringr 1.4.1
## ✔ tidyr 1.2.1 ✔ forcats 0.5.2
## ✔ readr 2.1.2 ✔ lubridate 1.8.0
## ✔ purrr 0.3.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(raster)
## Loading required package: sp
##
## Attaching package: 'raster'
##
## The following object is masked from 'package:dplyr':
##
## select
Take careful note of the message you get when loading libraries. It may help when resolving quesitons downstream in your code.
Before we do some raster work we may first want to do some data manipulations work. E.g. select some variables in the data:
minke <-
read_csv("ftp://ftp.hafro.is/pub/data/csv/minke.csv")
## Rows: 190 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): area, sex, maturity
## dbl (9): id, lon, lat, length, weight, age, stomach.volume, stomach.weight,...
## dttm (1): date
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
minke %>%
select(id, lon, lat)
## Error in (function (classes, fdef, mtable) : unable to find an inherited method for function 'select' for signature '"spec_tbl_df"'
The problem here is that the select function from the dplyr-package is now masked with the select function in the raster-package. And because we loaded the raster-package after the tidyverse-packages (load among other things the dplyr-package) the call is to the function the former. To see the order of your packages you can type:
search()
## [1] ".GlobalEnv" "package:raster" "package:sp"
## [4] "package:lubridate" "package:forcats" "package:stringr"
## [7] "package:dplyr" "package:purrr" "package:readr"
## [10] "package:tidyr" "package:tibble" "package:ggplot2"
## [13] "package:tidyverse" "package:stats" "package:graphics"
## [16] "package:grDevices" "package:utils" "package:datasets"
## [19] "package:methods" "Autoloads" "package:base"
Since the raster-package is in enviroment 2 and the dplyr-package in postion 6 any conflicting function name that reside in these two packages will be default be taken from the raster package.
The solution is to add the package-name in front of the function:
minke %>%
dplyr::select(id, lon, lat)
## # A tibble: 190 × 3
## id lon lat
## <dbl> <dbl> <dbl>
## 1 1 -21.4 65.7
## 2 690 -21.4 65.7
## 3 926 -19.8 66.5
## 4 1333 -21.6 65.7
## 5 1334 -15.6 66.3
## 6 1335 -18.7 66.2
## 7 1336 -21.5 65.7
## 8 1338 -22.8 66.1
## 9 1339 -17.5 66.6
## 10 1341 -14.7 66.2
## # … with 180 more rows
There is a package in development (as of this writing not in R) that may also be helpful, check out conflicted.