This package is one for which the functions are largely simple enough such that the function names well describe their purpose, so the manual is an excellent way to acquaint yourself with the package. But I made a couple of vignettes anyway.
First let’s load the library:
library(filesstrings)
#> Loading required package: stringr
Here are some file operations that I wished were easier in R.
I find it bizarre that base R has no file.move
. To move a file, you have to cleverly rename it. Well, no more.
setwd(tempdir())
dir.create("tmp_00")
file.create("tmp000.txt", "tmp001.txt")
#> [1] TRUE TRUE
list.files()
#> [1] "tmp000.txt" "tmp001.txt" "tmp_00"
MoveFiles("tmp000.txt", "tmp_00")
#> tmp000.txt
#> TRUE
list.files()
#> [1] "tmp001.txt" "tmp_00"
list.files("tmp_00")
#> [1] "tmp000.txt"
PutFilesInDir("tmp001.txt", "new_dir") # This function creates the directory new_dir and then puts the files in the first argument in there
#> tmp001.txt
#> TRUE
list.files()
#> [1] "new_dir" "tmp_00"
unlink(c("tmp_00", "new_dir"), recursive = TRUE)
That unlink
above with recursive = TRUE
was a cryptic way to delete a directory right? I give you RemoveDirs()
.
setwd(tempdir())
dir.create("tmp_00")
list.files()
#> [1] "tmp_00"
RemoveDirs("tmp_00")
#> tmp_00
#> TRUE
list.files()
#> character(0)
Surely I don’t have to convince anyone that spaces in file names are a bad idea? Let’s get rid of some!
setwd(tempdir())
file.create(c("file 1.txt", "file 2.txt"))
#> [1] TRUE TRUE
list.files()
#> [1] "file 1.txt" "file 2.txt"
RemoveFileNameSpaces(replace.with = "_")
#> [1] TRUE TRUE
list.files()
#> [1] "file_1.txt" "file_2.txt"
file.remove(list.files())
#> [1] TRUE TRUE