编写R包(1)

R包相较于source导入函数可也极大减少空间占用,提高代码运行效率。下面我们编写一个简单的R包。

library('devtools')
getwd()
create('./simple')#在指定文件夹下。create('~/simple')
setwd('./simple')
dir()#列出文件和文件夹
load_all()#导入包
asd(1,2)#使用函数
document()#生成文档
build()#生成安装包

在目录中R文件下添加R脚本,写入如下内容

#Shortcut keys: 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)
}

安装生成的R包

install.packages("R包名")

你可能感兴趣的:(常用方法,数据库)