forked from rorynolan/filesstrings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
102 lines (82 loc) · 4.36 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
---
output: github_document
---
# `filesstrings` <img src="junk/sticker.png" height="200" align="right">
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, comment = "#>")
```
Convenient functions for moving files, deleting directories, and a variety of string operations that facilitate manipulating file names and extracting information from strings.
[![Travis-CI Build Status](https://travis-ci.org/rorynolan/filesstrings.svg?branch=master)](https://travis-ci.org/rorynolan/filesstrings)
[![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/rorynolan/filesstrings?branch=master&svg=true)](https://ci.appveyor.com/project/rorynolan/filesstrings)
[![codecov](https://codecov.io/gh/rorynolan/filesstrings/branch/master/graph/badge.svg)](https://codecov.io/gh/rorynolan/filesstrings)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/filesstrings)](https://cran.r-project.org/package=filesstrings)
![RStudio CRAN downloads](http://cranlogs.r-pkg.org/badges/grand-total/filesstrings)
![RStudio CRAN monthly downloads](http://cranlogs.r-pkg.org/badges/filesstrings)
[![Rdocumentation](http://www.rdocumentation.org/badges/version/filesstrings)](http://www.rdocumentation.org/packages/filesstrings)
![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](http://www.repostatus.org/badges/latest/active.svg)
[![lifecycle](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://www.tidyverse.org/lifecycle/#stable)
[![JOSS publication](http://joss.theoj.org/papers/10.21105/joss.00260/status.svg)](https://doi.org/10.21105/joss.00260)
[![DOI](https://zenodo.org/badge/69170704.svg)](https://zenodo.org/badge/latestdoi/69170704)
# Installation
To install the release version (recommended) from CRAN, in R, enter
```{r Install filesstrings, eval=FALSE}
install.packages("filesstrings")
```
To install the development version, in R, first install `devtools` via `install.packages("devtools")`. Then enter
```{r Install filesstrings dev, eval=FALSE}
devtools::install_github("rorynolan/filesstrings")
```
# Use
First let's load the library:
```{r load}
library(filesstrings)
```
## 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:
```{r create dir}
dir.create("tmp_dir")
file.create("tmp.txt")
```
Now let's put the file into the directory:
```{r put a file in a dir}
file.move("tmp.txt", "tmp_dir")
```
### 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.
```{r, remove directory}
dir.remove("tmp_dir")
```
### Remove spaces from file names
"A space in your file name is a hole in your soul." - Jenny Bryan
`remove_filename_spaces(replacement = "_")` replaces them all with underscores for all files in a directory. By default, they are replaced with nothing.
```{r, RemoveFileNameSpaces}
file.create(c("file 1.txt", "file 2.txt"))
remove_filename_spaces(pattern = "txt$", replacement = "_")
list.files(pattern = "txt$")
file.remove(list.files(pattern = "txt$")) # clean up
```
## Strings
### The *n*^th^ number in a string
I often want to get the first, last or *n*^th^ number in a string.
```{r nth number}
pop <- "A population of 1000 comprised of 488 dogs and 512 cats."
nth_number(pop, n = 1)
nth_number(pop, n = -1) # last number
```
### All the numbers in a string
```{r all the numbers}
extract_numbers(pop)
```
### All the non-numbers in a string
```{r all the non-numbers}
extract_non_numerics(pop)
```
### Trim anything (not just whitespace)
`stringr`'s `str_trim` just trims whitespace. What if you want to trim something else? Now you can `trim_anything()`.
```{r trim anything}
trim_anything("__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](CONDUCT.md). By participating in this project you agree to abide by its terms.