简体   繁体   中英

How can I set a directory through a function?

When I start my PC, my current directory is:

C:\Users\kieken\Documents

However, my working directory for R should be:

C:\Users\kieken\Dropbox\science\R programming

For this case only, my data is stored in the following folder:

C:\Users\kieken\Dropbox\science\R programming\specdata

I have to to write a function that uses only the last part of this path:

pollutantmean("specdata", "sulfate", 43:44)

How do I use a variable "directory" in my function definition so that when I assign "specdata" to the variable "directory", everything works fine?

You can use file.path :

path <- "C:\\Users\\kieken\\Dropbox\\science\\R programming"
directory <- "specdata"

mypath <- file.path(path, directory, fsep = "\\")
# [1] "C:\\Users\\kieken\\Dropbox\\science\\R programming\\specdata"

Then you can use

setwd(mypath)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM