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

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

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest