Bug 14507 - file.copy does not understand "~"
Summary: file.copy does not understand "~"
Alias: None
Product: R
Classification: Unclassified
Component: Low-level (show other bugs)
Version: R 2.12.1
Hardware: All Linux
: P1 major
Assignee: R-core
Depends on:
Reported: 2011-02-21 10:50 UTC by Mario Frasca
Modified: 2014-02-16 11:42 UTC (History)
2 users (show)

See Also:


Note You need to log in before you can comment on or make changes to this bug.
Description Mario Frasca 2011-02-21 10:50:39 UTC
when I use file.copy, the from and to arguments cannot start with the character "~".  I have to use the value of Sys.getenv("HOME").  other functions (for example list.files or setwd) do understand "~".

tested on ubuntu 10.10 (i686) with R2.12.0 and Debian wheezy (powerpc) with R2.12.1.

- go to your HOME dir
cd ~

- create this directory structure 
mkdir -p file.copy.test/run 
mkdir file.copy.test/sandbox
mkdir -p file.copy.test/tests/H_gewogen_All_NA/input 
mkdir file.copy.test/tests/H_gewogen_All_NA/output 
mkdir file.copy.test/tests/H_gewogen_All_NA/config 
mkdir file.copy.test/tests/H_gewogen_All_NA/logs 
- and populate it with a few files
touch file.copy.test/tests/H_gewogen_All_NA/input/volBepaling.xml
touch file.copy.test/tests/H_gewogen_All_NA/output/--expected--volBepaling.xml
touch file.copy.test/tests/H_gewogen_All_NA/config/volBepaling.conf

- start R and observe the behaviour
> file.copy(from=c("~/file.copy.test/tests/H_gewogen_All_NA/input/", "file.copy.test/tests/H_gewogen_All_NA/output/"), to="file.copy.test/sandbox/", recursive=TRUE)
- the result informs me that the first directory is not copied correctly.

the ~ is also not understood if used in the `to` argument.

define a local file.copy function, for example like this:
file.copy <- function(from, to, overwrite = recursive, recursive = FALSE) {
  from[substr(from,1,1) == "~"] <- paste(Sys.getenv("HOME"), substring(from, 2)[substr(from,1,1) == "~"], sep='')
  to[substr(to,1,1) == "~"] <- paste(Sys.getenv("HOME"), substring(to, 2)[substr(to,1,1) == "~"], sep='')
  base::file.copy(from, to, overwrite, recursive)
Comment 1 Simon Urbanek 2011-02-21 17:46:28 UTC
Your report is quite confusing, but what you probably meant to say is that file.copy() is documented to expand filenames but it does so only for files, not for recursive directory copies (do_filecopy calls do_copy for directories which doesn't expand).

FWIW a real work-around is to call path.expand() -- as referenced on the help page.
Comment 2 Brian Ripley 2011-02-22 16:04:11 UTC
Please do read the help page: it does not say that ~ is interpreted by those functions (whereas it does on many others). Not doing what you don't say you do is not a bug -- reporting it as a bug, however, is a bug.

I'll make this an enhancement request.
Comment 3 Brian Ripley 2011-02-22 16:50:20 UTC
Sorry, I think I see what you are referring to.  It does not mention ~.
It does mention path expansion.  Please be clearer!
Comment 4 Mario Frasca 2011-02-22 18:23:44 UTC
thanks for the hints and for fixing this that I saw as inconsistent behaviour, but what was the solution?
Comment 5 Simon Urbanek 2011-02-22 18:41:05 UTC
As I said, "to" and "from" had to be expanded in case "to" is a directory.
Comment 6 Mario Frasca 2011-02-22 19:12:37 UTC
all right, my question "what was the solution?" was not explicit enough.

when I work at fixing a bug report I usually commit the changes adding to the commit message a reference to the bug report and then I go back to the bug report and write there a reference to the change-set that addresses (and hopefully fixes) the bug.  

sometimes I aggregate a set of bugs and put them in the TODO list for a specific release/milestone.

I am interested in the change set, that is: to have a look at this part of the sources for R.

I also would like to know in which release of R this correction/enhancement will be available.

Comment 7 Simon Urbanek 2011-02-22 19:31:35 UTC
In that case you should know better than abusing bug reporting system for unrelated user questions. Please consider reading R documentation (especially FAQ 9.2) it has answers to your questions. If you want to ask a question please use the corresponding mailing lists. Thanks.
Comment 8 Mario Frasca 2011-02-22 19:55:05 UTC
I do not have any generic question: I had a concrete bug report and I am interested in seeing how it was solved.
anyway, since it has been corrected/addresses, I will sooner or later install an R version containing the correction.
Comment 9 Jackie Rosen 2014-02-16 11:42:40 UTC
(spam comment removed)