Learn R Programming

⚠️There's a newer version (3.4.0) of this package.Take me there.

filesstrings

Convenient functions for moving files, deleting directories, and a variety of string operations that facilitate manipulating file names and extracting information from strings.

Installation

To install the release version (recommended) from CRAN, in R, enter

install.packages("filesstrings")

To install the development version, in R, first install devtools via install.packages("devtools"). Then enter

devtools::install_github("rorynolan/filesstrings")

Use

First let's load the library:

library(filesstrings)
#> Loading required package: stringr

Files

Move files around

I find it bizarre that base R has no file.move. To move a file, you have to unintuitively rename it. filesstrings provides file.move(files, destinations). This function has the nice feature that if you try to move files to a directory that doesn't exist, it creates the directory first and then puts the files inside. Let's create a directory and a file:

dir.create("tmp_dir")
file.create("tmp.txt")
#> [1] TRUE

Now let's put the file into the directory:

file.move("tmp.txt", "tmp_dir")
#> 1 files moved. 0 failed to move.

Delete Directories

To delete directories with base R, one has to use unlink(..., recursive = TRUE). The filesstrings package gives you dir.remove() which does the same job.

dir.remove("tmp_dir")
#> 1 directories deleted. 0 failed to delete.

Remove spaces from file names

"A space in your file name is a hole in your soul." - Jenny Bryan

RemoveFileNameSpaces(replace.with = "_") replaces them all with underscores for all files in a directory. By default, they are replaced with nothing.

file.create(c("file 1.txt", "file 2.txt"))
#> [1] TRUE TRUE
RemoveFileNameSpaces(pattern = "txt$", replace.with = "_")
#> 2 files renamed. 0 failed to rename.
list.files(pattern = "txt$")
#> [1] "file_1.txt" "file_2.txt"
file.remove(list.files(pattern = "txt$"))  # clean up
#> [1] TRUE TRUE

Strings

The nth number in a string

I often want to get the first, last or nth number in a string.

pop <- "A population of 1000 comprised of 488 dogs and 512 cats."
NthNumber(pop, 1)
#> [1] 1000
NthNumber(pop, -1)  # last number
#> [1] 512

All the numbers in a string

ExtractNumbers(pop)
#> [[1]]
#> [1] 1000  488  512

All the non-numbers in a string

ExtractNonNumerics(pop)
#> [[1]]
#> [1] "A population of " " comprised of "   " dogs and "      
#> [4] " cats."

Trim anything (not just whitespace)

stringr's str_trim just trims whitespace. What if you want to trim something else? Now you can TrimAnything().

TrimAnything("__rmarkdown_", "_")
#> [1] "rmarkdown"

Contribution

Contributions to this package are welcome. The preferred method of contribution is through a github pull request. Feel free to contact me by creating an issue. Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Copy Link

Version

Install

install.packages('filesstrings')

Monthly Downloads

1,002

Version

1.0.0

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Rory Nolan

Last Published

June 5th, 2017

Functions in filesstrings (1.0.0)

ExtractNumbers

Extract numbers (or non-numbers) from a string.
GetCurrencies

Get the currencies of numbers within a string.
DuplicatesToSingles

Remove back-to-back duplicates of a pattern in a string.
ExtendCharVec

Pad a character vector with empty strings.
CountMatches

Count the number of the matches of a pattern in a string.
CreateDirsIfNotThere

Create directories if they don't already exist.
CanBeNumeric

Check if a string could be considered as numeric.
CharListElemsNthElem

Get the nth element of each vector in a list of numeric or character vectors.
AllEqual

BeforeLastDot

Get the part of a string before the last period.
LocateBraces

Locate the braces in a string.
MergeTablesOnDisk

Merge Tables.
StrElem

Extract a single character of a string, using its index.
PasteDifferentLengths

Paste vectors/files with different lengths/numbers of lines.
PutInPos

Put specified strings in specified positions
StrListRemoveEmpties

Remove empty strings from a character list.
StrSplitByNums

Split a string by its numeric charachters.
StrElemsPasted

Extract bits of a string and paste them together
MoveFiles

Move files around.
NiceFileNums

Make file numbers comply with alphabetical order
RemoveQuoted

Remove the quoted parts of a string.
RenameWithNums

Replace file names with numbers.
StringToVec

Convert a string to a vector of characters
GiveExt

Ensure a file name has the intended extension.
GroupClose

Group together close adjacent elements of a vector.
SplitCamelCase

Split a string based on camelcase
StrAfterNth

Text before or after \(n\)th occurrence of pattern.
TrimAnything

Trim something other than whitespace
UnitDirs

Put files with the same unit measurements into directories.
NiceNums

Make string numbers comply with alphabetical order
PasteCollapseListElems

Apply paste collapse to each element of a list.
RemoveDirs

Remove directories.
RemoveFileNameSpaces

Remove spaces in file names
StringsWithPatterns

Which strings match the patterns?