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

filesstrings

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

Installation

In R, enter

install.packages("filesstrings")

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:

library(filesstrings)
#> Loading required package: stringr

Files

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.

setwd(tempdir())
dir.create("tmp_00")
file.create("tmp000.txt", "tmp001.txt")
#> [1] TRUE TRUE
list.files()
#> [1] "tmp_00"     "tmp000.txt" "tmp001.txt"
MoveFiles("tmp000.txt", "tmp_00")
#> tmp000.txt 
#>       TRUE
list.files()
#> [1] "tmp_00"     "tmp001.txt"
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)

Delete Directories

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)

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!

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

Strings

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
GetCurrency(request)
#> [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")
sort(file.names)
#> [1] "file1000.tif" "file999.tif"

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

NiceNums(file.names)
#> [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).

is.numeric(23)
#> [1] TRUE
is.numeric("23")
#> [1] FALSE
CanBeNumeric(23)
#> [1] TRUE
CanBeNumeric("23")
#> [1] TRUE
CanBeNumeric("23a")
#> [1] FALSE
StrSplitByNums("23a")
#> [[1]]
#> [1] "23" "a"
CanBeNumeric(StrSplitByNums("23a")[[1]])
#> [1]  TRUE FALSE

The name of a file without the extension

BeforeLastDot("spreadsheet_92.csv")
#> spreadsheet_92.csv 
#>   "spreadsheet_92"

Get the nth element of a string

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

Copy Link

Version

Down Chevron

Install

install.packages('filesstrings')

Monthly Downloads

2,230

Version

0.4.0

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Last Published

April 11th, 2017

Functions in filesstrings (0.4.0)