用户独立数据导入

时间:2015-11-27 13:12:27

标签: r data-import

假设X和Y正在使用project来处理文件data.csv。他们在名为main.R的云服务中有一些公共文件。现在假设在main.R内,X和Y分别通过

导入数据
# uncomment first line if you are X, otherwise uncomment second line
# data <- read.csv("C:/User/X/Documents/cloud/project/data.csv")
# data <- read.csv("C:/User/Y/Desktop/cloud/project/data.csv")

取而代之的是根据运行脚本的人取消注释其中一行,我希望总共有一个命令是通用的,并且引用它们共有的文件路径部分,比如说,

data <- read.csv(".../cloud/project/data.csv")

知道怎么做到这一点吗?

1 个答案:

答案 0 :(得分:1)

试试这个:

#check if directory exists
dataDir <-
  if(dir.exists(paste0("C:/Users/",Sys.info()["effective_user"], "/Documents/cloud/project/"))){
    paste0("C:/Users/", Sys.info()["effective_user"],"/Documents/cloud/project/")
  } else if(dir.exists(paste0("C:/Users/", Sys.info()["effective_user"],"/Desktop/cloud/project/"))) {
    paste0("C:/Users/", Sys.info()["effective_user"],"/Desktop/cloud/project/")
  }


#if exists then read in
if(!is.null(dataDir)){ read.csv(paste0(dataDir,"data.csv")) }