Package 'filesstrings'

Title: Handy File and String Manipulation
Description: This started out as a package for file and string manipulation. Since then, the 'fs' and 'strex' packages emerged, offering functionality previously given by this package (but it's done better in these new ones). Those packages have hence almost pushed 'filesstrings' into extinction. However, it still has a small number of unique, handy file manipulation functions which can be seen in the vignette. One example is a function to remove spaces from all file names in a directory.
Authors: Rory Nolan [aut, cre, cph] , Sergi Padilla-Parra [ths]
Maintainer: Rory Nolan <[email protected]>
License: GPL-3
Version: 3.4.0
Built: 2024-11-22 04:21:47 UTC
Source: https://github.com/rorynolan/filesstrings

Help Index


An alternative version of base::all.equal().

Description

This function will return TRUE whenever base::all.equal() would return TRUE, however it will also return TRUE in some other cases:

  • If a is given and b is not, TRUE will be returned if all of the elements of a are the same.

  • If a is a scalar and b is a vector or array, TRUE will be returned if every element in b is equal to a.

  • If a is a vector or array and b is a scalar, TRUE will be returned if every element in a is equal to b.

This function ignores names and attributes (except for dim).

When this function does not return TRUE, it returns FALSE (unless it errors). This is unlike base::all.equal().

Usage

all_equal(a, b = NULL)

Arguments

a

A vector, array or list.

b

Either NULL or a vector, array or list of length either 1 or length(a).

Value

TRUE if "equality of all" is satisfied (as detailed in 'Description' above) and FALSE otherwise.

Note

  • This behaviour is totally different from base::all.equal().

  • There's also dplyr::all_equal(), which is different again. To avoid confusion, always use the full filesstrings::all_equal() and never library(filesstrings) followed by just all_equal().

Examples

all_equal(1, rep(1, 3))
all_equal(2, 1:3)
all_equal(1:4, 1:4)
all_equal(1:4, c(1, 2, 3, 3))
all_equal(rep(1, 10))
all_equal(c(1, 88))
all_equal(1:2)
all_equal(list(1:2))
all_equal(1:4, matrix(1:4, nrow = 2)) # note that this gives TRUE

Get the part of a string before the last period.

Description

Copy of strex::str_before_last_dot().

Usage

before_last_dot(...)

str_before_last_dot(...)

Arguments

...

Pass-through to strex function.


Check if a string could be considered as numeric.

Description

Copy of strex::str_can_be_numeric().

Usage

can_be_numeric(...)

str_can_be_numeric(...)

Arguments

...

Pass-through to strex function.


Create directories if they don't already exist

Description

Given the names of (potential) directories, create the ones that do not already exist.

Usage

create_dir(...)

Arguments

...

The names of the directories, specified via relative or absolute paths. Duplicates are ignored.

Value

Invisibly, a vector with a TRUE for each time a directory was actually created and a FALSE otherwise. This vector is named with the paths of the directories that were passed to the function.

Examples

## Not run: 
create_dir(c("mydir", "yourdir"))
remove_dir(c("mydir", "yourdir"))

## End(Not run)

Get the currencies of numbers within a string.

Description

See strex::str_extract_currencies().

Usage

str_extract_currencies(...)

extract_currencies(...)

str_nth_currency(...)

nth_currency(...)

str_first_currency(...)

first_currency(...)

str_last_currency(...)

last_currency(...)

Arguments

...

Pass-through to strex function.


Pad a character vector with empty strings.

Description

Extend a character vector by appending empty strings at the end.

Usage

extend_char_vec(char_vec, extend_by = NA, length_out = NA)

str_extend_char_vec(char_vec, extend_by = NA, length_out = NA)

Arguments

char_vec

A character vector. The thing you wish to expand.

extend_by

A non-negative integer. By how much do you wish to extend the vector?

length_out

A positive integer. How long do you want the output vector to be?

Value

A character vector.

Examples

extend_char_vec(1:5, extend_by = 2)
extend_char_vec(c("a", "b"), length_out = 10)

Extract non-numbers from a string.

Description

Copies of strex::str_extract_non_numerics() and friends.

Usage

extract_non_numerics(...)

str_extract_non_numerics(...)

nth_non_numeric(...)

str_nth_non_numeric(...)

first_non_numeric(...)

str_first_non_numeric(...)

last_non_numeric(...)

str_last_non_numeric(...)

Arguments

...

Pass-through to strex function.


Extract numbers from a string.

Description

Copies of strex::str_extract_numbers() and friends.

Usage

extract_numbers(...)

str_extract_numbers(...)

nth_number(...)

str_nth_number(...)

first_number(...)

str_first_number(...)

last_number(...)

str_last_number(...)

Arguments

...

Pass-through to strex function.


filesstrings: handy file and string manipulation

Description

This started out as a package for file and string manipulation. Since then, the fs file manipulation package and the strex string manipulation package emerged, offering functionality previously given by this package (but slightly better). Those packages have hence almost pushed 'filesstrings' into extinction. However, it still has a small number of unique, handy file manipulation functions which can be seen in the vignette.. One example is a function to remove spaces from all file names in a directory.

References

Rory Nolan and Sergi Padilla-Parra (2017). filesstrings: An R package for file and string manipulation. The Journal of Open Source Software, 2(14). doi:10.21105/joss.00260.


Group together close adjacent elements of a vector.

Description

Given a strictly increasing vector (each element is bigger than the last), group together stretches of the vector where adjacent elements are separated by at most some specified distance. Hence, each element in each group has at least one other element in that group that is close to it. See the examples.

Usage

group_close(vec_ascending, max_gap = 1)

Arguments

vec_ascending

A strictly increasing numeric vector.

max_gap

The biggest allowable gap between adjacent elements for them to be considered part of the same group.

Value

A where each element is one group, as a numeric vector.

Examples

group_close(1:10, 1)
group_close(1:10, 0.5)
group_close(c(1, 2, 4, 10, 11, 14, 20, 25, 27), 3)

Locate the braces in a string.

Description

Copy of strex::str_locate_braces().

Usage

locate_braces(...)

str_locate_braces(...)

Arguments

...

Pass-through to strex function.


Argument Matching

Description

Copy of strex::match_arg().

Usage

match_arg(...)

str_match_arg(...)

Arguments

...

Pass-through to strex function.


Move files around.

Description

Move specified files into specified directories

Usage

move_files(files, destinations, overwrite = FALSE)

file.move(files, destinations, overwrite = FALSE)

Arguments

files

A character vector of files to move (relative or absolute paths).

destinations

A character vector of the destination directories into which to move the files.

overwrite

Allow overwriting of files? Default no.

Details

If there are nn files, there must be either 11 or nn directories. If there is one directory, then all nn files are moved there. If there are nn directories, then each file is put into its respective directory. This function also works to move directories.

If you try to move files to a directory that doesn't exist, the directory is first created and then the files are put inside.

Value

Invisibly, a logical vector with a TRUE for each time the operation succeeded and a FALSE for every fail.

Examples

## Not run: 
dir.create("dir")
files <- c("1litres_1.txt", "1litres_30.txt", "3litres_5.txt")
file.create(files)
file.move(files, "dir")

## End(Not run)

Make file numbers comply with alphabetical order

Description

If files are numbered, their numbers may not comply with alphabetical order, i.e. "file2.ext" comes after "file10.ext" in alphabetical order. This function renames the files in the specified directory such that they comply with alphabetical order, so here "file2.ext" would be renamed to "file02.ext".

Usage

nice_file_nums(dir = ".", pattern = NA)

Arguments

dir

Path (relative or absolute) to the directory in which to do the renaming (default is current working directory).

pattern

A regular expression. If specified, files to be renamed are restricted to ones matching this pattern (in their name).

Details

It works on file names with more than one number in them e.g. "file01part3.ext" (a file with 2 numbers). All the file names that it works on must have the same number of numbers, and the non-number bits must be the same. One can limit the renaming to files matching a certain pattern. This function wraps nice_nums(), which does the string operations, but not the renaming. To see examples of how this function works, see the examples in that function's documentation.

Value

A logical vector with a TRUE for each successful rename (should be all TRUEs) and a FALSE otherwise.

Examples

## Not run: 
dir.create("NiceFileNums_test")
setwd("NiceFileNums_test")
files <- c("1litres_1.txt", "1litres_30.txt", "3litres_5.txt")
file.create(files)
nice_file_nums()
nice_file_nums(pattern = "\\.txt$")
setwd("..")
dir.remove("NiceFileNums_test")

## End(Not run)

Find the nth number after the mth occurrence of a pattern.

Description

Copy of strex::str_nth_number_after_mth().

Usage

nth_number_after_mth(...)

str_nth_number_after_mth(...)

nth_number_after_first(...)

nth_number_after_last(...)

first_number_after_mth(...)

last_number_after_mth(...)

first_number_after_first(...)

first_number_after_last(...)

last_number_after_first(...)

last_number_after_last(...)

str_nth_number_after_first(...)

str_nth_number_after_last(...)

str_first_number_after_mth(...)

str_last_number_after_mth(...)

str_first_number_after_first(...)

str_first_number_after_last(...)

str_last_number_after_first(...)

str_last_number_after_last(...)

Arguments

...

Pass-through to strex function.


Find the nth number before the mth occurrence of a pattern.

Description

Copy of strex::str_nth_number_before_mth().

Usage

nth_number_before_mth(...)

str_nth_number_before_mth(...)

nth_number_before_first(...)

nth_number_before_last(...)

first_number_before_mth(...)

last_number_before_mth(...)

first_number_before_first(...)

first_number_before_last(...)

last_number_before_first(...)

last_number_before_last(...)

str_nth_number_before_first(...)

str_nth_number_before_last(...)

str_first_number_before_mth(...)

str_last_number_before_mth(...)

str_first_number_before_first(...)

str_first_number_before_last(...)

str_last_number_before_first(...)

str_last_number_before_last(...)

Arguments

...

Pass-through to strex function.


Put specified strings in specified positions in an otherwise empty character vector.

Description

Create a character vector with a set of strings at specified positions in that character vector, with the rest of it taken up by empty strings.

Usage

put_in_pos(strings, positions)

str_put_in_pos(strings, positions)

Arguments

strings

A character vector of the strings to put in positions (coerced by as.character if not character already).

positions

The indices of the character vector to be occupied by the elements of strings. Must be the same length as strings or of length 1.

Value

A character vector.

Examples

put_in_pos(1:3, c(1, 8, 9))
put_in_pos(c("Apple", "Orange", "County"), c(5, 7, 8))
put_in_pos(1:2, 5)

Remove directories

Description

Delete directories and all of their contents.

Usage

remove_dir(...)

dir.remove(...)

Arguments

...

The names of the directories, specified via relative or absolute paths.

Value

Invisibly, a logical vector with TRUE for each success and FALSE for failures.

Examples

## Not run: 
sapply(c("mydir1", "mydir2"), dir.create)
remove_dir(c("mydir1", "mydir2"))

## End(Not run)

Remove spaces in file names

Description

Remove spaces in file names in a specified directory, replacing them with whatever you want, default nothing.

Usage

remove_filename_spaces(dir = ".", pattern = "", replacement = "")

Arguments

dir

The directory in which to perform the operation.

pattern

A regular expression. If specified, only files matching this pattern will be treated.

replacement

What do you want to replace the spaces with? This defaults to nothing, another sensible choice would be an underscore.

Value

A logical vector indicating which operation succeeded for each of the files attempted. Using a missing value for a file or path name will always be regarded as a failure.

Examples

## Not run: 
dir.create("RemoveFileNameSpaces_test")
setwd("RemoveFileNameSpaces_test")
files <- c("1litres 1.txt", "1litres 30.txt", "3litres 5.txt")
file.create(files)
remove_filename_spaces()
list.files()
setwd("..")
dir.remove("RemoveFileNameSpaces_test")

## End(Not run)

Replace file names with numbers

Description

Rename the files in the directory, replacing file names with numbers only.

Usage

rename_with_nums(dir = ".", pattern = NULL)

Arguments

dir

The directory in which to rename the files (relative or absolute path). Defaults to current working directory.

pattern

A regular expression. If specified, only files with names matching this pattern will be treated.

Value

A logical vector with a TRUE for each successful renaming and a FALSE otherwise.

Examples

## Not run: 
dir.create("RenameWithNums_test")
setwd("RenameWithNums_test")
files <- c("1litres 1.txt", "1litres 30.txt", "3litres 5.txt")
file.create(files)
rename_with_nums()
list.files()
setwd("..")
dir.remove("RenameWithNums_test")

## End(Not run)

Text after the nth occurrence of pattern.

Description

Copies of strex::str_after_nth() and friends.

Usage

str_after_nth(...)

after_nth(...)

str_after_first(...)

after_first(...)

str_after_last(...)

after_last(...)

Arguments

...

Pass-through to strex function.


Text before the nth occurrence of pattern.

Description

Copies of strex::str_before_nth() and friends.

Usage

str_before_nth(...)

before_nth(...)

str_before_first(...)

before_first(...)

str_before_last(...)

before_last(...)

Arguments

...

Pass-through to strex function.


Extract a single character from a string, using its index.

Description

Copy of strex::str_elem().

Usage

str_elem(...)

elem(...)

Arguments

...

Pass-through to strex function.


Extract several single elements from a string.

Description

Copy of strex::str_elems().

Usage

str_elems(...)

elems(...)

Arguments

...

Pass-through to strex function.


Ensure a file name has the intended extension.

Description

Copy of strex::str_give_ext().

Usage

str_give_ext(...)

give_ext(...)

Arguments

...

Pass-through to strex function.


Get the indices of the nnth instance of a pattern.

Description

Copy of strex::str_locate_nth().

Usage

str_locate_nth(...)

locate_nth(...)

str_locate_first(...)

locate_first(...)

str_locate_last(...)

locate_last(...)

Arguments

...

Pass-through to strex function.


Make string numbers comply with alphabetical order.

Description

Copy of strex::str_alphord_nums().

Usage

str_nice_nums(...)

nice_nums(...)

str_alphord_nums(...)

alphord_nums(...)

Arguments

...

Pass-through to strex function.


Extract bits of a string and paste them together.

Description

Copy of strex::str_paste_elems().

Usage

str_paste_elems(...)

paste_elems(...)

Arguments

...

Pass-through to strex function.


Remove the quoted parts of a string.

Description

Copy of strex::str_remove_quoted().

Usage

str_remove_quoted(...)

remove_quoted(...)

Arguments

...

Pass-through to strex function.


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

Description

Copy of strex::str_singleize().

Usage

str_singleize(...)

singleize(...)

Arguments

...

Pass-through to strex function.


Split a string by its numeric characters.

Description

Copy of strex::str_split_by_numbers().

Usage

str_split_by_nums(...)

split_by_nums(...)

split_by_numbers(...)

str_split_by_numbers(...)

Arguments

...

Pass-through to strex function.


Split a string based on CamelCase

Description

See strex::str_split_camel_case().

Usage

str_split_camel_case(string, lower = FALSE)

split_camel_case(string, lower = FALSE)

Arguments

string

A character vector.

lower

Do you want the output to be all lower case (or as is)?


Convert a string to a vector of characters

Description

Copy of strex::str_to_vec().

Usage

str_to_vec(...)

to_vec(...)

Arguments

...

Pass-through to strex function.


Trim something other than whitespace.

Description

Copy of strex::str_trim_anything().

Usage

str_trim_anything(...)

trim_anything(...)

Arguments

...

Pass-through to strex function.


Put files with the same unit measurements into directories

Description

Say you have a number of files with "5min" in their names, number with "10min" in the names, a number with "15min" in their names and so on, and you'd like to put them into directories named "5min", "10min", "15min" and so on. This function does this, but not just for the unit "min", for any unit.

Usage

unitize_dirs(unit, pattern = NULL, dir = ".")

Arguments

unit

The unit upon which to base the categorizing.

pattern

If set, only files with names matching this pattern will be treated.

dir

In which directory do you want to perform this action (defaults to current)?

Details

This function takes the number to be the last number (as defined in nth_number()) before the first occurrence of the unit name. There is the option to only treat files matching a certain pattern.

Value

Invisibly TRUE if the operation is successful, if not there will be an error.

Examples

## Not run: 
dir.create("UnitDirs_test")
setwd("UnitDirs_test")
files <- c("1litres_1.txt", "1litres_3.txt", "3litres.txt", "5litres_1.txt")
file.create(files)
unitize_dirs("litres", "\\.txt")
setwd("..")
dir.remove("UnitDirs_test")

## End(Not run)