前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >「R」使用modules包来组织R的函数集合

「R」使用modules包来组织R的函数集合

作者头像
王诗翔呀
发布2021-09-03 12:12:15
1.1K0
发布2021-09-03 12:12:15
举报
文章被收录于专栏:优雅R优雅R

接触过Python的朋友肯定对模块很熟悉,R的代码组织方式以包为主。但基于文件的模块形式也是可以实现的,modules[1] 包提供了这种支持。

安装和使用

直接从CRAN下载即可:

代码语言:javascript
复制
1install.packages("modules")

使用了解2个函数的使用就可以了。

一是import(),用于替换library()加载包。

代码语言:javascript
复制
 1> library(modules)
 2> gp = import('ggplot2')
 3Masking (modules:ggplot2):
 4  `Position` from: base
 5> args(gp$ggplot)
 6function (data = NULL, mapping = aes(), ..., environment = parent.frame()) 
 7NULL
 8> args(ggplot)
 9function (data = NULL, mapping = aes(), ..., environment = parent.frame()) 
10NULL

这样我们可以直接使用这个函数,也可以通过gp这个对象去访问可用的函数。

如果不想要在全局直接访问包内的函数,在导入时设定attach=FALSE

代码语言:javascript
复制
 1> dp <- import(dplyr, attach = FALSE)
 2Masking (modules:dplyr):
 3  `intersect` from: base
 4  `setdiff` from: base
 5  `setequal` from: base
 6  `union` from: base
 7> select
 8错误: 找不到对象'select'
 9> dp$select
10function (.data, ...) 
11{
12    UseMethod("select")
13}
14<bytecode: 0x7fe5671213f8>
15<environment: namespace:dplyr>

除了导入包,也可以导入具体的包的某个/些函数:

代码语言:javascript
复制
1> slt <- import(dplyr, select)
2Replacing attached import/use on search path for: modules:dplyr.
3> slt$select
4function (.data, ...) 
5{
6    UseMethod("select")
7}
8<bytecode: 0x7fe5671213f8>
9<environment: namespace:dplyr>

我看中的其实不是上面这些特性,而是它可以将文件里写好的函数整体加载为模块对象。

接下来介绍第二个函数。

use()将代码文件加载为模块

最近使用GitHub page的时候发现它的访问速度相当可观,哪怕GitHub主站点本身网络我们国内访问时好时坏。所以,如果我将代码文件放到GitHub上,国内任何的读者应该是可以快速地通过GitHub page访问到其内容。那么,利用GitHub page加上这里介绍的use()函数构建一个可实时获取的代码库是可能的。对于小的脚本函数, 写一个文件总是比写一个包简单轻量。

基于上面的思想,我将去年写的R包安装以及TCGA样本名重过滤等几个函数单独通过GitHub page进行了部署。

代码语言:javascript
复制
1> tcga <- modules::use("https://biosisyphus.github.io/Rlib/tcga.R")
2> tcga$filterReplicates(tsb = c("TCGA-55-7913-01B-11D-2237-01", "TCGA-55-7913-01B-11X-2237-01", "TCGA-55-7913-01B-11D-2237-01"))
3ooo Filter barcodes successfully!
4[1] "TCGA-55-7913-01B-11D-2237-01"

目前放置的几个代码文件都可以在代码库https://github.com/BioSisyphus/Rlib中查看。

这里一个对绝大部分读者有用的函数是install(),它之前被放在R包wfun中。我前几天把它重新进行了迁移和修改。该模块可以替换BiocManager::install()工作,可以从CRAN/BioC/GitHub/Gitee/目录等地方安装包。代码核心其实 就是各种情况的检查,优先使用适合的包和函数进行下载、安装。它的存在就是方便国内使用者,特别是 初学者简便地下载、安装包。

代码语言:javascript
复制
 1install <- modules::use("https://biosisyphus.github.io/Rlib/install.R")
 2> install$install("tidyverse/ggplot2")
 3载入需要的名字空间:remotes
 4Required package remotes has been installed.
 5Installing from GitHub mirrors...
 6试开URL’https://codeload.github.com/tidyverse/ggplot2/zip/HEAD'
 7downloaded 3.3 MB
 8
 9载入需要的名字空间:pkgbuild
10Required package pkgbuild has been installed.
11✓  checking for file ‘/Volumes/Extra/R/Rtmp/RtmpobJHhN/filed328f28489/ggplot2-HEAD/DESCRIPTION’ ...
12─  preparing ‘ggplot2’:
13✓  checking DESCRIPTION meta-information ...
14─  checking for LF line-endings in source and make files and shell scripts (412ms)
15─  checking for empty or unneeded directories
16─  building ‘ggplot2_3.3.5.9000.tar.gz’
17   
18将程序包安装入‘/Volumes/Extra/R/R_Library’
19(因为‘lib’没有被指定)
20Using library: /Volumes/Extra/R/R_Library
21Using temp directory: /Volumes/Extra/R/Rtmp/
22* installing *source* package ‘ggplot2’ ...
23** using staged installation
24** R
25** data
26*** moving datasets to lazyload DB
27** inst
28** byte-compile and prepare package for lazy loading
29Using library: /Volumes/Extra/R/R_Library
30Using temp directory: /Volumes/Extra/R/Rtmp/
31** help
32*** installing help indices
33*** copying figures
34** building package indices
35Using library: /Volumes/Extra/R/R_Library
36Using temp directory: /Volumes/Extra/R/Rtmp/
37** installing vignettes
38** testing if installed package can be loaded from temporary location
39Using library: /Volumes/Extra/R/R_Library
40Using temp directory: /Volumes/Extra/R/Rtmp/
41** testing if installed package can be loaded from final location
42Using library: /Volumes/Extra/R/R_Library
43Using temp directory: /Volumes/Extra/R/Rtmp/
44** testing if installed package keeps a record of temporary installation path
45* DONE (ggplot2)

考虑到该函数的常用性,如果你觉得这个函数好用,可以使用下面的命令将其保存到本地并进行配置:

代码语言:javascript
复制
1install$save()

这样你每次打开RStudio,install模块总是在存在。

如果代码库中程序存在问题,或者你有好的反馈,欢迎file issue。

参考资料

[1]

modules: https://github.com/klmr/modules

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-08-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 优雅R 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装和使用
  • use()将代码文件加载为模块
    • 参考资料
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档