如何在不更改R中的工作目录的情况下访问指定子文件夹中的文件?

如何在不更改R中的工作目录的情况下访问指定子文件夹中的文件?,r,csv,R,Csv,在R中,我想访问子文件夹中的某个文件。但我不想更改工作目录,然后再向后移动。它浪费了时间和时间 对于exmaple,我正在处理/home/phuong文件夹。 这是phuong的树结构 phuong-> data1, data2, data3. data1-> abc.csv, def.csv, script1.R data2-> bond.csv, option.csv, pricing.R data3->..... 所以我想在abc.csv、def.csv中加载数据

在R中,我想访问子文件夹中的某个文件。但我不想更改工作目录,然后再向后移动。它浪费了时间和时间

对于exmaple,我正在处理
/home/phuong
文件夹。 这是phuong的树结构

phuong-> data1, data2, data3.
data1-> abc.csv, def.csv, script1.R
data2-> bond.csv, option.csv, pricing.R
data3->.....
所以我想在abc.csv、def.csv中加载数据,并在pricing.R中运行代码

因此,如果使用code
setwd
,它会让我浪费很多时间,而且代码看起来很愚蠢,如下所示:

setwd("/home/phuong/data1" );

read.csv("abc.csv");
read.csv("def.csv");
setwd("/home/phuong/data2" );
source("pricing.R")
$ cd /home/phuong
$ ln -s ./data1/abc.csv data1_abc.csv
$ ln -s ./data1/def.csv data1_def.csv
$ ln -s ./data2/pricing.R pricing.R
setwd("~/repos/library/all_the_things")
library_files <- list.files()
sapply(library_files, source)
从一个文件夹移动到另一个文件夹时,我损失了很多时间,但都在同一个文件夹中
home/phuong/
。 因此,我需要一些方法来访问子文件夹中的任何文件,而无需
setwd
命令。
请帮助我,thks.

假设您的工作目录是
/home/hermie
,并且您希望从当前WD下的目录加载
.csv
文件(比如
/home/hermie/data
),您可以简单地执行以下操作:

setwd('/home/hermie')
myData <- read.csv('./data/myCsvFile.csv')
希望这有帮助


更新

不知怎的,我想你希望有人为你写解决方案。。。我提议:

> setwd('/home/phuong')
> data_abc <- read.csv('./data1/abc.csv')
> data_def <- read.csv('./data1/def.csv')
> source('./data2/pricing.R')
然后,从R:

> setwd('/home/phuong')
> data_abc <- read.csv('data_abc.csv')
> data_def <- read.csv('data_def.csv')
> source('pricing.R')
>setwd('/home/phuong')
>数据\u abc数据\u定义源('pricing.R')

如果我理解您的意图,您可以使用Hadley在中所称的闭包:

## Make a function that takes a path and another function
## and returns that same function with the path pre-progammed in
pathit <- function(FUN, path){
    function(file, ...){
        FUN(file=file.path(path, file), ...)
    }
}

## generate new functions that have the path pre-programmed in
read.csv2b <- pathit(read.csv, "home/phuong/data1")
source2 <- pathit(source, "home/phuong/data2")


read.csv2b("abc.csv")
read.csv2b("def.csv")
source2("pricing.R")
##生成一个函数,该函数采用一个路径和另一个函数
##并返回与中预先编程的路径相同的函数

pathit对我来说,学习浏览文件夹最直观的方法是使用
list.files(“..”
)。您将看到您需要如何从当前位置向上游或下游导航:)

我不知道这是否是您正在寻找的,但我在一个特定的github文件夹中有一个文件库,当我在一个新分支中初始化一个项目时,我在网上找到了这样一个解决方案:

setwd("/home/phuong/data1" );

read.csv("abc.csv");
read.csv("def.csv");
setwd("/home/phuong/data2" );
source("pricing.R")
$ cd /home/phuong
$ ln -s ./data1/abc.csv data1_abc.csv
$ ln -s ./data1/def.csv data1_def.csv
$ ln -s ./data2/pricing.R pricing.R
setwd("~/repos/library/all_the_things")
library_files <- list.files()
sapply(library_files, source)
setwd(“~/repos/library/all\u theu things”)
库文件我刚刚发现了这个包。我用它来做我正在做的一件事。与目前提到的其他选项相比,它提供了一种更干净的引用嵌套文件夹的方法。例如:

install.packages('here')
library('here')
here::i_am("pricing.R")
read.csv(here("data1/abc.csv"));
read.csv(here("data1/def.csv"));


我知道这种方法,但它会让我键入文件夹的完整路径,当我们在不同的时间加载某个文件夹中的许多文件时,这会使我的代码变得如此冗长和复杂。在文件系统中创建链接。你的R没有问题,你的文件夹结构有问题。。。这有两个解决方案:1。以你觉得舒服的方式重新组织你的文件,或者2。使用指向您的文件的链接(更多信息)@Barrabka,我不明白您的想法,这是关于这个问题我问的第一个问题。将路由写入您需要的每个数据文件有什么复杂之处?您不需要每次都更改WD来执行此操作,您只需编写一次即可!同样,如果您希望从一个位置访问所有必需的文件,请在当前WD中创建指向这些文件的符号链接,然后只需读取符号链接(就像直接读取文件一样)(我估计您正在linux环境中工作)。为什么每次都需要更改目录?没有必要。我更喜欢这个解决方案,而不是公认的答案。它就像一个符咒!虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。