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 |
base::all.equal()
.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()
.
all_equal(a, b = NULL)
all_equal(a, b = NULL)
a |
A vector, array or list. |
b |
Either |
TRUE
if "equality of all" is satisfied (as detailed in
'Description' above) and FALSE
otherwise.
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()
.
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
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
Copy of strex::str_before_last_dot()
.
before_last_dot(...) str_before_last_dot(...)
before_last_dot(...) str_before_last_dot(...)
... |
Pass-through to |
Copy of strex::str_can_be_numeric()
.
can_be_numeric(...) str_can_be_numeric(...)
can_be_numeric(...) str_can_be_numeric(...)
... |
Pass-through to |
Given the names of (potential) directories, create the ones that do not already exist.
create_dir(...)
create_dir(...)
... |
The names of the directories, specified via relative or absolute paths. Duplicates are ignored. |
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.
## Not run: create_dir(c("mydir", "yourdir")) remove_dir(c("mydir", "yourdir")) ## End(Not run)
## Not run: create_dir(c("mydir", "yourdir")) remove_dir(c("mydir", "yourdir")) ## End(Not run)
See strex::str_extract_currencies()
.
str_extract_currencies(...) extract_currencies(...) str_nth_currency(...) nth_currency(...) str_first_currency(...) first_currency(...) str_last_currency(...) last_currency(...)
str_extract_currencies(...) extract_currencies(...) str_nth_currency(...) nth_currency(...) str_first_currency(...) first_currency(...) str_last_currency(...) last_currency(...)
... |
Pass-through to |
Extend a character vector by appending empty strings at the end.
extend_char_vec(char_vec, extend_by = NA, length_out = NA) str_extend_char_vec(char_vec, extend_by = NA, length_out = NA)
extend_char_vec(char_vec, extend_by = NA, length_out = NA) str_extend_char_vec(char_vec, extend_by = NA, length_out = NA)
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? |
A character vector.
extend_char_vec(1:5, extend_by = 2) extend_char_vec(c("a", "b"), length_out = 10)
extend_char_vec(1:5, extend_by = 2) extend_char_vec(c("a", "b"), length_out = 10)
Copies of strex::str_extract_non_numerics()
and friends.
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(...)
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(...)
... |
Pass-through to |
Copies of strex::str_extract_numbers()
and friends.
extract_numbers(...) str_extract_numbers(...) nth_number(...) str_nth_number(...) first_number(...) str_first_number(...) last_number(...) str_last_number(...)
extract_numbers(...) str_extract_numbers(...) nth_number(...) str_nth_number(...) first_number(...) str_first_number(...) last_number(...) str_last_number(...)
... |
Pass-through to |
filesstrings
: handy file and string manipulationThis 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.
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.
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.
group_close(vec_ascending, max_gap = 1)
group_close(vec_ascending, max_gap = 1)
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. |
A where each element is one group, as a numeric vector.
group_close(1:10, 1) group_close(1:10, 0.5) group_close(c(1, 2, 4, 10, 11, 14, 20, 25, 27), 3)
group_close(1:10, 1) group_close(1:10, 0.5) group_close(c(1, 2, 4, 10, 11, 14, 20, 25, 27), 3)
Copy of strex::str_locate_braces()
.
locate_braces(...) str_locate_braces(...)
locate_braces(...) str_locate_braces(...)
... |
Pass-through to |
Copy of strex::match_arg()
.
match_arg(...) str_match_arg(...)
match_arg(...) str_match_arg(...)
... |
Pass-through to |
Move specified files into specified directories
move_files(files, destinations, overwrite = FALSE) file.move(files, destinations, overwrite = FALSE)
move_files(files, destinations, overwrite = FALSE) file.move(files, destinations, overwrite = FALSE)
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. |
If there are files, there must be either
or
directories. If there is one directory, then all
files are moved
there. If there are
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.
Invisibly, a logical vector with a TRUE
for each time the operation
succeeded and a FALSE
for every fail.
## 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)
## 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)
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".
nice_file_nums(dir = ".", pattern = NA)
nice_file_nums(dir = ".", pattern = NA)
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). |
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.
A logical vector with a TRUE
for each successful rename
(should be all TRUE
s) and a FALSE
otherwise.
## 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)
## 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)
n
th number after the m
th occurrence of a pattern.Copy of strex::str_nth_number_after_mth()
.
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(...)
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(...)
... |
Pass-through to |
n
th number before the m
th occurrence of a pattern.Copy of strex::str_nth_number_before_mth()
.
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(...)
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(...)
... |
Pass-through to |
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.
put_in_pos(strings, positions) str_put_in_pos(strings, positions)
put_in_pos(strings, positions) str_put_in_pos(strings, positions)
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. |
A character vector.
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)
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)
Delete directories and all of their contents.
remove_dir(...) dir.remove(...)
remove_dir(...) dir.remove(...)
... |
The names of the directories, specified via relative or absolute paths. |
Invisibly, a logical vector with TRUE
for each success and
FALSE
for failures.
## Not run: sapply(c("mydir1", "mydir2"), dir.create) remove_dir(c("mydir1", "mydir2")) ## End(Not run)
## Not run: sapply(c("mydir1", "mydir2"), dir.create) remove_dir(c("mydir1", "mydir2")) ## End(Not run)
Remove spaces in file names in a specified directory, replacing them with whatever you want, default nothing.
remove_filename_spaces(dir = ".", pattern = "", replacement = "")
remove_filename_spaces(dir = ".", pattern = "", replacement = "")
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. |
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.
## 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)
## 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)
Rename the files in the directory, replacing file names with numbers only.
rename_with_nums(dir = ".", pattern = NULL)
rename_with_nums(dir = ".", pattern = NULL)
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. |
A logical vector with a TRUE
for each successful renaming and a
FALSE
otherwise.
## 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)
## 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)
n
th occurrence of pattern.Copies of strex::str_after_nth()
and friends.
str_after_nth(...) after_nth(...) str_after_first(...) after_first(...) str_after_last(...) after_last(...)
str_after_nth(...) after_nth(...) str_after_first(...) after_first(...) str_after_last(...) after_last(...)
... |
Pass-through to |
n
th occurrence of pattern.Copies of strex::str_before_nth()
and friends.
str_before_nth(...) before_nth(...) str_before_first(...) before_first(...) str_before_last(...) before_last(...)
str_before_nth(...) before_nth(...) str_before_first(...) before_first(...) str_before_last(...) before_last(...)
... |
Pass-through to |
Copy of strex::str_elem()
.
str_elem(...) elem(...)
str_elem(...) elem(...)
... |
Pass-through to |
Copy of strex::str_elems()
.
str_elems(...) elems(...)
str_elems(...) elems(...)
... |
Pass-through to |
Copy of strex::str_give_ext()
.
str_give_ext(...) give_ext(...)
str_give_ext(...) give_ext(...)
... |
Pass-through to |
th instance of a pattern.Copy of strex::str_locate_nth()
.
str_locate_nth(...) locate_nth(...) str_locate_first(...) locate_first(...) str_locate_last(...) locate_last(...)
str_locate_nth(...) locate_nth(...) str_locate_first(...) locate_first(...) str_locate_last(...) locate_last(...)
... |
Pass-through to |
Copy of strex::str_alphord_nums()
.
str_nice_nums(...) nice_nums(...) str_alphord_nums(...) alphord_nums(...)
str_nice_nums(...) nice_nums(...) str_alphord_nums(...) alphord_nums(...)
... |
Pass-through to |
Copy of strex::str_paste_elems()
.
str_paste_elems(...) paste_elems(...)
str_paste_elems(...) paste_elems(...)
... |
Pass-through to |
Copy of strex::str_remove_quoted()
.
str_remove_quoted(...) remove_quoted(...)
str_remove_quoted(...) remove_quoted(...)
... |
Pass-through to |
Copy of strex::str_singleize()
.
str_singleize(...) singleize(...)
str_singleize(...) singleize(...)
... |
Pass-through to |
Copy of strex::str_split_by_numbers()
.
str_split_by_nums(...) split_by_nums(...) split_by_numbers(...) str_split_by_numbers(...)
str_split_by_nums(...) split_by_nums(...) split_by_numbers(...) str_split_by_numbers(...)
... |
Pass-through to |
See strex::str_split_camel_case()
.
str_split_camel_case(string, lower = FALSE) split_camel_case(string, lower = FALSE)
str_split_camel_case(string, lower = FALSE) split_camel_case(string, lower = FALSE)
string |
A character vector. |
lower |
Do you want the output to be all lower case (or as is)? |
Copy of strex::str_to_vec()
.
str_to_vec(...) to_vec(...)
str_to_vec(...) to_vec(...)
... |
Pass-through to |
Copy of strex::str_trim_anything()
.
str_trim_anything(...) trim_anything(...)
str_trim_anything(...) trim_anything(...)
... |
Pass-through to |
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.
unitize_dirs(unit, pattern = NULL, dir = ".")
unitize_dirs(unit, pattern = NULL, dir = ".")
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)? |
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.
Invisibly TRUE
if the operation is successful, if not there will be
an error.
## 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)
## 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)