1

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 '\'
anderwyang
  • 1,801
  • 4
  • 18
  • 1
    Use forward-slashes; even on windows, `setwd('C:/Users/Adminstrator/Desktop/myfolder')` works. (BTW, running as `Administrator`? Seems like a really bad idea.) – r2evans Nov 10 '21 at 03:25
  • I gather you're also aware of tab-completion, where you can hit tab and it will autocomplete the working directory with slashes etc included if you hit the first couple of letters. – thelatemail Nov 10 '21 at 03:27

1 Answers1

2

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())
U13-Forward
  • 69,221
  • 14
  • 89
  • 114