r语言 之自己构建package

来源:互联网 发布:笔记本如何安装软件 编辑:程序博客网 时间:2024/06/05 16:19

从一大堆英文文档总结来的啊,心酸。。。。
首先介绍一下具体步骤;
第一步:新建一个目录,随意名称,我新建的是
/Users/Mac/Desktop/R
第二步:新建一个demo.R的脚本,名称随意;
编写函数,很简单的一个函数
demo1<-function(x,y)
{
return(x+y);
}
第三步:开启R,

rm(list=ls())
setwd(“/Users/Mac/Desktop/R”)
package.skeleton(name=”demo1”,code_file=”/Users/Mac/Desktop/R/demo.R”)
Creating directories …
Creating DESCRIPTION …
Creating NAMESPACE …
Creating Read-and-delete-me …
Copying code files …
Making help files …
Done.
Further steps are described in ‘./demo1/Read-and-delete-me’.
首先解释一下哈,package.skeleton(name=”demo1”,code_file=”/Users/Mac/Desktop/R/demo.R”)
name是你新建的包的名称,code_file是你函数文件位置
第四步:编辑第三步生成的文件
首先编辑Description这个文件,
Package: demo1
Type: Package
Title: demo
Version: 1.0
Date: 2015-03-12
Author: novas
Maintainer: novas
Description: demo
License: GPL
Depends: R (>= 3.0.1)
这个是我写的,很容易看懂就不详细介绍了。
然后还要编辑demo1.rd这个文件。里面每个属性都要有值。
\name{demo1}
\alias{demo1}
%- Also NEED an ‘\alias’ for EACH other topic documented here.
\title{
nothing
%% function to do ... ~~
}
\description{
nothing
%% ~~ A concise (1-5 lines) description of what the function does. ~~
}
\usage{
demo1(x, y)
}
%- maybe also 'usage' for other objects documented here.
\arguments{
\item{x}{
nothing
%% ~~Describe \code{x} here

}
\item{y}{
nothing
%% Describe \code{y} here
}
}
\details{
nothing
%% ~~ If necessary, more details than the description above ~~
}
\value{
nothing
%% ~Describe the value returned
%% If it is a LIST, use
%% \item{comp1 }{Description of ‘comp1’}
%% \item{comp2 }{Description of ‘comp2’}
%% …
}
\references{
nothing
%% ~put references to the literature/web site here ~
}
\author{
nothing
%% who you are
}
\note{
nothing
%% further notes
}

%% ~Make other sections like Warning with \section{Warning }{….} ~

\seealso{
nothing
%% objects to See Also as \code{\link{help}}, ~
}
\examples{
nothing

—- Should be DIRECTLY executable !! —-

– ==> Define data, use random,

– or do help(data=index) for the standard data sets.

The function is currently defined as

}
% Add one or more standard keywords, see file ‘KEYWORDS’ in the
% R documentation directory.
\keyword{ nothing}
\keyword{ nothing}% ONLY ONE keyword per line

然后删除Read-and-delete-me和demo1-package.rd
这两个文件。
第四步:
进入R这个文件夹,编译;
R CMD build demo1
* checking for file ‘demo1/DESCRIPTION’ … OK
* preparing ‘demo1’:
* checking DESCRIPTION meta-information … OK
* checking for LF line-endings in source and make files
* checking for empty or unneeded directories
* building ‘demo1_1.0.tar.gz’
第五步:安装自己写的这个包
R CMD INSTALL demo1_1.0.tar.gz
* installing to library ‘/Library/Frameworks/R.framework/Versions/3.1/Resources/library’
* installing source package ‘demo1’ …
** R
** preparing package for lazy loading
** help
* installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (demo1)
最后一步:进入R

library(demo1)
demo1(2,3)
[1] 5

执行了刚才自己写的函数了吧,这个是第一步,假如在自己写的包里引用了其他包呢
就需要在description文件中,增加
Depends:tm
假设你引入了tm这个包里面的函数。

1 0