# 启动R程序
~ R
# 当前的目录
> getwd()
[1] "/home/conan/R/fs"
# 查看当前目录的子目录
> list.dirs()
[1] "." "./tmp"
> dir()
[1] "readme.txt" "tmp"
<span style="background-color: rgb(255, 255, 255);"># 查看指定目录的子目录和文件。</span>
<span style="background-color: rgb(255, 255, 255);"># 只列出以字母R开头的子目录或文件</span>
> list.files()
[1] "readme.txt" "tmp"
> list.files(".",all.files=TRUE)
[1] "." ".." "readme.txt" "tmp"
# 查看当前目录权限
> file.info(".")
size isdir mode mtime ctime atime uid gid uname grname
. 4096 TRUE 775 2013-11-14 08:40:46 2013-11-14 08:40:46 2013-11-14 08:41:57 1000 1000 conan conan
# 查看指定目录权限
> file.info("./tmp")
size isdir mode mtime ctime atime uid gid uname grname
./tmp 4096 TRUE 775 2013-11-14 14:35:56 2013-11-14 14:35:56 2013-11-14 14:35:56 1000 1000 conan conan
# 在当前目录下,新建一个目录
> dir.create("create")
> list.dirs()
[1] "." "./create" "./tmp"
# 直接创建,出错
> dir.create(path="a1/b2/c3")
Warning message:
In dir.create(path = "a1/b2/c3") :
cannot create dir ‘a1/b2/c3‘, reason ‘No such file or directory‘
# 递归创建,成功
> dir.create(path="a1/b2/c3",recursive = TRUE)
> list.dirs()
[1] "." "./a1" "./a1/b2" "./a1/b2/c3" "./create" "./tmp"
# 通过系统命令查看目录结构
> system("tree")
.
├── a1
│ └── b2
│ └── c3
├── create
├── readme.txt
└── tmp
# 目录存在
> file.exists(".")
[1] TRUE
> file.exists("./a1/b2")
[1] TRUE
# 目录不存在
> file.exists("./aa")
[1] FALSE
> df<-dir(full.names = TRUE)
# 检查文件或目录是否存在,mode=0
> file.access(df, 0) == 0
./a1 ./create ./readme.txt ./tmp
TRUE TRUE TRUE TRUE
# 检查文件或目录是否可执行,mode=1,目录为可以执行
> file.access(df, 1) == 0
./a1 ./create ./readme.txt ./tmp
TRUE TRUE FALSE TRUE
# 检查文件或目录是否可写,mode=2
> file.access(df, 2) == 0
./a1 ./create ./readme.txt ./tmp
TRUE TRUE TRUE TRUE
# 检查文件或目录是否可读,mode=4
> file.access(df, 4) == 0
./a1 ./create ./readme.txt ./tmp
TRUE TRUE TRUE TRUE
# 修改目录权限,所有用户只读
> Sys.chmod("./create", mode = "0555", use_umask = TRUE)
# 查看目录完整信息,mode=555
> file.info("./create")
size isdir mode mtime ctime atime uid gid uname grname
./create 4096 TRUE 555 2013-11-14 08:36:28 2013-11-14 09:07:05 2013-11-14 08:36:39 1000 1000 conan conan
# create目录不可以写
> file.access(df, 2) == 0
./a1 ./create ./readme.txt ./tmp
TRUE FALSE TRUE TRUE
# 对tmp目录重命名
> file.rename("tmp", "tmp2")
[1] TRUE
# 查看目录
> dir()
[1] "a1" "create" "readme.txt" "tmp2"
# 删除tmp2目录
> unlink("tmp2", recursive = TRUE)
# 查看目录
> dir()
[1] "a1" "create" "readme.txt"
# 拼接目录字符串
> file.path("p1","p2","p3")
[1] "p1/p2/p3"
> dir(file.path("a1","b2"))
[1] "c3"
# 当前目录
> getwd()
[1] "/home/conan/R/fs"
# 最底层子目录
> dirname("/home/conan/R/fs/readme.txt")
[1] "/home/conan/R/fs"
# 最底层子目录或文件名
> basename(getwd())
[1] "fs"
> basename("/home/conan/R/fs/readme.txt")
[1] "readme.txt"
# 转换~为用户目录
> path.expand("~/foo")
[1] "/home/conan/foo"
# linux
> normalizePath(c(R.home(), tempdir()))
[1] "/usr/lib/R" "/tmp/RtmpqNyjPD"
# win
> normalizePath(c(R.home(), tempdir()))
[1] "C:\\Program Files\\R\\R-3.0.1"
[2] "C:\\Users\\Administrator\\AppData\\Local\\Temp\\RtmpMtSnci"
# win
> shortPathName(c(R.home(), tempdir()))
[1] "C:\\PROGRA~1\\R\\R-30~1.1"
[2] "C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\RTMPMT~1"
> dir()
[1] "create" "readme.txt"
# 检查文件是否存在
> file.exists("readme.txt")
[1] TRUE
# 文件不存在
> file.exists("readme.txt222")
[1] FALSE
# 查看文件完整信息
> file.info("readme.txt")
size isdir mode mtime ctime atime uid gid uname grname
readme.txt 7 FALSE 664 2013-11-14 08:24:50 2013-11-14 08:24:50 2013-11-14 08:24:50 1000 1000 conan conan
# 查看文件访问权限,存在
> file.access("readme.txt",0)
readme.txt
0
# 不可执行
> file.access("readme.txt",1)
readme.txt
-1
# 可写
> file.access("readme.txt",2)
readme.txt
0
# 可读
> file.access("readme.txt",4)
readme.txt
0
# 查看一个不存在的文件访问权限,不存在
> file.access("readme.txt222")
readme.txt222
-1
# 判断是否是目录
> file_test("-d", "readme.txt")
[1] FALSE
> file_test("-d", "create")
[1] TRUE
# 判断是否是文件
> file_test("-f", "readme.txt")
[1] TRUE
> file_test("-f", "create")
[1] FALSE
# 创建一个空文件 A.txt
> file.create("A.txt")
[1] TRUE
# 创建一个有内容的文件 B.txt
> cat("file B\n", file = "B.txt")
> dir()
[1] "A.txt" "B.txt" "create" "readme.txt"
# 打印A.txt
> readLines("A.txt")
character(0)
# 打印B.txt
> readLines("B.txt")
[1] "file B"
# 合并文件
> file.append("A.txt", rep("B.txt", 10))
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
# 查看文件内容
> readLines("A.txt")
[1] "file B" "file B" "file B" "file B" "file B" "file B" "file B" "file B" "file B" "file B"
# 复制文件
> file.copy("A.txt", "C.txt")
[1] TRUE
# 查看文件内容
> readLines("C.txt")
[1] "file B" "file B" "file B" "file B" "file B" "file B" "file B" "file B" "file B" "file B"
# 修改文件权限,创建者可读可写可执行,其他人无权限
> Sys.chmod("A.txt", mode = "0700", use_umask = TRUE)
# 查看文件信息
> file.info("A.txt")
size isdir mode mtime ctime atime uid gid uname grname
A.txt 70 FALSE 700 2013-11-14 12:55:18 2013-11-14 12:57:39 2013-11-14 12:55:26 1000 1000 conan conan
# 给文件A.txt重命名为AA.txt
> file.rename("A.txt","AA.txt")
[1] TRUE
> dir()
[1] "AA.txt" "B.txt" "create" "C.txt" "readme.txt"
# 硬连接
> file.link("readme.txt", "hard_link.txt")
[1] TRUE
# 软连接
> file.symlink("readme.txt", "soft_link.txt")
[1] TRUE
# 查看文件目录
> system("ls -l")
-rwx------ 1 conan conan 70 Nov 14 12:55 AA.txt
-rw-rw-r-- 1 conan conan 7 Nov 14 12:51 B.txt
dr-xr-xr-x 2 conan conan 4096 Nov 14 08:36 create
-rw-rw-r-- 1 conan conan 70 Nov 14 12:56 C.txt
-rw-rw-r-- 2 conan conan 7 Nov 14 08:24 hard_link.txt
-rw-rw-r-- 2 conan conan 7 Nov 14 08:24 readme.txt
lrwxrwxrwx 1 conan conan 10 Nov 14 13:11 soft_link.txt -> readme.txt
# 删除文件
> file.remove("A.txt", "B.txt", "C.txt")
[1] FALSE TRUE TRUE
# 删除文件
> unlink("readme.txt")
# 查看目录文件
> system("ls -l")
total 12
-rwx------ 1 conan conan 70 Nov 14 12:55 AA.txt
dr-xr-xr-x 2 conan conan 4096 Nov 14 08:36 create
-rw-rw-r-- 1 conan conan 7 Nov 14 08:24 hard_link.txt
lrwxrwxrwx 1 conan conan 10 Nov 14 13:11 soft_link.txt -> readme.txt
# 打印硬连接文件
> readLines("hard_link.txt")
[1] "file A"
# 打印软连接文件,soft_link.txt,由于原文件被删除,有错误
> readLines("soft_link.txt")
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
cannot open file ‘soft_link.txt‘: No such file or directory
# 打印R软件安装目录
> R.home()
[1] "/usr/lib/R"
# 打印R软件bin的目录
> R.home(component="bin")
[1] "/usr/lib/R/bin"
# 打印R软件文件的目录
> R.home(component="doc")
[1] "/usr/share/R/doc"
通过系统命令,找到R文件的位置。
# 检查系统中R文件的位置
~ whereis R
R: /usr/bin/R /etc/R /usr/lib/R /usr/bin/X11/R /usr/local/lib/R /usr/share/R /usr/share/man/man1/R.1.gz
# 打印环境变量R_HOME
~ echo $R_HOME
/usr/lib/R
通过R.home()函数,我们可以很容易的定位R软件的目录。
# 打印核心包的目录
> .Library
[1] "/usr/lib/R/library"
# 打印核心包的目录和root用户安装包目录
> .Library.site
[1] "/usr/local/lib/R/site-library" "/usr/lib/R/site-library"
[3] "/usr/lib/R/library"
# 打印所有包的存放目录
> .libPaths()
[1] "/home/conan/R/x86_64-pc-linux-gnu-library/3.0"
[2] "/usr/local/lib/R/site-library"
[3] "/usr/lib/R/site-library"
[4] "/usr/lib/R/library"
# base包的存放目录
> system.file()
[1] "/usr/lib/R/library/base"
# pryr包的存放目录
> system.file(package = "pryr")
[1] "/home/conan/R/x86_64-pc-linux-gnu-library/3.0/pryr"
原文:http://blog.csdn.net/u013361361/article/details/40797853