filesstrings v0.3.2


Monthly downloads



by Rory Nolan

Handy String and File Manipulation

Handy string and file processing and manipulation tools. Built on top of the functionality of 'stringr'. Good for those who like to do all of their file and string manipulation from within R.



An R package for string and file manipulation inspired by struggles with microscopy filenames.

Travis-CI Build Status AppVeyor Build Status codecov CRAN\_Status\_Badge RStudio CRAN downloads Project Status: Active – The project has reached a stable, usable state and is being actively developed.


In R, enter


and you're done!

Please Read the Manual

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. However, I made a couple of vignettes anyway.

First let's load the library:

#> Loading required package: stringr


Here are some file operations that I wished were easier in R.

Move files around

I find it bizarre that base R has no file.move. To move a file, you have to cleverly rename it. Well, no more.

#> [1] TRUE
#> [1] "tmp_00"     "tmp000.txt"
PutFilesInDir("tmp000.txt", "tmp_00")
#> tmp000.txt 
#>       TRUE
#> [1] "tmp_00"
#> [1] "tmp000.txt"
unlink("tmp_00", recursive = TRUE)

Delete Directories

That unlink above with recursive = TRUE was a cryptic way to delete a directory right? I give you RemoveDirs().

#> [1] "tmp_00"
#> tmp_00 
#>   TRUE
#> character(0)

Remove spaces from file names

Surely I don't have to convince anyone that spaces in file names are a bad idea? Let's get rid of some!

file.create(c("file 1.txt", "file 2.txt"))
#> [1] TRUE TRUE
#> [1] "file 1.txt" "file 2.txt"
RemoveFileNameSpaces(replace.with = "_")
#> [1] TRUE TRUE
#> [1] "file_1.txt" "file_2.txt"
#> [1] TRUE TRUE


Here are some string operations that I wished were easier in R.

The nth number in a string

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

request <- "I want the $35 scarf."
NthNumber(request, 1)
#> [1] 35
NthNumber("20 people want the $12 scarf.", -1)  # last number
#> [1] 12
#> [1] "$"

Messed up file numbering

The microscope I use numbers files with 3 numbers by default, i.e. file001.tif, file002.tif and so on. This is a problem when the automatic numbering passes 1000, whereby we have file999.tif, file1000.tif. What's the problem with this? Well, sometimes you need alphabetical order to reflect the true order of your files. These file numbers don't satisfy this requirement:

file.names <- c("file999.tif", "file1000.tif")
#> [1] "file1000.tif" "file999.tif"

so file1000.tif comes before file999.tif in alphabetical order. We want them to be like

#> [1] "file0999.tif" "file1000.tif"

The function NiceFileNums renames all the files in an entire directory to be as we would like. It wraps NiceNums.

Could that be interpreted as numeric?

Sometimes we don't want to know is something is numeric, we want to know if it could be considered to be numeric (or could be coerced to numeric).

#> [1] TRUE
#> [1] FALSE
#> [1] TRUE
#> [1] TRUE
#> [1] FALSE
#> [[1]]
#> [1] "23" "a"
#> [1]  TRUE FALSE

The name of a file without the extension

#> spreadsheet_92.csv 
#>   "spreadsheet_92"

Get the nth element of a string

StrElem("abc", 2)
#> [1] "b"
StrElem("abcdefz", -1)
#> [1] "z"

Functions in filesstrings

Name Description
GetCurrencies Get the currencies of numbers within a string.
ExtractNumbers Extract numbers (or non-numbers) from a string.
MergeTablesOnDisk Merge Tables.
RenameWithNums Replace file names with numbers.
LocateBraces Locate the braces in a string.
GroupClose Group together close adjacent elements of a vector.
GiveExt Ensure a file name has the intended extension.
StrElem Extract a single character of a string, using its index.
StrAfterNth Text before or after
SplitCamelcase Split a string based on camelcase
RemoveQuoted Remove the quoted parts of a string.
CanBeNumeric Check if a string could be considered as numeric.
NiceNums Make string numbers comply with alphabetical order
UnitDirs Put files with the same unit measurements into directories.
DuplicatesToSingles Remove back-to-back duplicates of a pattern in a string.
PasteCollapseListElems Apply paste collapse to each element of a list.
CharListElemsNthElem Get the nth element of each vector in a list of numeric or character
RemoveFileNameSpaces Remove spaces in file names
StringToVec Convert a string to a vector of characters
PasteDifferentLengths Paste vectors/files with different lengths/numbers of lines.
ExtendCharVec Pad a character vector with empty strings.
StrSplitByNums Split a string by its numeric charachters.
PutFilesInDir Put files in a directory
CountMatches Count the number of the matches of a pattern in a string.
StrElemsPasted Extract bits of a string and paste them together
AllEqual Check if all elements are equal.
MoveFiles Move files around.
PutInPos Put specified strings in specified positions
NiceFileNums Make file numbers comply with alphabetical order
CreateDirsIfNotThere Create directories if they don't already exist.
RemoveDirs Remove directories.
BeforeLastDot Get the part of a string before the last period.
StringsWithPatterns Which strings match the patterns?
TrimAnything Trim something other than whitespace
StrListRemoveEmpties Remove empty strings from a character list.
No Results!

Last month downloads


Type Package
License GPL-3
Encoding UTF-8
LazyData true
RoxygenNote 6.0.1
LinkingTo Rcpp
VignetteBuilder knitr
NeedsCompilation yes
Packaged 2017-03-18 09:39:34 UTC; rnolan
Repository CRAN
Date/Publication 2017-03-18 17:52:11 UTC

Include our badge in your README