LoginSignup
1
1

More than 5 years have passed since last update.

パイプ演算子の途中結果を後から使う

Posted at

やりたいこと

パイプ演算子でつなげている途中結果から、関数に入れた結果を保持して、後からそれを参照したい

pipe中毒になったら、更にわがままな人間になった の記事をみてたのですが、うまくできなかったので、調べたメモを残しておく

参考 : R pipe (%>%) capabilities - storage and partial use?

save_to <- function(x, v, fun) {
  var <- substitute(v)
  eval(bquote(.(var) <- .(fun(x))), envir = globalenv())
  x
}

mtcars %>%
  group_by(cyl, carb) %>%
  dplyr::summarise(sgear = sum(gear)) %>%
  save_to(num_cols, ncol) %>%
  kable() %>% 
  kableExtra::kable_styling(full_width = T) %>% 
  kableExtra::add_header_above(c("Group" = num_cols - 1, "Value" = 1))

途中で列数を数えた結果をnum_colsに入れて、あとからそれを使っている

おわりに

magrittrにありそう。pipeRは最近見ないが今でもactiveなのだろうか

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1