❝本节来介绍两个R包内容较多,小编只介绍其中的两个功能分别是R包官网检索与循环格式转换,更多详细内容请参考官方文档。 ❞
官方文档
❝https://github.com/jsugarelli/packagefinder/ https://github.com/TimTeaFan/loopurrr/ ❞
R包文档检索
代码语言:javascript复制❝经常会遇到一些R包安装的问题使用install.packages会提示R包不存在,这种问题可能是R包未提交到CRAN官方仓库,而安装github上的包则需要知道作者名及仓库名,因此使用packagefinder包在Rstudio内执行代码则会直接跳转到R包官网即可找到安装代码。「注:此方法只适用于CRAN及Github上的R包非100%都能检索,其它仓库不适用」 ❞
install.packages("packagefinder", dependencies = TRUE)
library(packagefinder)
代码语言:javascript复制go("ggh4x","website") # 打开ggh4x包主页
循环格式转换
代码语言:javascript复制❝使用purrr包写循环时会使得过程极为简洁,但是不利于直观阅读代码过程,而loopurrr内的as_loop函数则可将此过程转换为常见的for循环。 ❞
# install.packages("remotes")
remotes::install_github("TimTeaFan/loopurrr")
代码语言:javascript复制library(loopurrr)
library(tidyverse)
代码语言:javascript复制get_supported_fns("as_loop") # 了解当前支持purrr中的哪些函数适用as_loop()
代码语言:javascript复制#> $map
#> [1] "map" "map_at" "map_chr" "map_dbl" "map_df" "map_dfc" "map_dfr"
#> [8] "map_if" "map_int" "map_lgl" "map_raw"
#>
#> $imap
#> [1] "imap" "imap_chr" "imap_dbl" "imap_dfc" "imap_dfr" "imap_int" "imap_lgl"
#> [8] "imap_raw"
#>
#> $map
#> [1] "map2" "map2_chr" "map2_dbl" "map2_df" "map2_dfc" "map2_dfr" "map2_int"
#> [8] "map2_lgl" "map2_raw"
#>
#> $pmap
#> [1] "pmap" "pmap_chr" "pmap_dbl" "pmap_df" "pmap_dfc" "pmap_dfr" "pmap_int"
#> [8] "pmap_lgl" "pmap_raw"
#>
#> $lmap
#> [1] "lmap" "lmap_at"
#>
#> $modify
#> [1] "modify" "modify_at" "modify_if" "modify2" "imodify"
#>
#> $walk
#> [1] "iwalk" "pwalk" "walk" "walk2"
#>
#> $accumulate
#> [1] "accumulate" "accumulate2"
#>
#> $reduce
#> [1] "reduce" "reduce2"
循环展示
代码语言:javascript复制x <- list(1, c(1:2), c(1:3))
x %>% purrr::map(sum)
代码语言:javascript复制> x %>% purrr::map(sum)
[[1]]
[1] 1
[[2]]
[1] 3
[[3]]
[1] 6
循环转换
代码语言:javascript复制x %>%
purrr::map(sum) %>%
as_loop()
# --- convert: `purrr::map(x, sum)` as loop --- #
out <- vector("list", length = length(x))
for (i in seq_along(x)) {
out[[i]] <- sum(x[[i]])
}
# --- end loop --- #
out