2

I have no problem making this tibble:

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

Which produce this:

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

But when I tried to wrap it with reprex

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

The clipboard showed this:

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

What's the right way to do it?

www
  • 38,575
  • 12
  • 48
  • 84
neversaint
  • 60,904
  • 137
  • 310
  • 477

1 Answers1

5

You should put package loading also into expression, otherwise the example is not reproducible:

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

This will produce:

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
mt1022
  • 16,834
  • 5
  • 48
  • 71