简体   繁体   中英

Accessing a sub directory in a R package

I have a data folder in an R package. With in the the data directory I have a sub directory data/JE. I want to access this directory and apply list.file function and then using the list I want to read each file. I can setwd() when not running the package. 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. 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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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