-
Notifications
You must be signed in to change notification settings - Fork 2
/
README.Rmd
66 lines (51 loc) · 1.65 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
```{r setup, include=FALSE}
library(sourcetools)
library(microbenchmark)
```
<!-- badges: start -->
[![R-CMD-check](https://github.com/kevinushey/sourcetools/workflows/R-CMD-check/badge.svg)](https://github.com/kevinushey/sourcetools/actions)
<!-- badges: end -->
# sourcetools
Tools for reading, tokenizing, and (eventually) parsing `R` code.
## Getting Started
You can install `sourcetools` from CRAN with:
```{r, eval=FALSE}
install.packages("sourcetools")
```
Or, you can install the development version from GitHub with:
```{r, eval=FALSE}
devtools::install_github("kevinushey/sourcetools")
```
## Reading
`sourcetools` comes with a couple fast functions for reading
files into `R`.
Use `read()` and `read_lines()` to quickly read a file into
`R` as character vectors. `read_lines()` handles both
Windows style `\r\n` line endings, as well as Unix-style
`\n` endings. Performance is on par with the readers
provided by the
[readr](https://cran.r-project.org/package=readr) package.
```{r}
text <- replicate(10000, {
paste(sample(letters, 200, TRUE), collapse = "")
})
file <- tempfile()
cat(text, file = file, sep = "\n")
mb <- microbenchmark::microbenchmark(times = 10,
base::readLines(file),
readr::read_lines(file),
sourcetools::read_lines(file)
)
sm <- summary(mb)
print(sm[c("expr", "mean", "median")], digits = 3)
unlink(file)
```
## Tokenization
`sourcetools` provides the `tokenize_string()` and
`tokenize_file()` functions for generating a tokenized
representation of R code. These produce 'raw' tokenized
representations of the code, with each token's value as a
string, and a recorded row, column, and type:
```{r}
tokenize_string("if (x < 10) 20")
```