2017-04-21 37 views
1

我沒有問題,使得這種tibble:爲什麼reprex無法呈現%>%結果

library(dplyr) 
library(tibble) 
as.tibble(mtcars[2:3,2:3]) %>% mutate(cyl_x_disp = cyl * disp) 

產生本:

# A tibble: 2 × 3 
    cyl disp cyl_x_disp 
    <dbl> <dbl>  <dbl> 
1  6 160  960 
2  4 108  432 

但是,當我試圖用reprex

把它包
reprex::reprex(as.tibble(mtcars[2:3,2:3]) %>% mutate(cyl_x_disp = cyl * disp)) 

剪貼板顯示此:

as.tibble(mtcars[2:3, 2:3]) %>% mutate(cyl_x_disp = cyl * disp) 
#> Error in eval(expr, envir, enclos): could not find function "%>%" 

什麼是正確的做法?

回答

3

你應該把包也加載到表達,否則的例子是不可重現:

reprex::reprex({ 
    library(tibble) 
    library(dplyr) 
    as.tibble(mtcars[2:3,2:3]) %>% mutate(cyl_x_disp = cyl * disp) 
}) 

這將產生:

library(tibble) 
library(dplyr) 
#> 
#> Attaching package: 'dplyr' 
#> The following objects are masked from 'package:stats': 
#> 
#>  filter, lag 
#> The following objects are masked from 'package:base': 
#> 
#>  intersect, setdiff, setequal, union 
as.tibble(mtcars[2:3, 2:3]) %>% mutate(cyl_x_disp = cyl * disp) 
#> # A tibble: 2 × 3 
#>  cyl disp cyl_x_disp 
#> <dbl> <dbl>  <dbl> 
#> 1  6 160  960 
#> 2  4 108  432 
相關問題