R语言编程
20220104_R包构建方法
20220104_R包构建
R包是R语言中的一种可重复使用的代码集合,它通常包含一组函数、数据集、帮助文档和示例代码等,旨在解决某个特定问题或提供某种功能。R包可以通过R语言的命令行或RStudio等集成开发环境进行安装、加载和使用,使得用户能够轻松地使用这些函数和数据集。R包的优势在于它们可以在不同的数据分析项目中重复使用,避免了重复编写代码的繁琐和错误,并使得代码更易于维护和分享。在R社区中,有大量的R包可供下载和使用,可以大大提高数据分析和统计建模的效率和准确性。
=============================================================================
1. 系统环境
操作系统:Linux Ubuntu 12.04.2 LTS 64bit
R语言版本:R 3.0.1
~ uname -a
Linux conan-deskop 3.5.0-23-generic #35~precise1-Ubuntu SMP Fri Jan 25 17:13:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
~ cat /etc/issue
Ubuntu 12.04.2 LTS \n \l
~ R --version
R version 3.0.1 (2013-05-16) -- "Good Sport"
Copyright (C) 2013 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
http://www.gnu.org/licenses/.
2. vi中制作R包
创建目录:/home/conan/R/demo
~ mkdir /home/conan/R/demo
~ cd /home/conan/R/demo
新建R脚本sayHello.R
~ vi sayHello.R
sayHello<-function(name){
print(paste("Hello",name))
}
自定义一个sayHello的函数,将作为自定义包的第一个函数。
启动R程序
~ R
#清空变量设置工作目录
rm(list=ls())
setwd("/home/conan/R/demo")
#通过sayHello的脚本生成开发包的骨架
package.skeleton(name="sayHello",code_files="/home/conan/R/demo/sayHello.R")
Creating directories ...
Creating DESCRIPTION ...
Creating NAMESPACE ...
Creating Read-and-delete-me ...
Copying code files ...
Making help files ...
Done.
Further steps are described in './sayHello/Read-and-delete-me'.
#退出R的客户端
q()
在当前目录生成sayHello目录
~ ls -l
drwxrwxr-x 4 conan conan 4096 8月 1 15:07 sayHello
-rw-rw-r-- 1 conan conan 59 8月 1 15:04 sayHello.R
~ ls -l sayHello
-rw-rw-r-- 1 conan conan 281 8月 1 15:07 DESCRIPTION
drwxrwxr-x 2 conan conan 4096 8月 1 15:07 man
-rw-rw-r-- 1 conan conan 31 8月 1 15:07 NAMESPACE
drwxrwxr-x 2 conan conan 4096 8月 1 15:07 R
-rw-rw-r-- 1 conan conan 420 8月 1 15:07 Read-and-delete-me
~ ls -l sayHello/man
-rw-rw-r-- 1 conan conan 1043 8月 1 15:07 sayHello-package.Rd
-rw-rw-r-- 1 conan conan 1278 8月 1 15:07 sayHello.Rd
~ ls -l sayHello/R
-rw-rw-r-- 1 conan conan 59 8月 1 15:07 sayHello.R
文件及目录解释:
- DESCRIPTION文件: 包描述文件
- NAMESPACE文件: 包的命名空间文件
- Read-and-delete-me文件: 说明文件,可以删除
- man目录: 存放函数的说明文件的目录
- R目录:存放源文件的目录
- man/sayHello.Rd: sayHello函数的说明文件,latex语法,用来生成PDF文档
- man/sayHello-package.Rd: sayHello包的说明文件,可以删除
编辑DESCRIPTION文件:
注意要删掉DESCRIPTION文件中Maintainer: Dan Zhang 这句话,因为不能这样写
~ vi sayHello/DESCRIPTION
Package: sayHello
Type: Package
Title: R package demo for sayHello
LazyLoad: yes
Author: Dan Zhang
Description: This package provides a package demo
License: GPL
Version: 1.0
Date: 2013-07-31
Depends: R (>= 3.0.1)
编辑sayHello.Rd文件:
~ vi sayHello/man/sayHello.Rd
\name{sayHello}
\alias{sayHello}
\title{a sayHello function demo}
\description{
a sayHello function demo
}
\usage{
sayHello(name)
}
\arguments{
\item{name}{a word}
}
\details{
nothing
}
\value{
no return
}
\references{
nothing
}
\author{
Dan Zhang
}
\note{
nothing
}
\seealso{
nothing
}
\examples{
sayHello("world")
}
\keyword{ sayHello }
删除文件:
~ rm sayHello/Read-and-delete-me
~ rm sayHello/man/sayHello-package.Rd
3.打包sayHello
~ R CMD build sayHello
* checking for file ‘sayHello/DESCRIPTION’ ... OK
* preparing ‘sayHello’:
* checking DESCRIPTION meta-information ... OK
* checking for LF line-endings in source and make files
* checking for empty or unneeded directories
* building ‘sayHello_1.0.tar.gz’
~ ls -l
drwxrwxr-x 4 conan conan 4096 8月 1 15:22 sayHello
-rw-r--r-- 1 conan conan 663 8月 1 15:24 sayHello_1.0.tar.gz
-rw-rw-r-- 1 conan conan 59 8月 1 15:04 sayHello.R
得到了sayHello_1.0.tar.gz压缩文件包后,可以在windows下打开Rstudio,然后点击package -> install 方法,手动安装sayHello包。
4.Linux本地安装sayHello
~ R CMD INSTALL sayHello_1.0.tar.gz
* installing to library ‘/home/conan/R/x86_64-pc-linux-gnu-library/3.0’
* installing *source* package ‘sayHello’ ...
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (sayHello)
~ ls /home/conan/R/x86_64-pc-linux-gnu-library/3.0
bitops digest manipulate rstudio sayHello XML
使用sayHello函数
~ R
library(sayHello)
sayHello("Conan")
[1] "Hello Conan"
#查看sayHello文档
?sayHello
sayHello package:sayHello R Documentation
a sayHello function demo
Description:
a sayHello function demo
Usage:
sayHello(name)
Arguments:
name: a word
Details:
nothing
Value:
no return
Note:
nothing
Author(s):
Dan Zhang
References:
nothing
我们包已经成功制作完成!并在本地进行安装和使用!!
参考资料 https://zhuanlan.zhihu.com/p/26333013