rwindowspathsetwd

How to set path in R in windows operating system using r'path\to\directory'?


In windows operating system (OS), for python, I can use os.chdir(r"path\to\directory\"). Is there any way that I can set the path in R also in similar way?.

One of the way known to me is setwd(readClipboard()). (Taking path in clipboard, Ctrl+C).

The purpose of the question is that, I need to everytime change the symbol \ to / to set the path in Windows OS.


Solution

  • R uses raw string syntax similar to C++,

    r"(path\to\directory)"
    

    or

    setwd(R"(path\to\directory)")
    

    with some further examples from ?Quotes:

    r"(c:\Program files\R)"
    
    ## More raw strings:
    r"{(\1\2)}"
    r"(use both "double" and 'single' quotes)"
    r"---(\1--)-)---"