File Manipulation with R language

http://me.seekingQED.com

Here I would like to introduce the usage of the file manupulation functions in R language. Some example codes in the official help documents will be used here, but with more details (you can access the official help documents by command help(file.create)).

# 1. Basic File Manipulation

cat("file A\n", file = "A")
cat("file B\n", file = "B")

cat function is more commonly used to print output, but it can also be used to export character string into a file. The format of the file is not limited, for example, cat("file A\n", file = "A.txt"), cat("file A\n", file = "A.html") or even cat("print('a')\nprint('b')", file = "A.R") would work too.

This feature is quite useful in some situations, like I can generate some javascript codes with R and embed them into html code automatically.

file.append("A", "B")

file.append function is used to append the content of file B into file A.

Please note that even if these two files are in different format, this function can still work, like file.append("A.R", "B.txt") given that the contents are both character strings.

file.create("A")

file.create function helps create an empty file. The format here is not limited either, that is, we can have codes like file.create("A.R") or file.create("A.txt").

file.append("A", rep("B", 10))

Please note that the second argument rep("B", 10) is actually a vector.

The expalation of file1 and file2 for file.append(file1, file2) is “character vectors, containing file names or paths”, which means the contents we’re going to append can be from multiple files, as well as the “destinations” of the appended contents. Such as file.append("A", c("B","A")) and file.append(c("B","A"), "A").

if(interactive()) file.show("A")

Here interactive() is a function checking if you are using R interactively, that is, if you are opening a R console and using it (interactively, return TRUE), or are running a R script (not interactively, return FALSE).

interactive() would be useful in function development. E.g., if you want to publish your package on CRAN, you may need this function to pass their checks while you have some interactive lines in your functions (e.g., you need the user to make decision among several options).

Let’s come back to file.show function. It helps open files in text-editor way.

dir.create("tmp")
file.copy("A", "C")
file.copy(c("A", "B"), "tmp")

dir.create function is used to build a folder with the given name.

Regarding function file.copy, to avoid misunderstanding, it’s better to use the complete format like file.copy(from = "A", to = "C"). This command will copy the file A and paste it.

Please note that if to argument is a file name, then the new file will be pasted and named as “C”. If to argument, i.e. C is a path, then file A will be pasted into path C and the name of new file in path C will be “A”.

file.rename("A", "B")

Sometimes you may want to change the name of one file, then you may need function file.rename. In the example above, the name of file “A” is changed to be “B”.

Please note that this function can also help us change the format of one file, like with file.rename("A.txt", "A.R"), we can convert a text file into a R script.

file.info("A")

file.info function returns the basic information of one file, including the size, if it’s a directory, permission mode, modification time, create time, access time, etc.

file.mode("A")

The file ‘mode’ follows POSIX conventions, giving three octal digits summarizing the permissions for [1] the file owner, [2] the owner’s group [3] and for anyone respectively.

Each digit is the logical or of read (4), write (2) and execute/search (1) permissions.

file.mtime("A")
file.size("A")
file.exists("A")

“mtime” and “size” here are simply a more concise usage of file.info().

file.exists helps confirm if the file or path (folder) exists.

file.remove("B")

file.remove function is used to remove files.

It can also be used to remove folders but these folders must be empty.

# 2. Folder Manipulation

Other than files, it’s also necessary to manipulate folders, like build a folder, or return the list of the files in one folder.

dir.create("test")

dir.create function helps build a folder. In the example above, a folder named “test” will be built in the current working directory. You can also speicify the directory.

list.dirs("c:/")
list.files("c:/", recursive = FALSE)
dir("c:/", recursive = FALSE)

list.dirs will return a list of all the directories (folders) in the given path, while list.files and dir (actually these two are the same) will return a list of all the files and directories in the given path. If the path is left blank, then the default path will be the current working directory.

Please be careful about the argument recursive in list.files and dir. If you set it to be TRUE, it will also list all the files in the folders and subfolders of the current path. If you’re running with a high-level path, the result may be very long.