R规范学习—基础知识

不以规矩 不能成方圆

###part one 管理R工作空间的命令
getwd()    #returns an absolute filepath representing the current working directory of the R process
setwd()    #set the working directory
Sys.info()  #Reports system and user information.
Sys.Date()  #current date 
Sys.time()
Sys.getenv()  #obtains the environment variables
list.dirs()  
list.files() #directories in the named directory

rm(objectlist) #移除(删除)一个或多个对象
options() #显示或设置当前选项
history()  #显示最近使用过的#个命令(默认值为 25)
savehistory("myfile")  #保存命令历史到文件myfile中(默认值为.Rhistory)
loadhistory("myfile")  #载入一个命令历史文件(默认值为.Rhistory)
save.image("myfile")  #保存工作空间到文件 myfile 中(默认值为.RData)
save(objectlist, file="myfile")  #保存指定对象到一个文件中
load("myfile")  #读取一个工作空间到当前会话中(默认值为.RData)
dir.create()   #创建新目录
q()  #退出R将会询问你是否保存工作空间

###part two 规范代码
install.packages("formatR")
library(formatR)
#01 tidy_source()以字符串作为输入函数,对代码进行格式化
tidy_source(text = c("{if(TRUE)1 else 2;if(FALSE){1+1","##comments","}else 2}"))
#02 文件格式化
messy <- system.file("format","messy.R",package = "formatR")
messy
strc<-readLines(messy)
cat(strc,sep = "\n")
tidy_source(messy)
#03 格式化并输出R脚本文件
x<-"Empirical_likelihood.R"  #Don't have Chinese comments
tidy_source(x)
f<-"Empirical_likelihood01.R"  #export formatting result
tidy_source(x,blank = TRUE,file=f)  #感觉还不错,部分中文留下来了

#04 输出格式化代码和运行结果
tidy_eval(text = c("a<-1+1;a", "matrix(rnorm(10),5)"))

#05 格式化函数定义
plus01<-function(a,b,c){return(a^b+a*c+b%/%c)}
plus02<-function(a,b,c){return(a^b+a*c+b%%c)}
plus01(2,3,4) 
plus02(2,3,4) 
usage(plus01) #只打印函数定义
#06 格式化目录中的文件
tidy_dir(path = "D:/R")

###part three  R中的帮助函数
search()  #查看当前环境已加载的R包
ls() #查看当前环境空间的变量
help.start() #打开帮助文档首页
help("foo") #或 
?foo #查看函数 foo 的帮助(引号可以省略)
help.search("foo") 
??foo #以 foo 为关键词搜索本地帮助文档
example("foo")
#函数 foo 的使用示例(引号可以省略)
RSiteSearch("foo")
#以 foo 为关键词搜索在线文档和邮件列表存档
apropos("foo", mode="function")
#列出名称中含有 foo 的所有可用函数
data()
#列出当前已加载包中所含的所有可用示例数据集
vignette()
#列出当前已安装包中所有可用的 vignette 文档
vignette("foo")
#为主题 foo 显示指定的 vignette 文档

###part four
#输入和输出记住几个函数即可
source()  #在当前会话中执行一个脚本
sink()  #将输出重定向到文件中
dev.off()  #要重定向图形输出请使用jpg()等系列,用dev.off返回终端
##包的安装,载入
#01====install.packages("package_name");library(package_name)
#02====source("http://bioconductor.org/biocLite.R")
      #biocLite("package_name")
#03====install.packages("devtools");library(devtools);install_github("hadley/private", auth_token = "abc")
#04====下载安装包,解压到library目录下刷新即可

###part five
#R语言编程中的常见错误要避免
#结果的重用就是找对象,没对象你就自己想象吧

熟悉熟悉正在熟悉,逐步规范自己,不然真的就是瞎学,哭唧唧!

猜你喜欢

转载自blog.csdn.net/zhuangailing/article/details/81045942
今日推荐