gpt4 book ai didi

r - 为什么 reprex 无法渲染 %>% 结果

转载 作者:行者123 更新时间:2023-12-02 03:56:51 24 4
gpt4 key购买 nike

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

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

正确的做法是什么?

最佳答案

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

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

关于r - 为什么 reprex 无法渲染 %>% 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43532098/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com