Chapter 11 How R reads files
We introduced the file system and file system tree above in Section 2. This section discusses how to access and load files with R. We look at data–csv files–a little later, here we just display images. But exactly as data, images are also stored in files, and so we need to understand the paths to access those.
TBD: do we do images here?
11.1 R’s working directory
Remember, working directory is the folder where each app, such as R, “thinks” it is located in the file system tree (see Section 2.3). You can find the name of the working directory as
## [1] "/home/siim/tyyq/info201-book"
Exercise 11.1 Is the example here, “/home/siim/tyyq/info201-book”, relative path or absolute path? (See Sections 2.3 and 2.4.)
See the solution
Note that each instance of R has its own working directory. This
means when you run multiple R’s in different windows
at the same time, all of these
may have different working directories. If the working directory in
your RStudio console is /Users/yucun/Desktop
, then that does not
mean that another window that is compiling your homework, will have
the same working directory!
Exercise 11.2 What is the working directory of your R console inside your RStudio?
See the solution
You can change the working directory using setwd()
. However, this
is rarely needed, as you should start R in the right folder to begin
with, and access files using their path.
11.2 Accessing files in R

For a refresher: this is how Yucun’s info201 content looks like. See Section 2.1.1.
You can see the files in the current working directory using
list.files()
. For instance, if Yucun will run R inside of his
info201 directory, he would see:
This is the R’s “view” of the same folder. Instead of icons, we see
file names. We stress here that R shows the file names in their
complete form–including the complete extension (like .pdf
) and all
eventual spaces and other symbols in the names. The graphical viewers
may or may not display the complete names, depending on their
configuration. The files may also be displayed in a different order,
in the example here, the graphical viewer puts all directories first,
but R orders everything alphabetically.
Exercise 11.3
- Find the working directory of your current R instance (R Console inside RStudio).
- Print all file names there using
list.files()
. - Open the same folder using your graphical file manager.
- Show that it contains the same files what you saw in R!
- Does your graphical file manager show the file names in the same form, or do you see simplified versions of the names?
See the solution
After you have figured out what files you have in the working directory, you can access those easily. Obviously, what you do with the files will depend on what kind of files they are.
For instance, pdf-s and images can be displayed as
library(magick) # you need to install the package first
cheatsheet <- image_read_pdf("cheatsheet.pdf")
plot(cheatsheet)
(You first need to install the package magick, see Section 5.6).
So in order to access files in your current working directory, you just need the file name. No directions needed here, no folder name or anything else.
Exercise 11.4
- Install the “magick” package
- Put an image into your current R working directory
- Use
list.files()
to ensure the image is there - Use the
image_read()
andplot()
to display the image on screen.
You can use file.info()
to query information about existing files,
such as their size, creation time, or whether they are files or
directories. For instance,
## size isdir mode mtime ctime
## index.rmd 3357 FALSE 664 2024-11-20 14:42:52 2024-11-20 21:13:12
## atime uid gid uname grname
## index.rmd 2025-04-04 14:48:57 1000 1000 siim siim
(Obviously, this assumes “index.rmd” exists in the current working directory!) This result is a list (more specifically, a data frame, see Section 12.1) where you can use dollar-notation to extract individual components. For instance, the file size is
## [1] 3357
bytes.
To access files in R:
getwd()
prints the current working directorylist.files()
prints the file names in the current working directory- file names must be quoted!
But what if Yucun is not interested in the cheatsheet, but wants to access the photos in his Pictures folder instead? This can be done either by using absolute or relative path. Next, we discuss how to do that.
11.2.1 Accessing files through relative path
Normally, relative path is what you want to use as this allows you to move your project to a different folder, and collaborate with others more easily. It also indicates the folder layout of your project, it is much harder to understand it from the absolute path.
We should begin by writing the “Directions” to get into the Pictures folder from info201 (see Section 2.1.1):
- up (into UW)
- up (into Documents)
- up (into Yucun’s stuff)
- into Pictures
Or in a shorter form “up - up - up - into Pictures”.
But these directions are made for humans, not for the computer. We need to translate it to computer language as this: First, take the short form of the directions. And now:
- replace up by two dots
..
- replace the dash “-” with slash
/
(not backslash\
!) - ensure that everything is enclosed in quotes.
So the relative path of Pictures from info201 will look like
The list.files()
command we used above accepts a relative path as an argument,
so Yucun can check his images as
A note about Windows.
Mac and linux consistently use slash /
as the path separator, but
Windows uses backslash \
by default. That will work too, but you
need to use double backslashes \\
instead of single one. This is
because backslash is a special character inside of character strings,
and you need to escape it with another backslash to actually be able
to insert one into the string.
In this course we consistently use the forward slash /
instead.
A very similar approach also helps to display an image there. For instance, in order to display Ross Lake.jpg, you need directions:
- up (into UW)
- up (into Documents)
- up (into Yucun’s stuff)
- into Pictures
- grab Ross Lake.jpg

Photo of Ross Lake, as plotted in R using the magick library.
And the corresponding short directions are
“up - up - up - into Pictures - grab Ross Lake.jpg”.
In the “computer language”
this
translates to
Yucun can display it as
Exercise 11.5
- What is the working directory of your current R instance? (In the Rstudio console.)
- Pick and image in your Pictures folder
- Draw the file system tree that includes both the current R working directory, and that image (in pictures folder).
- Write down the directions to get to the image from R’s working directory.
- What is the relative path of the image, from the current R working directory, using R notation?
- Use
image_read()
with relative path, andplot()
to show the image on screen!
11.2.2 Accessing files through absolute path
Alternatively, we can access the files through absolute path. For a small project that only runs in your computer, the relative and absolute path will work equally well. Absolute path has two distinct advantages:
- if you need access data or files that are not connected to the current project, you may need absolute path
- Many graphical file managers have an option to display and copy the absolute path of files. This is very helpful for beginners.
However, it is harder to use when your code also has to run on your team-mates computers, and it is virtually impossible to do if the code also has to run on cloud servers. Absolute path is also somewhat different for unix (mac and linux) and for windows.
The absolute path directions to the image folder (if Yucun uses Mac) are (see Section 2.4.1):
- Start at root “/”
- into Users
- into yucun
- into Pictures
or “/ - Users - yucun - Pictures”. This translates in exactly the same way as the relative path into
In a similar fashion, Yucun can display the Ross Lake picture as
If Yucun uses Windows, the absolute path looks like
- Start at root “This PC”
- into drive “C:”
- into Users
- into yucun
- into Pictures
Or “start at root This PC - C: - Users - yucun - Pictures”. The difference is that on Windows, the root directory “This PC” is not marked, so the absolute path is
Yucun can now display the Ross Lake picture as

Accessing and plotting files on Windows. The file explorer window does not display the file extension–the image is just diplayed as “Ross lake” (of type “JPG”). However, R lists it as “Ross lake.jpg”. The latter name is needed to load and plot it.
Exercise 11.6
- Pick an pdf document in your Documents folder (or wherever you keep your documents)
- Draw the file system tree that includes the document (in Documents folder).
- Write down the directions about how to get to the document from the root folder.
- What is the absolute path of the document using the R notation?
- Use
image_read()
with absolute path, andplot()
to show the pdf on screen!
TBD: tilde as home folder marker (Documents on win)