R: Read in data error "no such file/dir": solution setwd()


Okay so let's talk about an error that you might might encounter if you are trying to read in some type of file or data set that you have on your computer but it's giving you this. Error cannot open file assets. Whatever the file name is no such file or directory. That's really the the big point of this error. No such file or directory and so it may be confusing because you look at it and you say okay here's my file structure and I know that it's in assets and that the file is called this and I. I spelled it correctly. Everything should be working. But if you're still getting this error one of the problems that you might be running into is that your working. Directory is not pointed to the right location so what. I mean by that. Is that this terminal down here. This console isn't looking in the right place. This is kind of where everything is executed. When you run it up here. It tries to run down here and if this console if it's not looking at the right place within your computer then it's not going to be able to find your file so for example if you run this command get a wd open and closed parenthesis it says users that's my current working directory so if I back up here I'm not even on here here it is users so it's sitting right here and it's looking throughout this closest file structure from users and it's trying to match assets sample text file and that's not actually where it is in relation to users in relation to users it is you have to drill down a little bit further minds and documents over here. It's an art foundation so it's it's a little distant from where your file actually is so the way to fix that is that you're going to use this function called set working directory and if I run it with nothing in it then it's not going to do anything because you have to pass it the argument of where you want your directory to be looking where you want this console to be looking so in order to learn how to do that. Let's go ahead and use the terminal. Let's bring it into window here so using this terminal.

We're going to figure out what argument to pass set working directory so that the console is looking in the right place for this file right here so right off the bat. I ran a command CD dot dot that means. I'm going to go back into directory and if we run LS that means the list lists all of the files and folders of wherever you're looking right now so I'm looking in home right now. If we pull this up this is home right so this is really this finder. Folder is really just the graphical representation of this so we are going to dive a little bit deeper and. I know that my project is within documents so I'm gonna say it's going to documents LS again and I know that my project is within our projects so CD our projects. So now we're here. I'm going to do LS again and I know mines in our foundations so I'm gonna go there but I forgot the s on the end of that so now I know that I'm in the right place and if we want to get the current working directory of where we are right here you use PWD and that will give you the directory and so what you need to do you come in here copy this and say set working directory you gotta put it in quotes so you set that and then we'll run that and now we don't have an error and we know that my txt data was correctly found so I hope that makes sense a possible alternative way that you can do that is if you just use this finder folder. You might be more familiar with this and go down here and you will say okay our foundations and let's say we want to get info and it might have the directory in here names and extensions no okay so it doesn't look like this. Might this might not be the best way to do it. It doesn't have the information that we're looking for maybe if you click on one of these files and what. I'm looking for is that directory that we found in the terminal but it's not like looking like we found it here so the terminal right now is probably the best option to do that. And yeah so good luck with that and I hope this helps solve some problems.