R包相较于source导入函数可也极大减少空间占用,提高代码运行效率。下面我们编写一个简单的R包。
#基础代码
library('devtools')
getwd()
create('./simple')#在指定文件夹下。create('~/simple')
setwd('./simple')
dir()#列出文件和文件夹
在目录中R文件下添加R脚本,写入如下内容
#Quickly add comments: crtl+shift+alt+R
#' The sum of two numbers
#'
#' @param x a number, float or int
#' @param y a number, float or int
#'
#' @return as a number, float or int
#' @export
#'
#' @examples
#' asd(2,3)
asd<-function(x,y){
as=x+y
return(as)
}
#测试
load_all()#导入所有函数
asd(1,2)#使用函数
安装生成的R包
document()#生成文档
build()#生成安装包
install.packages("R包路径")#安装
1、易错点:R文件下的R脚本的函数没有写注释。导致生成的包安装以后,无法使用!注释快捷键是crtl+shift+alt+R。