[R] Create directory and copy files in R

Yihui Xie xieyihui at gmail.com
Tue Sep 22 18:43:15 CEST 2009


You assigned values to 'fn' and 'dpath' outside the function body and
use these names as the arguments of your function, so actually 'cyfun'
does not know what 'fn' and 'path' are. The solution is to put them in
the arguments as default values:

cyfun <- function(fn = "C:/Documents and
Settings/lma/Desktop/FamilyAEntrepreneurs/Entrepreneurs/Juha/book_log-20041210T095019.txt",
    dpath = "C:/Documents and Settings/lma/My Documents/Juha/book") {
    dir <- dirname(fn)
    fn <- basename(fn)
    deskdir <- dir.create(dpath)
    file.copy(fn, deskfile)
}

But even if you have fixed this problem, I don't think you can
successfully copy all the files under 'dir', as you've only copied a
single file 'fn' to 'deskdir' (and as 'fn' is only a base name,
probably this function will fail again if R does not know where is
'fn'!). To get the file list under 'dir', you may need
list.files(dirname(fn), all.files = TRUE); then copy this file list to
'deskdir'.

BTW, it's not a good programming habit to assign values to the names
of internal R functions/objects, as you did in 'dir <- dirname(fn)'
because 'dir' is already an R function.

> library(fortunes)
> fortune('dog')

Firstly, don't call your matrix 'matrix'. Would you call your dog
'dog'? Anyway, it might clash
with the function 'matrix'.
   -- Barry Rowlingson
      R-help (October 2004)

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: 515-294-6609 Web: http://yihui.name
Department of Statistics, Iowa State University
3211 Snedecor Hall, Ames, IA



On Mon, Sep 21, 2009 at 9:24 AM, Don MacQueen <macq at llnl.gov> wrote:
> In your file.copy() command you have "deskfile", but "deskfile" does not
> appear anywhere else. Perhaps you meant deskdir?
>
> After the error occurs, type
>
>  traceback()
>
> and try to discern exactly where things went wrong.
>
> -Don
>
> At 5:05 PM +0300 9/21/09, Tammy Ma wrote:
>>
>> Content-Type: text/plain
>> Content-Disposition: inline
>> Content-length: 792
>>
>>
>>
>> HI, All R users,
>>
>> My problem is:
>>
>>>  fn
>>
>> [1] "C:/Documents and
>> Settings/lma/Desktop/FamilyAEntrepreneurs/Entrepreneurs/Juha/book_log-20041210T095019.txt"
>>>
>>>  dpath
>>
>> [1] "C:/Documents and Settings/lma/My Documents/Juha/book"
>>
>>
>> I want to make  a function "cyfun" to copy all files in "dir" to "deskdir"
>> but I always got the following problem:
>>
>> Error in file.exists(to) : invalid 'file' argument.
>> Whats the problem??
>>
>>
>>
>>
>> cyfun<-function(fn,dpath){
>> dir<-dirname(fn)
>> fn<-basename(fn)
>>
>> deskdir<-dir.create(dpath)
>> file.copy(fn, deskfile)
>>
>> }
>>
>> Thanks!
>>
>> Tammy
>>
>> _________________________________________________________________
>> Drag n’ drop—Get easy photo sharing with Windows Live™ Photos.
>>
>> http://*www.*microsoft.com/windows/windowslive/products/photos.aspx
>>        [[alternative HTML version deleted]]
>>
>>
>> ______________________________________________
>> 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.
>
>
> --
> --------------------------------------
> Don MacQueen
> Environmental Protection Department
> Lawrence Livermore National Laboratory
> Livermore, CA, USA
> 925-423-1062
>
> ______________________________________________
> 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.
>




More information about the R-help mailing list