Press "Enter" to skip to content

How to work with file system from R

The temporary directory and files

Firstly let’s create a folder to work within a temporary directory. Look for details on how to work with temporary files in R in my post When do you need a temporary directory and files in R and how to use them.

Function tempfile() does not create any files. It generates a random file name, which is unique for the current R session. By default, tempfile() generates a file name in R temporary directory.

mydirname <- tempfile(pattern = "mydir")
mydirname
[1] "/tmp/RtmpVuBPUY/mydir1e5a69bc39ac"

Exploring file system

In themydirnamevariable, we have a file path where we can create a file. Now there is nothing. Let’s check it with the function file.exists() and the content of the temporary directory with the function dir().

file.exists(mydirname)
[1] FALSE
dir(tempdir())
character(0) # Empty character vector

Creating a directory

Next, we touch our file system and create the desired directory.

dir.create(mydirname)

And recheck it:

  • existence;
  • content of the parent directory;
  • modification time.
file.exists(mydirname)
[1] TRUE
dir(tempdir(), full.names = TRUE)
[1] "/tmp/RtmpVuBPUY/mydir1e5a69bc39ac"
file.mtime(mydirname)
[1] "2016-08-17 19:19:37 MSK"

More listings, including R system files

Let’s inspect files from one of the installed R-packages. A command system.file() returns the full path of files from installed packages. We use it to get a file path of stats package.

system.file(package = "stats")
[1] "/usr/lib64/R/library/stats"

Next, we list the content of this folder.

dir(system.file(package = "stats"))
 [1] "COPYRIGHTS.modreg" "demo"              "DESCRIPTION"    
 [4] "help"              "html"              "INDEX"          
 [7] "libs"              "Meta"              "NAMESPACE"      
[10] "R"                 "SOURCES.ts"     

We can check the content of any subdirectory. For example, the “demo” folder.

dir(system.file("demo", package = "stats"))
[1] "glm.vr.R" "lm.glm.R" "nlm.R"    "smooth.R"

In some situations, you would prefer full names:

dir(system.file("demo", package = "stats"), full.names = TRUE)
[1] "/usr/lib64/R/library/stats/demo/glm.vr.R"
[2] "/usr/lib64/R/library/stats/demo/lm.glm.R"
[3] "/usr/lib64/R/library/stats/demo/nlm.R"   
[4] "/usr/lib64/R/library/stats/demo/smooth.R"

Constructing of file names in R

What is a file name? It is a character string. Wrong. It is a system-specific character string.

R function file.path() creates system-specific file names.

workingdir <- "projects"
projectdir <- "warandpeace"
datadir    <- "data"
file.path(workingdir, projectdir, datadir)
# Linux-based OS
[1] "projects/warandpeace/data"
# Surprisingly in Windows we have similar results
[1] "projects/warandpeace/data"

Note about Windows

Why did file.path() put slashes as separators when we used to backslash in Windows (for example C:\Program Files\R\R-3.3.1)? There is even a special note on it in file.path’s help page:

The components are by default separated by ‘/’ (not ‘\’) on Windows.

Surprisingly DOS and Windows supported slash as file path separators from the beginning. So in most cases, one can use both variants in Windows.

Leave a Reply

Your email address will not be published. Required fields are marked *

Защита от спама *