Moving down a folder in working directory

10,570

The symbol ~ does not do what you seem to think it does. It does not mean "the current directory". ~ refers to your home directory.

The correct symbol to use for current directory is a period .

So, what you want is

setwd("./subfolder")

Current operating systems typically assume that if a full path is not provided, then a relative path (i.e. relative to the current directory) will be used by default. Therefore it is also possible to simply use

setwd("subfolder")

A summary of symbols used in paths

  • . = current directory
  • .. = parent of current directory
  • ~ = home directory (see explanatory note below on home directory)
  • / as 1st character = root directory E.g. setwd("/folder")
  • / within the path = separator between directories in path. E.g. setwd("/folder/subfolder")
  • \ = In Windows and DOS operating systems only, backslash \ is equivalent to /. If you use this format in R, you will need to use double backslash \\ to 'escape' this. E.g., setwd("C:\\folder\\subfolder"). However, to maintain compatibility between platforms, it is recommended to stick to using a forward-slash / even on windows systems, as this will be converted to the correct path by R.
  • Any path that does not begin with one of the above characters is interpreted as being relative to the current directory.

Explanatory note on 'home' directory

In Unix-derived and Unix-like operating systems (such as Linux, OsX, BSD) the meaning of home directory referred to by ~ is straightforward. The meaning of ~ is defined by the operating system. Depending on the OS, it is usually /home/<username> (in Linux and BSD), /Users/<username> (in OS X) or a similar platform-dependent variant. See here for a list of definitions for various operating systems.

But in Windows things are slightly different, because the OS does not recognize "~" as a valid path. The R for Windows FAQ explains how expand.path interprets the home directory on Windows computers thusly,

The home directory is set as follows: If environment variable R_USER is set, its value is used. Otherwise if environment variable HOME is set, its value is used. After those two user-controllable settings, R tries to find system-defined home directories. It first tries to use the Windows "personal" directory (typically C:\Users\username\Documents). If that fails, if both environment variables HOMEDRIVE and HOMEPATH are set (and they normally are), the value is ${HOMEDRIVE}${HOMEPATH}. If all of these fail, the current working directory is used.

In my experience, on Windows R most often interprets "~" as "C:\Users\username\Documents". You can find the values of the environment variables with the following commands

Sys.getenv("R_USER")
Sys.getenv("HOME")
Sys.getenv("HOMEDRIVE")
Sys.getenv("HOMEPATH")

And, you can find out what path "~" is interpreted as by using the command

normalizePath("~")
Share:
10,570
socialscientist
Author by

socialscientist

Updated on June 19, 2022

Comments

  • socialscientist
    socialscientist almost 2 years

    I am trying to move down a folder in the working directly in R. For example, I have a working directory of foo/bar and I want to move to foo/bar/subfolder:

    setwd("/Users/foo/bar")
    getwd()
    [1] "/Users/foo/bar"
    setwd("~/subfolder")
    

    I then receive:

    Error in setwd("~/subfolder"): cannot change working directory

    What am I doing wrong?