files R

Author

E Vlasov

Files in R

R code can do everything with files computer system can do. File processing is done using base R or fs package. Also direct system call can be done.

Base R

First let’s make a folder for the text files

dname <- "txt_files" # variable to store folder name
dir.create(dname)
Warning in dir.create(dname): 'txt_files' already exists

Then check the list of files in the folder

list.files(dname)
[1] "a.crt" "d.crt" "e.crt" "f.crt"

It is empty as it was just created

Create some files

# create string of file names
fnames <- c("a.txt", "b.txt", "c.txt", "d.txt", "e.txt", "f.txt")
# add path to the folder
fnames <- paste0(dname, "/", fnames) # paste glues strings together
fnames
[1] "txt_files/a.txt" "txt_files/b.txt" "txt_files/c.txt" "txt_files/d.txt"
[5] "txt_files/e.txt" "txt_files/f.txt"
file.create(fnames)
[1] TRUE TRUE TRUE TRUE TRUE TRUE

Check again for the files in the folder

list.files(dname)
 [1] "a.crt" "a.txt" "b.txt" "c.txt" "d.crt" "d.txt" "e.crt" "e.txt" "f.crt"
[10] "f.txt"

Now there are some empty files

Put some file names into another variable

Concatenate with path

fn_subset <- paste0(dname, '/', c("b.txt", "c.txt"))
fn_subset
[1] "txt_files/b.txt" "txt_files/c.txt"

Check if the files are in the folder

file.exists(fn_subset)
[1] TRUE TRUE

Both files are in the folder

Let’s remove

file.remove(fn_subset)
[1] TRUE TRUE
list.files()
[1] "files_R_files"      "files_R.nb.html"    "files_R.qmd"       
[4] "files_R.rmarkdown"  "files_R.Rmd"        "palace of creation"
[7] "robbo"              "rsconnect"          "txt_files"         
file.exists(fn_subset)
[1] FALSE FALSE

R does not support bulk rename but we can make our own function for it later

Rename a file

file.rename("e.txt", "j.txt")
Warning in file.rename("e.txt", "j.txt"): cannot rename file 'e.txt' to 'j.txt',
reason 'No such file or directory'
[1] FALSE
list.files()
[1] "files_R_files"      "files_R.nb.html"    "files_R.qmd"       
[4] "files_R.rmarkdown"  "files_R.Rmd"        "palace of creation"
[7] "robbo"              "rsconnect"          "txt_files"         

Copy a file

file.copy("j.txt", "k.txt")
[1] FALSE
list.files()
[1] "files_R_files"      "files_R.nb.html"    "files_R.qmd"       
[4] "files_R.rmarkdown"  "files_R.Rmd"        "palace of creation"
[7] "robbo"              "rsconnect"          "txt_files"         

fs package

# install.packages("fs", repos = "http://cran.us.r-project.org")
library(fs)
library(stringr)

Folder exists

dir_exists(dname)
txt_files 
     TRUE 

Files exist

file_exists(fnames)
txt_files/a.txt txt_files/b.txt txt_files/c.txt txt_files/d.txt txt_files/e.txt 
           TRUE           FALSE           FALSE            TRUE            TRUE 
txt_files/f.txt 
           TRUE 

List files in the folder and set a variable

Then make a new variable replacing .txt by .crt for new file names

fnames <- dir_ls("txt_files")
fnames_new <- str_replace(fnames, ".txt", ".crt")
fnames_new
[1] "txt_files/a.crt" "txt_files/a.crt" "txt_files/d.crt" "txt_files/d.crt"
[5] "txt_files/e.crt" "txt_files/e.crt" "txt_files/f.crt" "txt_files/f.crt"

Rename files with move function

file_move(fnames, fnames_new)

List files

dir_ls(dname)
txt_files/a.crt txt_files/d.crt txt_files/e.crt txt_files/f.crt 

All files are crt now

Package fs has all the functions, for example file can be copied or deleted

file_copy("txt_files/a.crt", "txt_files/aa.crt")
print("files before deleting: ")
[1] "files before deleting: "
dir_ls(dname)
txt_files/a.crt  txt_files/aa.crt txt_files/d.crt  txt_files/e.crt  
txt_files/f.crt  
file_delete("txt_files/aa.crt")
print("files after deleting: ")
[1] "files after deleting: "
dir_ls(dname)
txt_files/a.crt txt_files/d.crt txt_files/e.crt txt_files/f.crt