Search code examples
rsetwd

In R, how to change working directory more easily?


In how to change working directory more easily? Currently, if we use 'setwd',we have to add many '\', sometimes it's boring Is there any easier way for this ? (Just like Python can add 'r' )

setwd('C:\Users\Administrator\Desktop\myfolder') # can't work
setwd('C:\\Users\\Administrator\\Desktop\\myfolder') # can work,but havt to add many '\'

Solution

  • You could use r (for raw string) and add parenthesis:

    > r"(C:\Users\Administrator\Desktop\myfolder)"
    [1] "C:\\Users\\Administrator\\Desktop\\myfolder"
    > 
    

    And now:

    setwd(r"(C:\Users\Administrator\Desktop\myfolder)")
    

    Or reading from clipboard automatically adds the extra slashes:

    setwd(readClipboard())