read excel file from R Project

Hi everyone,

I am new to using Rproj, and I am stuck at reading my raw data saved in a subfolder of R project to the R script.

My peculiar experience is that when I click on the file in the Rproj. environment, it gives me an option to open the file with this code:

X4_foodbalances_usa_fao_1961_2013_01272023 <- read_excel("00_raw_data/4-foodbalances-usa-fao-1961-2013-01272023.xlsx")

But when I copy and paste this same command and try to load the data, I get this error message:

Error: path does not exist: ‘00_raw_data/4-foodbalances-usa-fao-1961-2013-01272023.xlsx’

RStudio info:
Edition: Desktop- Mac laptop
Version: Version 2022.12.0+353 (2022.12.0+353)
OS version: macOS Monterey version 12.4

  • R Version: 4.1.2 (2021-11-01)

The error message you're seeing suggests that R is unable to find the file "00_raw_data/4-foodbalances-usa-fao-1961-2013-01272023.xlsx" at the location where it's looking. One possible reason for this is that the working directory of your R script is different from the location of your R project. The working directory is the location where R looks for files by default, and it can be set using the setwd() function or by navigating to the desired location in the RStudio file pane.

You can check your working directory by running getwd() in your R script. To change it, you can use setwd("path/to/directory") where you replace the path/to/directory with the directory where your data file is located.

Another possible reason is that the file is not in the right location, check if the file is in the right folder,

Also, you can try to use the full path of your file instead of the relative path.

If the above suggestions did not help, please let me know the exact file location in your computer, so that I can help you with the solution.

1 Like

Hi Talha,

Thank you so much for your reply. I checked the working directory of R: it was directed to the R.Project's folder. About full vs relative path, my intention is to use the relative path. So that this project can be portable to others.

I did not understand what you mean by not having the file in the right location. I have created multiple folders inside the RProject, one for raw data, one for tidy data, one for R code, etc.

I am trying to read my raw data from its corresponding folder, 00_raw_data.

Can you explain more what did you mean?

I appreciate your help.

You could try to verify the correct path with this:

# select the file and in the console show the correct path.
file.choose()
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.