[R] Open a file which name contains a tilde

John jwd @end|ng |rom @urewe@t@net
Wed Jun 12 21:50:12 CEST 2019

On Wed, 5 Jun 2019 18:07:15 +0200
Frank Schwidom <schwidom using gmx.net> wrote:

In reading file names, names with spaces require escaping of the
spaces, and you are using not only a tilde but the space as spell.  The
tilde is conventionally read as wild card representing the leading
portion of the file address, which is what you see here:

The long standing convention in unix and linux is that a tilde is
shorthand for your home directory, just as "./" is the local directory
and "../" is one step back up the directory tree. The output of
path.expand() varies in how your string is constructed:

1. > path.expand("ab")
   [1] "ab"
2. > path.expand("a b")
   [1] "a b"
3. > path.expand("a ~b")
   [1] "a ~b"
4. > path.expand("a ~ b")
   [1] "a /home/john b"
5. > path.expand("a ~/b")
   [1] "a /home/john/b"
6. > path.expand("~/a  b")
   [1] "/home/john/a  b"

Notice that the spaces have an effect on how the tilde is parsed in
the string.  The next to last case sees a string with three elements:
"a", the local path, and "b".  The last expands the tilde as the "path"
to a hypothetical file "b" in the home directory. In the sixth case the
same behaviour occurs.  

If you read the help for path.expand(), the routine expects
"leading" tildes, which explains the free floating "path in the fourth
example.  In the third example, where the forward slash is omitted, the
routine simply treats the tilde as part of a string. 

Also note this at http://www.linfo.org/file_name.html:

"...file names only use alphanumeric characters (mostly lower case),
underscores, hyphens and periods. Other characters, such as dollar
signs, percentage signs and brackets, have special meanings to the
shell and can be distracting to work with. File names should never
begin with a hyphen."

The probable "bug" is that none of the programmers imagined that anyone
would use special characters within file names, "legal" or not, (just
not done, don't y' know). In any case the simple solution, if you
really, really need a tilde in a filename, is to avoid setting it off in
spaces, or any other character that could mislead a routine into
treating it conventionally as shorthand for you home directory.  


More information about the R-help mailing list