In R, I want to access to some file in subfolder. But I don\'t want to change working directory then move back. It lost time and long.
For exmaple, I working on /
You could use what Hadley calls a closure in Advanced R if I understand what you're after:
## Make a function that takes a path and another function
## and returns that same function with the path pre-progammed in
pathit <- function(FUN, path){
function(file, ...){
FUN(file=file.path(path, file), ...)
}
}
## generate new functions that have the path pre-programmed in
read.csv2b <- pathit(read.csv, "home/phuong/data1")
source2 <- pathit(source, "home/phuong/data2")
read.csv2b("abc.csv")
read.csv2b("def.csv")
source2("pricing.R")
If you have a lot of stuff to read in this may be worthwhile otherwise why not supply the whole path to the actual functions? If this isn't what you're after it was still a fun learning experience for me :-)