How to access to specify file in subfolder without change working directory In R?

后端 未结 4 400
名媛妹妹
名媛妹妹 2021-01-31 05:03

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 /

4条回答
  •  借酒劲吻你
    2021-01-31 05:16

    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 :-)

提交回复
热议问题