Skip to content

Commit cfc53e5

Browse files
Adding Rebus package demo R Markdown and DummyFiles for the data that is used for the demo. The demo is for generating a regular expression for number_range.
1 parent ca1cd9c commit cfc53e5

23 files changed

+542
-0
lines changed

.DS_Store

6 KB
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
2+
{\fonttbl}
3+
{\colortbl;\red255\green255\blue255;}
4+
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
2+
{\fonttbl}
3+
{\colortbl;\red255\green255\blue255;}
4+
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
2+
{\fonttbl}
3+
{\colortbl;\red255\green255\blue255;}
4+
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
2+
{\fonttbl}
3+
{\colortbl;\red255\green255\blue255;}
4+
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
2+
{\fonttbl}
3+
{\colortbl;\red255\green255\blue255;}
4+
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
2+
{\fonttbl}
3+
{\colortbl;\red255\green255\blue255;}
4+
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
2+
{\fonttbl}
3+
{\colortbl;\red255\green255\blue255;}
4+
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
2+
{\fonttbl}
3+
{\colortbl;\red255\green255\blue255;}
4+
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
2+
{\fonttbl}
3+
{\colortbl;\red255\green255\blue255;}
4+
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
2+
{\fonttbl}
3+
{\colortbl;\red255\green255\blue255;}
4+
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
2+
{\fonttbl}
3+
{\colortbl;\red255\green255\blue255;}
4+
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
2+
{\fonttbl}
3+
{\colortbl;\red255\green255\blue255;}
4+
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
2+
{\fonttbl}
3+
{\colortbl;\red255\green255\blue255;}
4+
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
2+
{\fonttbl}
3+
{\colortbl;\red255\green255\blue255;}
4+
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
2+
{\fonttbl}
3+
{\colortbl;\red255\green255\blue255;}
4+
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
2+
{\fonttbl}
3+
{\colortbl;\red255\green255\blue255;}
4+
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
2+
{\fonttbl}
3+
{\colortbl;\red255\green255\blue255;}
4+
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
2+
{\fonttbl}
3+
{\colortbl;\red255\green255\blue255;}
4+
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
2+
{\fonttbl}
3+
{\colortbl;\red255\green255\blue255;}
4+
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
5+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
title: "Generate a Regular Expression for a Number Range in R"
3+
author: "Andrew Gillreath-Brown"
4+
mainfont: Calibri
5+
output:
6+
html_document:
7+
code_folding: show
8+
keep_md: yes
9+
number_sections: yes
10+
theme: sandstone
11+
toc: yes
12+
toc_depth: 3
13+
toc_float: yes
14+
pdf_document:
15+
toc: yes
16+
toc_depth: '3'
17+
always_allow_html: yes
18+
---
19+
20+
Not too long ago, I was trying to find a simple way to get files that were in a particular year range in R. I tried writing regular expressions, but these always failed.
21+
22+
However, I eventually stumbled upon the `rebus` package that could make this extremely easy. Here, I have created a very simple demo to show the function `number_range`, which will generate the regular expression.
23+
24+
```{r load_packages, include=TRUE}
25+
26+
library(rebus)
27+
28+
```
29+
30+
Download the files and put them in your working directory. I simply named the folder “DummyFiles”.
31+
32+
```{r data, include=TRUE, results='asis'}
33+
34+
# Specify a directory for the dummy rtf files.
35+
DUMMY.DIR <- "../Data/DummyFiles/"
36+
37+
# Get list of all file names in the dummy directory.
38+
dummy.files <- list.files(paste0(DUMMY.DIR), recursive=T, full.names=T)
39+
40+
dummy.files
41+
42+
```
43+
44+
The regular expression, `rx`, that was generated by ‘number_range’ can now be used as the pattern in the ‘grep’ function and applied to the list of files.
45+
46+
```{r demo, include=TRUE, results='asis'}
47+
48+
# Use the rebus package to generate a regular expression for a number range,
49+
# which will allow for the extraction of specific years in the dummy data (i.e., 1948-1956).
50+
rx <- number_range(1948, 1956)
51+
52+
rx
53+
54+
# Now use the expression stored in rx, and trim list of files to to 1948:1956.
55+
dummy.files <- grep(rx, dummy.files, value = TRUE, perl = TRUE)
56+
57+
dummy.files
58+
59+
```

Number_Range_Expression/RebusPackageDemo.html

