Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R将工作目录设置为文件夹_R - Fatal编程技术网

R将工作目录设置为文件夹

R将工作目录设置为文件夹,r,R,我正在尝试将工作目录设置为函数中的其他子文件夹。我希望打印命令可以打印 C:/Users/Blah/Desktop/dir2/SUBFOLDER 而是打印 C:/Users/Blah/Desktop/dir2 然而,当我在控制台中运行dirs时,我得到: C:/Users/Blah/Desktop/dir2/SUBFOLDER ...(Much longer list) 如我所料。这是我的密码: temp<-function(path) { print(path) #outp

我正在尝试将工作目录设置为函数中的其他子文件夹。我希望打印命令可以打印

C:/Users/Blah/Desktop/dir2/SUBFOLDER 
而是打印

C:/Users/Blah/Desktop/dir2
然而,当我在控制台中运行dirs时,我得到:

C:/Users/Blah/Desktop/dir2/SUBFOLDER 
...(Much longer list)
如我所料。这是我的密码:

temp<-function(path)
{
  print(path) #output is C:/Users/Blah/Desktop/dir2
  setwd(path)
  print(getwd())
  xml=xmlParse("filename.xml")
  ...
}

dirs<-list.dirs("C:/Users/Blah/Desktop/dir2")
lapply(dirs,temp)#apply function tempt to every item in dirs

你的问题很难理解

默认情况下,list.dirs将返回相对于当前工作目录的路径

如果更改工作目录,则相对路径将无效

您可以尝试在list.dirs中使用full.names=TRUE,让temp函数将工作目录返回到其原始状态

temp <- function(path) {
           owd <- getwd()
           on.exit(setwd(owd))
           print(path) 
           setwd(path)
         print(getwd())
    }

是否检查了list.dirs的可选参数

文档中说,默认情况下,答案包括路径本身,因此函数temp将首先应用于您提供给list.dirs,C:/Users/Blah/Desktop/dir2的目录。
您可能想尝试使用list.dirsC:/Users/Blah/Desktop/dir2,recursive=FALSE,如果您想要的内容没有问题

我想第一个目录就是您指定的根目录。关于lapplydirs[-1],temp呢?我不知道你到底看到了什么输出,但是运行dir后向量的第一个元素在我的最终代码中,我按照你的建议做了,而不是更改setwd,但是@davidrd的解决方案是我需要的。谢谢
files <- list.files(pattern = '\\.xml$', recurvise = TRUE)
XML <-   lapply(files, xmlParse)