Reputation: 377
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))
Upvotes: 4
Views: 1654
Reputation: 263352
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
Upvotes: 6