Suppose some environment variable (say MY_R_INC) has a number of paths. I want to source some file relative to some path in $MY_R_INC (just as #include in C++ does, which looks for header file in a number of directories). I RSiteSearch'ed, but I don't find any function that satisfies my need. Could somebody let me know if I overlooked something?
1. Etiquette on this list is to sign posts with your real name. 2. Please use R's Help facilities (beyond RSiteSearch()) first before posting: ?help.search help.search("environment variable") Bert Gunter Genentech Nonclinical Statistics -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of blue sky Sent: Friday, February 12, 2010 2:55 PM To: r-help at stat.math.ethz.ch Subject: [R] How to source files from a search path? Suppose some environment variable (say MY_R_INC) has a number of paths. I want to source some file relative to some path in $MY_R_INC (just as #include in C++ does, which looks for header file in a number of directories). I RSiteSearch'ed, but I don't find any function that satisfies my need. Could somebody let me know if I overlooked something? ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
On Fri, Feb 12, 2010 at 6:11 PM, Don MacQueen <macq at llnl.gov> wrote:> Well, you still will need Sys.getenv() to get the value of the environment > variable into R. > > Are you familiar with the function named list.files() ? > > This may do the job: > > ? source( list.files( Sys.getenv('SOMEENVVAR'), pattern='myfilename') ) > > But I haven't tested it.This doesn't do what I want. Anyway, I made my own program. Let me know if a function that does the same thing is available in some other packages. smart.source=function(partial_path) { source(full.path(partial_path)) } full.path=function(partial_path) { found_rpath=base:::Find( function(p) { full_path=base:::file.path(p, partial_path) base:::file.exists(full_path) } , base:::unlist(base:::strsplit(base:::Sys.getenv('RPATH'), ':')) ) if(is.null(found_rpath)) { stop(paste(partial_path, ' is not found in $RPATH', sep='')) } else { base:::file.path(found_rpath, partial_path) } } #shell environment variable RPATH=/dir1:/dir2 I have the following R file, which is in /dir2. $ cat main_test.R f=function() { print('in f') } Then I can source the above file like the following.> smart.source('smart.source/main_test.R') > f()[1] "in f"