1

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.

  • 1
    You can do this, but it is generally regarded as bad practice to do so because it is easy to forget to restore the original working directory at the end of your script. If this happens, you script has not left the environment in its original state. This means that subsequent scripts may not work as expected and debugging the issue will be difficult. – Limey Aug 04 '22 at 05:55
  • @Limey . Totally agree to your comment –  Aug 04 '22 at 06:13

1 Answers1

0

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--)-)---"
Donald Seinen
  • 4,179
  • 5
  • 15
  • 40