Lines changed: 298 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
title: "Generate a Regular Expression for a Number Range in R"
3+
author: "Andrew Gillreath-Brown"
4+
mainfont: Calibri
5+
output:
6+
html_document:
7+
code_folding: show
8+
keep_md: yes
9+
number_sections: yes
10+
theme: sandstone
11+
toc: yes
12+
toc_depth: 3
13+
toc_float: yes
14+
pdf_document:
15+
toc: yes
16+
toc_depth: '3'
17+
always_allow_html: yes
18+
---
19+
20+
Not too long ago, I was trying to find a simple way to get files that were in a particular year range in R. I tried writing regular expressions, but these always failed.
21+
22+
However, I eventually stumbled upon the `rebus` package that could make this extremely easy. Here, I have created a very simple demo to show the function `number_range`, which will generate the regular expression.
23+
24+
25+
```r
26+
library(rebus)
27+
```
28+
29+
Download the files and put them in your working directory. I simply named the folder “DummyFiles”.
30+
31+
32+
```r
33+
# Specify a directory for the dummy rtf files.
34+
DUMMY.DIR <- "../Data/DummyFiles/"
35+
36+
# Get list of all file names in the dummy directory.
37+
dummy.files <- list.files(paste0(DUMMY.DIR), recursive=T, full.names=T)
38+
39+
dummy.files
40+
```
41+
42+
[1] "../Data/DummyFiles//cai_tmin_us_us_30s_194501.rtf"
43+
[2] "../Data/DummyFiles//cai_tmin_us_us_30s_194601.rtf"
44+
[3] "../Data/DummyFiles//cai_tmin_us_us_30s_194701.rtf"
45+
[4] "../Data/DummyFiles//cai_tmin_us_us_30s_194811.rtf"
46+
[5] "../Data/DummyFiles//cai_tmin_us_us_30s_194901.rtf"
47+
[6] "../Data/DummyFiles//cai_tmin_us_us_30s_195001.rtf"
48+
[7] "../Data/DummyFiles//cai_tmin_us_us_30s_195101.rtf"
49+
[8] "../Data/DummyFiles//cai_tmin_us_us_30s_195201.rtf"
50+
[9] "../Data/DummyFiles//cai_tmin_us_us_30s_195301.rtf"
51+
[10] "../Data/DummyFiles//cai_tmin_us_us_30s_195401.rtf"
52+
[11] "../Data/DummyFiles//cai_tmin_us_us_30s_195501.rtf"
53+
[12] "../Data/DummyFiles//cai_tmin_us_us_30s_195601.rtf"
54+
[13] "../Data/DummyFiles//cai_tmin_us_us_30s_195701.rtf"
55+
[14] "../Data/DummyFiles//cai_tmin_us_us_30s_195801.rtf"
56+
[15] "../Data/DummyFiles//cai_tmin_us_us_30s_195901.rtf"
57+
[16] "../Data/DummyFiles//cai_tmin_us_us_30s_196001.rtf"
58+
[17] "../Data/DummyFiles//cai_tmin_us_us_30s_196101.rtf"
59+
[18] "../Data/DummyFiles//cai_tmin_us_us_30s_196201.rtf"
60+
[19] "../Data/DummyFiles//cai_tmin_us_us_30s_196301.rtf"
61+
62+
The regular expression, `rx`, that was generated by ‘number_range’ can now be used as the pattern in the ‘grep’ function and applied to the list of files.
63+
64+
65+
```r
66+
# Use the rebus package to generate a regular expression for a number range,
67+
# which will allow for the extraction of specific years in the dummy data (i.e., 1948-1956).
68+
rx <- number_range(1948, 1956)
69+
70+
rx
71+
```
72+
73+
<regex> (?:194[8-9]|195[0-6])
74+
75+
```r
76+
# Now use the expression stored in rx, and trim list of files to to 1948:1956.
77+
dummy.files <- grep(rx, dummy.files, value = TRUE, perl = TRUE)
78+
79+
dummy.files
80+
```
81+
82+
[1] "../Data/DummyFiles//cai_tmin_us_us_30s_194811.rtf"
83+
[2] "../Data/DummyFiles//cai_tmin_us_us_30s_194901.rtf"
84+
[3] "../Data/DummyFiles//cai_tmin_us_us_30s_195001.rtf"
85+
[4] "../Data/DummyFiles//cai_tmin_us_us_30s_195101.rtf"
86+
[5] "../Data/DummyFiles//cai_tmin_us_us_30s_195201.rtf"
87+
[6] "../Data/DummyFiles//cai_tmin_us_us_30s_195301.rtf"
88+
[7] "../Data/DummyFiles//cai_tmin_us_us_30s_195401.rtf"
89+
[8] "../Data/DummyFiles//cai_tmin_us_us_30s_195501.rtf"
90+
[9] "../Data/DummyFiles//cai_tmin_us_us_30s_195601.rtf"

0 commit comments

Comments
 (0)