STATMATT
STATMATT

Reputation: 17

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.

The file of interest is in the Folder "C:/Users/Model Dev/New Model Setup/Data Inputs" I have set my working directory to...

getwd()

[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('..\\..\\..\\Data Inputs\\')

[1] "Archive"
[2] "Data Inputs Master File.xlsx"

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

[1] "Archive"
[2] "Data Inputs Master File.xlsx"

However when I use the same structure in the read_excel function I get the Error path does not exist.

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’

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’

Upvotes: 0

Views: 28

Answers (1)

STATMATT
STATMATT

Reputation: 17

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.

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

Upvotes: 0

Related Questions