tidyverse / magrittr

Improve the readability of R code with the pipe
https://magrittr.tidyverse.org
Other
957 stars 157 forks source link

example for extract alias is not working #189

Closed Make42 closed 5 years ago

Make42 commented 5 years ago

running

iris %>%
+    extract(, 1:4) %>%
+    head

I get the error

Error in eval_tidy(enquo(var), var_env) : object '' not found

cderv commented 5 years ago

It seems fine

library(magrittr)
packageVersion("magrittr")
#> [1] '1.5'
iris %>%
  extract(, 1:4) %>%
  head
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width
#> 1          5.1         3.5          1.4         0.2
#> 2          4.9         3.0          1.4         0.2
#> 3          4.7         3.2          1.3         0.2
#> 4          4.6         3.1          1.5         0.2
#> 5          5.0         3.6          1.4         0.2
#> 6          5.4         3.9          1.7         0.4
iris %>%
  extract(1:4,) %>%
  head
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1          5.1         3.5          1.4         0.2  setosa
#> 2          4.9         3.0          1.4         0.2  setosa
#> 3          4.7         3.2          1.3         0.2  setosa
#> 4          4.6         3.1          1.5         0.2  setosa

Created on 2019-01-04 by the reprex package (v0.2.1)

You may just have a conflict with tidyr::extract https://tidyr.tidyverse.org/reference/extract.html

You can use

iris %>%
  magrittr::extract(, 1:4) %>%
  head

Also, see the conflicted 📦 to help see through those situations

# this will prints an error if there is conflicts between functions
``` r
library(conflicted)
#> Warning: le package 'conflicted' a été compilé avec la version R 3.5.2
library(magrittr)
library(tidyr)
iris %>%
  extract(, 1:4) %>%
  head
#> Error: [conflicted] `extract` found in 2 packages.
#> Either pick the one you want with `::` 
#> * tidyr::extract
#> * magrittr::extract
#> Or declare a preference with `conflict_prefer()`
#> * conflict_prefer("extract", "tidyr")
#> * conflict_prefer("extract", "magrittr")
# or extract <- magrittr::extract
conflict_prefer("extract", "magrittr")
#> [conflicted] Will prefer magrittr::extract over any other package
iris %>%
  extract(,1:4) %>%
  head
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width
#> 1          5.1         3.5          1.4         0.2
#> 2          4.9         3.0          1.4         0.2
#> 3          4.7         3.2          1.3         0.2
#> 4          4.6         3.1          1.5         0.2
#> 5          5.0         3.6          1.4         0.2
#> 6          5.4         3.9          1.7         0.4

Can you check ?

Make42 commented 5 years ago

You seem to be right. Thank you also for the tip regarding conflicted.