Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file.path function in R

Tags:

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.

like image 801
Jason Lin Avatar asked Jun 25 '26 11:06

Jason Lin


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)
like image 189
stevec Avatar answered Jun 27 '26 01:06

stevec


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

like image 43
adm Avatar answered Jun 27 '26 00:06

adm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!