file.path function in R

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.

1

2 Answers

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)
0

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like