I am learning function called file.path() in R. I am wondering whether this command will change the working directory just like setwd() or simply give R the path of the file and change the workspace only? Thanks in advance.
file.path() is just a convenient way of making a file path (it won't actually do any navigation at all)
e.g. if I want "C:\Users\John\Documents"
file.path("C:", "Users", "John", "Documents", fsep="/")
You could then pass that to setwd() like so
path <- file.path("C:", "Users", "John", "Documents", fsep="\\")
setwd(path)
file.path() is used to create reproducible code that can be run on other operating systems.
i.e. mac and windows use different directory separators
Therefore, reducing the headache for the person you give your script to with a different OS
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With