read.csv("my-very-important-data-file-somewhere.csv")
Warning in file(file, "rt"): cannot open file
'my-very-important-data-file-somewhere.csv': No such file or directory
Error in file(file, "rt"): cannot open the connection
Before we start with Quarto, we need to make sure that you understand file storage hygiene.
We can prevent unexpected problems if we can maintain an order to your files, paths, and directories. A common problem that arises is R not knowing where a certain file is. For example, we get the error:
read.csv("my-very-important-data-file-somewhere.csv")
Warning in file(file, "rt"): cannot open file
'my-very-important-data-file-somewhere.csv': No such file or directory
Error in file(file, "rt"): cannot open the connection
Because R doesn’t know where "my-very-important-data-file-somewhere.csv"
is.
Practicing good file storage hygiene will help maintain an order to files, paths, and directories. This will make you more productive in the future, because you’ll spend less time fighting against file paths.
Not sure what a file path is? We explain that as well.
::: {.callout-note title = “Your Turn”}
In groups of 2-4 discuss:
:::
This section is heavily influenced by Jenny Bryan’s great blog post on project based workflows.
Sometimes this is the first line of an R Script or R markdown file.
setwd("c:/really/long/file/path/to/this/directory")
What do you think the setwd
code does?
This says, “set my working directory to this specific working directory”.
It means that you can read in data and other things like this:
<- read_csv("data/mydata.csv") data
Instead of
<- read_csv("c:/really/long/file/path/to/this/directory/data/mydata.csv") data
So while this has the effect of making the file paths work in your file, it is a problem. It is a problem because, among other things, using setwd()
like this:
So, to get this to work, you need to hand edit the file path to your machine.
This is painful.
When you do this all the time, it gets old, fast.
This might all be a bit confusing if you don’t know what a file path is. A file path is the machine-readable directions to where files on your computer live. So, the file path:
/Users/njtierney/Desktop/qmd4sci-materials/demo.R
Describes the location of the file “demo.R”. This could be visualised as:
users
└── njtierney
└── Desktop
└── qmd4sci-materials
└── demo.R << THIS IS THE FILE HERE
└── exercises
└── exploratory-data-analysis
└── eda-document.qmd
└── eda-script.R
└── data
└── gapminder.csv
So, if you want to read in the gapminder.csv
file, you might need to write code like this:
<- read_csv("/Users/njtierney/Desktop/qmd4sci-materials/data/gapminder.csv") gapminder
As we now know, this is a problem, because this is not portable code. It is unlikely someone else will have the gapminder.csv
data stored under the folders, "Users/njtierney/Desktop"
.
If you have an RStudio project file inside the qmd4sci-materials
folder, you can instead write the following:
<- read_csv("data/gapminder.csv") gapminder
(1-2 minutes) Imagine you see the following directory path: "/Users/miles/etc1010/week1/data/health.csv"
what are the folders above the file, health.csv
?
What would be the result of using the following code in demo-gapminder.qmd
, and then using the code, and then moving this to another location, say inside your C drive?
setwd("Downloads/etc1010/week1/week1.qmd)
This file path situation is a real pain. Is there an answer to the madness?
The answer is yes!
I highly recommend when you start on a new idea, new research project, paper. Anything that is new. It should start its life as an rstudio project.
An rstudio project helps keep related work together in the same place. Amongst other things, they:
This helps keep you sane, because:
And finally, the big one:
RStudio projects help resolve file path problems, because they automatically set the working directory to the location of the rstudio project.
Let’s open one together.
::use_course("njtierney/qmd4sci-materials") usethis
Follow the prompts to download this to your desktop and then run the rstudio project. (You can move it later if you like!)
You are now in an rstudio project!
demo.R
file
demo.R
fileread_csv
code work?exploratory-data-analysis
folder - eda-script.R
.read_csv
code work?exploratory-data-analysis
folder - eda-document.qmd
, by clicking the “render” button (we’ll go into this in more detail soon!)Although RStudio projects help resolve file path problems, in some cases you might have many folders in your r project. To help navigate them appropriately, you can use the here
package to provide the full path directory, in a compact way.
::here("data") here
returns
[1] "/Users/nick/github/njtierney/qmd4sci-materials/data"
And
::here("data", "gapminder.csv") here
returns
[1] "/Users/nick/github/njtierney/qmd4sci-materials/data/gapminder.csv"
(Note that these absolute file paths will indeed be different on my computer compared to yours - super neat!)
You can read the above here
code as:
In the folder
data
, there is a file calledgapminder.csv
, can you please give me the full path to that file?
This is really handy for a few reasons:
If the first line of your R script is
setwd("C:\Users\jenny\path\that\only\I\have")
I will come into your office and SET YOUR COMPUTER ON FIRE 🔥.
– Jenny Bryan
::: {.callout-caution title=“Aside: Creating an RStudio project” collapse = “true”}
You can create an Rstudio project by going to:
file > new project > new directory > new project > name your project > create project.
You can also click on the create project button in the top left corner
Then go to new directory, if it is a new folder - otherwise if you have an existing folder you have - click on existing directory.
Then go to new project
Then write the name of your project. I think it is usually worthwhile spending a bit of time thinking of a name for your project. Even if it is only a few minutes, it can make a difference. You want to think about:
For example, I had a project looking at bat calls, so I called it screech
, because bats make a screech-y noise. But maybe you’re doing some global health analysis so you call it “world-health”.
And click “create project”.
:::
In this lesson we’ve:
here
package