Filesystem in R
Posted on Nov 06, 2010 in Programming
Things under legendu.net/outdated are outdated technologies that the author does not plan to update any more. Please look for better alternatives.
** Things under legendu.net/outdated are outdated technologies that the author does not plan to update any more. Please look for better alternatives. **
-
dirname
returns the parent folder of a file or directory, andbasename
return the name (without the parent folder) of the file or directory. -
Function
file.path
joins path components into one path. It works similar topaste
which concatenates strings to a single one, but it is platform independent. The following code get the full path of the file1.txt
in the current working directory.
file.path(getwd(), '1.txt')
file.path
does not handle the leading/trailing forward/backword slash(es) well,
but this does not matter as the result is still a valid Linux-style path.
> file.path('C:/Study/','1.txt')
[1] "C:/Study//1.txt"
The author of this blog has a package named dclong.fs
which contains a function join_path
that can handle leading/trailing forward/backword slash(es) nicely.
-
There are many useful functions in R which provide low-level interface to the computer's file and directory system, e.g.,
file.create
,file.exists
anddir.create
and so on. If a function for manipulating directories is missing, it is probably the same as the function for manipulating files, e.g. functionfile.rename
can also be used to rename a directory. -
file.info
returns the information of files and directories. The "isdir" column of the resulting data frame indicates whether a path stands for a file or a directory. For example,
> file.info('.')
size isdir mode mtime ctime
. 0 TRUE 777 2011-11-07 22:26:38 2011-10-20 17:14:41
atime exe
. 2011-11-07 22:33:37 no
Notice that file\_test
can also be used to check whether a path is a file or a directory.
For example,
> file_test('-f',".")
[1] FALSE
- Often times, one need to use temporary files.
tempfile
returns a vector of character strings which can be used as names for temporary files. By default the temporary file is created in a temporary directory returned bytempdir
. For example (the following results is platform dependent),
> tempdir()
[1] "C:\\Users\\adu\\AppData\\Local\\Temp\\Rtmp55KGaO"
> tempfile('output.txt')
[1] "C:\\Users\\adu\\AppData\\Local
\\Temp\\Rtmp55KGaO\\output.txt122935c"
Excel
- Write hyper link into excel (using
write.xlsx
) does not work well. One workaround is to write the same content into a CSV file (using write.csv). The hyper links work when you open the CSV file in Excel. You can then save an Excel copy if really want a Excel spreadsheet.