2

我制作这个小标题没有问题:

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 "%>%"

正确的方法是什么?

4

1 回答 1

5

您应该将包加载也放入表达式中,否则该示例不可重现:

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
于 2017-04-21T01:25:41.380 回答