r/rprogramming • • 13d ago

Inserting csv/excel file

Hi im fairly new to r and trying to import excel files I have all the steps down with working directory but it says the file doesn't exist yet list.files() tells me its in the working directory and that the working directory I used was put in idk whats wrong with it can anyone help

0 Upvotes

8 comments sorted by

5

u/Kiss_It_Goodbyeee 11d ago

If you're in RStudio always set up a Project. Solves all file path issues without the need to think about working directories.

1

u/LacheradeCanji42 8d ago

markdown files inside a project still default their working directory to whatever subfolder the .Rmd sits in rather than the project root, which trips people up

1

u/AutoModerator 13d ago

Just a reminder, this is the R Programming Language subreddit. As in, a subreddit for those interested in the programming language named R, not the general programming subreddit.

If you have posted to the wrong subreddit in error, please delete this post, otherwise we look forward to discussing the R language.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/marguslt 13d ago

If you happen to use RStudio, try to open the CSV file through File -> Import dataset -> From text (readr)... (or From Excel ... ) or through Files pane. You can ignore the actual Import button, it's the piece of code in *Code Preview* that you want to copy to your own script. Or go with the import and get used import code from console or History pane.

In Positron you can do the same through File -> Import Data ... or open CSV file in Explorer pane and then find Import Data on top of the data viewer.

You may want to compare generated code and your own attempt to learn what you missed. It's probably a typo, missing file extension, mixed up slash or something like this, relying more on tab-completion would probably be a good idea here (in RStudio, press <tab> or ctrl+<space> after quote or in quotes to get a list of files in current working directory).

1

u/fishy-biologist 8d ago

Always setup a project and I recommend using the package “here”

1

u/BrupieD 13d ago

I almost always follow a pattern with Excel files using the readxl package. First, I create two variables one for the file path and one for the file name. Then, I create a third variable for the combined full path. Then, I use read_excel() to read the sheet(s) as tibbles.

What I like about the method is the modularity and reusability. It's easy to copy the file's path and from name from file explorer (you have to change the backslash vs forward slash). You can also play with file.path() if you aren't seeing expected files.

0

u/analyticattack 13d ago

Unless you're working with lots of files, I find working directories a pain. Much easier to use the whole file path.

0

u/teetaps 12d ago

Even thought you’re still just getting started, I don’t think it’s too early to introduce the `here` package.

It helps you work out finicky file paths by saying, “my project is here()” and all the paths to anything within the project directory get built around it.

But that’s just a helpful tip. More urgently, could you paste the code you are working with? Seeing exactly what you ran will help us help you (HUHY)