简体   繁体   English

R 文件引用在 readxl::read_excel 的上下文中不起作用 function

[英]R file referencing not working in the context of readxl::read_excel function

What I am trying to do is go up three folders to the "New Model Setup" Folder, within that folder I am attempting to go down into the "Data Inputs" Folder and access my excel file.我想做的是 go 向上三个文件夹到“New Model Setup”文件夹,在该文件夹中我试图 go 向下进入“Data Inputs”文件夹并访问我的 excel 文件。

The file of interest is in the Folder "C:/Users/Model Dev/New Model Setup/Data Inputs" I have set my working directory to...感兴趣的文件位于文件夹“C:/Users/Model Dev/New Model Setup/Data Inputs”中,我已将工作目录设置为...

getwd()

[1] "C:/Users/Model Dev/New Model Setup/Fiscal_Year/Quarter/Run" [1] "C:/Users/Model Dev/New Model Setup/Fiscal_Year/Quarter/Run"

I can see this files location using two different methods of list.files()我可以使用 list.files() 的两种不同方法查看此文件位置

list.files('..\\..\\..\\Data Inputs\\')

[1] "Archive" [1] “存档”
[2] "Data Inputs Master File.xlsx" [2] 《数据输入主文件.xlsx》

list.files('../../../Data Inputs/')

[1] "Archive" [1] “存档”
[2] "Data Inputs Master File.xlsx" [2] 《数据输入主文件.xlsx》

However when I use the same structure in the read_excel function I get the Error path does not exist.但是,当我在 read_excel function 中使用相同的结构时,我得到错误path不存在。

crop_yield <- readxl::read_excel('..\\..\\..\\Data Inputs\\Data Inputs Master File.xlsx',
                                  sheet = "CropYields")

Error: path does not exist: '..\..\..\Data Inputs\Data Inputs Master File.xlsx'错误: path不存在:'..\..\..\Data Inputs\Data Inputs Master File.xlsx'

crop_yield <- readxl::read_excel('../../../Data Inputs/Data Inputs Master File.xlsx',
                                 sheet = "CropYields")

Error: path does not exist: '../../../Data Inputs/Data Inputs Master File.xlsx'错误: path不存在:'../../../Data Inputs/Data Inputs Master File.xlsx'

This is my workaround, but it isn't the route I wanted to go, I would prefer to have all of this in the function.这是我的解决方法,但这不是我想要的 go 路线,我更愿意将所有这些都放在 function 中。

mainDir <- getwd()
setwd('../../../Data Inputs')
crop_yield <- readxl::read_excel('Data Inputs Master File.xlsx',sheet = "CropYields")
setwd(mainDir)

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

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