简体   繁体   English

访问R包中的子目录

[英]Accessing a sub directory in a R package

I have a data folder in an R package. 我在R包中有一个数据文件夹。 With in the the data directory I have a sub directory data/JE. 在数据目录中我有一个子目录data / JE。 I want to access this directory and apply list.file function and then using the list I want to read each file. 我想访问此目录并应用list.file函数,然后使用我想要读取每个文件的列表。 I can setwd() when not running the package. 不运行包时我可以setwd()。 But while the package is running I don't know how to access the directory. 但是当程序包运行时我不知道如何访问该目录。

     JESP = system.file("data/JE",package="DNA_test")
     listoffiles <- list.files(path=JESP)

To access files in the pacakg I use the system.file function. 要访问pacakg中的文件,我使用system.file函数。 But I don't know how to access a directory and read files from it. 但我不知道如何访问目录并从中读取文件。 I use the following statement in the loop to read 我在循环中使用以下语句来阅读

     check <- as.matrix(read.csv(file=listoffiles[j],sep=",",header=FALSE))

You should be able to modify this to suit your needs: 您应该能够修改它以满足您的需求:

> packagePath <- find.package("survival", lib.loc=NULL, quiet = TRUE)
> packagePath
[1] "/Library/Frameworks/R.framework/Versions/3.0/Resources/library/survival"

> list.files(packagePath)
 [1] "CITATION"    "data"        "DESCRIPTION" "doc"         "help"        "html"       
 [7] "INDEX"       "libs"        "Meta"        "NAMESPACE"   "NEWS.Rd"     "R"          
> list.files(paste0(packagePath, "/data") )
[1] "Rdata.rdb" "Rdata.rds" "Rdata.rdx"

If you need a further look at how system.file works, just type 如果您需要进一步了解system.file工作原理,请输入

system.file 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM