% mean() 这目前给出一个错误,因为 saveRDS 返回空值-6ren">
gpt4 book ai didi

r - 在管道 %>% 中保存 RDS 并让它返回对象

转载 作者:行者123 更新时间:2023-12-02 06:47:01 26 4
gpt4 key购买 nike

有没有办法在管道 %>% 链中使用 saveRDS?

c(1,2,3) %>% 
saveRDS(file="123.rda") %>%
mean()

这目前给出一个错误,因为 saveRDS 返回空值。

我希望 saveRDS() 返回 c(1,2,3)!

最佳答案

我们可以使用 magrittr 中的 tee (%T>) 运算符

library(magrittr)
1:3 %T>%
saveRDS(file="123.rda") %>%
mean
#[1] 2

如果我们想返回同一个对象,使用I

1:3 %T>% 
saveRDS(file="123.rda") %>%
I
#[1] 1 2 3

根据?"%T>%"

Pipe a value forward into a function- or call expression and return the original value instead of theresult. This is useful when an expression is used for its side-effect, say plotting or printing.

关于r - 在管道 %>% 中保存 RDS 并让它返回对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56687241/

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