[R] paste argument of a function as a file name
Martin Maechler
maechler at stat.math.ethz.ch
Wed Nov 23 10:26:16 CET 2005
>>>>> "UweL" == Uwe Ligges <ligges at statistik.uni-dortmund.de>
>>>>> on Thu, 10 Nov 2005 15:11:45 +0100 writes:
UweL> Luis Ridao Cruz wrote:
>> R-help,
>>
>> I have a function which is exporting the output to a file via
>> write.table(df, file = "file name.xls" )
>>
>> What I want is to paste the file name (above) by taking the argument to
>> the function as a file name
>>
>> something like this:
>>
>> MY.function<- function(df)
>> {
>> ...
>> ...
>> write.table(df,"argument.xls")
>> }
>> MY.function(argument)
UweL> Has been asked hundreds of times on this list.
indeed! ... and, it seems, has been answered 200 times with
hints that I would consider "suboptimal" ..
UweL> check the archives as the posting guide asks to do ...
UweL> Hint: paste()
well, we know that that works;
however, since at least R version 1.0.0, there's the function
file.path() which is a simple wrapper for paste() allowing a
slightly nicer syntax more readable and portable *) code.
So, we do recommend using file.path() as in
> list.files( file.path("C:", "WINDOWS", "system") )
or
> list.files( file.path("usr", "local", "lib"))
---
*) Though since nowadays the file separator "/" works on
all current versions of R (I think),
the trick of automatically using the correct file name separator
for the given platform (namely .Platform$file.sep )
seems less important now.
Martin Maechler, ETH Zurich
More information about the R-help
mailing list