简体   繁体   English

如何通过函数设置目录?

[英]How can I set a directory through a function?

When I start my PC, my current directory is: 启动PC时,当前目录为:

C:\Users\kieken\Documents

However, my working directory for R should be: 但是,我的R工作目录应该是:

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? 如何在函数定义中使用变量“目录”,以便在将“ specdata”分配给变量“目录”时,一切正常?

You can use file.path : 您可以使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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